From 9c0ec54213a93d2b3552965a4bedccc0b7bd1e2c Mon Sep 17 00:00:00 2001 From: Carl Worth Date: Sun, 5 Jul 2020 22:16:11 -0700 Subject: [PATCH] scribe: Reject moves that are illegal by the move constraint If the player has a previous move, (this isn't their first move of the game), then the mini-grid being moved to must match the position within the mini-grid of the previous move, (unless the indicated mini grid is full). --- scribe.js | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/scribe.js b/scribe.js index 46be6a4..f008bd2 100644 --- a/scribe.js +++ b/scribe.js @@ -25,8 +25,28 @@ class Scribe extends Game { if (! result.legal) return result; - /* TODO: Need to enforce super-square matching mini-square from - * previous move (if given super-square isn't full already). */ + /* Ensure move is legal by Scribe rules. First, if this is only + * the first or second move, then any move is legal. + */ + if (state.moves.length >= 2) { + const prev = state.moves.slice(-2, -1)[0]; + + /* Then check for mini-grid compatibility with move from two moves ago. */ + if (move[0] != prev[1]) { + /* This can still be legal if the target mini grid is full. */ + let count = 0; + for (let square of state.squares[prev[1]]) { + if (square) + count++; + } + if (count != 9) { + return { legal: false, + message: "Move is inconsistent with your previous move" }; + } + } + + } + /* Cannot move to an occupied square. */ if (state.squares[i][j]) -- 2.43.0