mirror of
https://github.com/TheRealOwenRees/chess-endgame-trainer.git
synced 2026-11-18 11:42:07 +00:00
Play the opponent's optimal defence after a passing move
#8: after a passing move, fetch the post-move position's tablebase and reply with moves[0] (the defender's optimal defence). The client applies and animates the reply, reports the resulting FEN, and the server fetches that position's tablebase before resuming the user's turn. Losing moves and terminal positions do not trigger a reply; a failed post-reply fetch blocks the attempt. The board stays locked during the reply round-trip so play cannot get out of sync.
This commit is contained in:
@@ -5,6 +5,7 @@ export const ChessBoard = {
|
||||
mounted() {
|
||||
this.chess = new Chess();
|
||||
this.ground = null;
|
||||
this.playerColor = "white";
|
||||
|
||||
this.handleEvent("init_board", (payload) => {
|
||||
this.initBoard(payload);
|
||||
@@ -37,6 +38,14 @@ export const ChessBoard = {
|
||||
}
|
||||
});
|
||||
|
||||
this.handleEvent("opponent_move", ({ from, to, promotion, san }) => {
|
||||
this.applyOpponentMove({ from, to, promotion, san });
|
||||
});
|
||||
|
||||
this.handleEvent("resume_board", () => {
|
||||
this.resumeBoard();
|
||||
});
|
||||
|
||||
this.initBoard({
|
||||
fen: this.chess.fen(),
|
||||
orientation: "white",
|
||||
@@ -56,6 +65,8 @@ export const ChessBoard = {
|
||||
return;
|
||||
}
|
||||
|
||||
this.playerColor = player_color;
|
||||
|
||||
if (this.ground) {
|
||||
this.ground.destroy();
|
||||
}
|
||||
@@ -171,4 +182,42 @@ export const ChessBoard = {
|
||||
this.ground.set({ fen: this.chess.fen() });
|
||||
}
|
||||
},
|
||||
|
||||
applyOpponentMove({ from, to, promotion, san }) {
|
||||
const move = promotion
|
||||
? this.chess.move({ from, to, promotion })
|
||||
: this.chess.move({ from, to });
|
||||
|
||||
if (!move) {
|
||||
console.error("Opponent move rejected by chess.js", { from, to, promotion, san });
|
||||
return;
|
||||
}
|
||||
|
||||
const nextTurnColor = this.chess.turn() === "w" ? "white" : "black";
|
||||
|
||||
this.ground.set({
|
||||
fen: this.chess.fen(),
|
||||
turnColor: nextTurnColor,
|
||||
movable: { color: null, dests: new Map() },
|
||||
});
|
||||
|
||||
this.pushEvent("opponent_reply_applied", {
|
||||
fen: this.chess.fen(),
|
||||
san: move.san,
|
||||
});
|
||||
},
|
||||
|
||||
resumeBoard() {
|
||||
const turnColor = this.chess.turn() === "w" ? "white" : "black";
|
||||
|
||||
this.ground.set({
|
||||
fen: this.chess.fen(),
|
||||
turnColor,
|
||||
movable: {
|
||||
color: this.playerColor || turnColor,
|
||||
free: false,
|
||||
dests: this.getValidDests(),
|
||||
},
|
||||
});
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user