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;
const point = game.boardState[`${x}-${y}`]; let game = this;
const isTurn =
(game.turn === 1 && player === "black") || // if checkMove has not been run, determine legality
(game.turn === -1 && player === "white"); if (!game.move) {
if (isTurn) { game = game.checkMove({ player, pos: { x, y } });
if (point.legal) { }
game.pass = 0; if (
game.addToRecord({ player, pos: { x, y } }); // if move is legal
if (this.kos.length) helper.clearKo.call(this); // ? unclear if checking move values is beneficial to prevent race conditions
point.makeMove(game); game.move &&
game.turn *= -1; game.move.player === player &&
success = true; game.move.pos.x === x &&
} game.move.pos.y
) {
const point = game.boardState[`${x}-${y}`];
game.pass = 0;
// allows for recording of prior move on game record
game.addToRecord(game.move);
if (game.kos.length) helper.clearKo.call(game);
point.makeMove(game);
game.turn *= -1;
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 };
}, },