]> git.cworth.org Git - empires-server/commitdiff
scribe: Reject moves that are illegal by the move constraint
authorCarl Worth <cworth@cworth.org>
Mon, 6 Jul 2020 05:16:11 +0000 (22:16 -0700)
committerCarl Worth <cworth@cworth.org>
Mon, 6 Jul 2020 05:36:59 +0000 (22:36 -0700)
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

index 46be6a44f8969d74ba653e3ce6514e4070e89116..f008bd2d8e05dd454c2e213d730b2f379397684f 100644 (file)
--- a/scribe.js
+++ b/scribe.js
@@ -25,8 +25,28 @@ class Scribe extends Game {
     if (! result.legal)
       return result;
 
     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])
 
     /* Cannot move to an occupied square. */
     if (state.squares[i][j])