Initial Commit
This commit is contained in:
71
index.js
Normal file
71
index.js
Normal file
@@ -0,0 +1,71 @@
|
||||
async function getPythonData(path, json) {
|
||||
try {
|
||||
const response = await fetch(path, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(json)
|
||||
});
|
||||
|
||||
// This waits for the JSON to be unpacked
|
||||
const data = await response.json();
|
||||
|
||||
// Now you can return the actual data object
|
||||
return data;
|
||||
} catch (error) {
|
||||
console.error("Failed to get data:", error);
|
||||
}
|
||||
}
|
||||
|
||||
function onDrop(source, target, piece, newPos, oldPos, orientation) {
|
||||
if (source == target || moves.length === 10) return 'snapback';
|
||||
getPythonData("https://honey-motel.bnr.la/app/move", { uci: source + target, fen: fen })
|
||||
.then(result => {
|
||||
board.position(result.fen)
|
||||
fen = result.fen
|
||||
if (result.san == undefined) return
|
||||
moves.push(result.san)
|
||||
document.getElementById("guess"+guessNum+"ply"+moves.length).innerText = result.san
|
||||
})
|
||||
}
|
||||
|
||||
let answer = ["Nf3", "f6", "e3", "d5", "Ng5", "fxg5", "Qh5+", "g6", "Qe5", "Be6"]
|
||||
|
||||
function validateGuess() {
|
||||
if (moves.length != 10) return;
|
||||
for (let i = 0; i < 10; i++) {
|
||||
if (answer.includes(moves[i])) {
|
||||
document.getElementById("guess"+guessNum+"ply"+(i+1)).style.backgroundColor = "orange"
|
||||
}
|
||||
if (moves[i] === answer[i]) {
|
||||
document.getElementById("guess"+guessNum+"ply"+(i+1)).style.backgroundColor = "green"
|
||||
}
|
||||
}
|
||||
document.getElementById("guessDisplay").innerHTML += "<br>"
|
||||
guessNum++
|
||||
for (let i = 0; i < 10; i++) {
|
||||
document.getElementById("guessDisplay").innerHTML += `<div id="guess${guessNum}ply${i + 1}" class="move">- -</div>
|
||||
`
|
||||
}
|
||||
fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
|
||||
board.position(fen)
|
||||
moves = []
|
||||
}
|
||||
|
||||
function resetBoard() {
|
||||
fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
|
||||
board.position(fen)
|
||||
moves = []
|
||||
for (let i = 0; i < 10; i++) {
|
||||
document.getElementById("guess"+guessNum+"ply"+(i+1)).innerText = "- -"
|
||||
}
|
||||
}
|
||||
|
||||
let fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
|
||||
let moves = []
|
||||
let guessNum = 1
|
||||
const board = Chessboard('board', {
|
||||
position: 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR',
|
||||
pieceTheme: './pieces/{piece}.png',
|
||||
draggable: true,
|
||||
onDrop: onDrop,
|
||||
});
|
||||
Reference in New Issue
Block a user