refactor Game.makeMove to check legality only if checkMove has not yet been called

This commit is contained in:
sorrelbri 2020-07-07 17:08:02 -07:00
parent cfec49845b
commit 0f3e7942ef

View file

@ -266,26 +266,35 @@ const Game = ({ gameData = {}, gameRecord = [] } = {}) => {
if (this.pass > 1) { if (this.pass > 1) {
return { ...this, success: false }; return { ...this, success: false };
} }
if (x === 0) return game.submitPass(player);
if (x === 0) return this.submitPass(player);
let game = this;
let success = false; let success = false;
let game = this;
// if checkMove has not been run, determine legality
if (!game.move) {
game = game.checkMove({ player, pos: { x, y } });
}
if (
// if move is legal
// ? unclear if checking move values is beneficial to prevent race conditions
game.move &&
game.move.player === player &&
game.move.pos.x === x &&
game.move.pos.y
) {
const point = game.boardState[`${x}-${y}`]; const point = game.boardState[`${x}-${y}`];
const isTurn =
(game.turn === 1 && player === "black") ||
(game.turn === -1 && player === "white");
if (isTurn) {
if (point.legal) {
game.pass = 0; game.pass = 0;
game.addToRecord({ player, pos: { x, y } }); // allows for recording of prior move on game record
if (this.kos.length) helper.clearKo.call(this); game.addToRecord(game.move);
if (game.kos.length) helper.clearKo.call(game);
point.makeMove(game); point.makeMove(game);
game.turn *= -1; game.turn *= -1;
success = true; success = true;
} }
}
game.boardState = getBoardState(game); game.boardState = getBoardState(game);
// remove move attribute to prevent duplicate moves
delete game.move;
return { ...game, legalMoves: getLegalMoves(game), success }; return { ...game, legalMoves: getLegalMoves(game), success };
}, },