From: Carl Worth <cworth@cworth.org>
Date: Mon, 6 Jul 2020 05:16:11 +0000 (-0700)
Subject: scribe: Reject moves that are illegal by the move constraint
X-Git-Url: https://git.cworth.org/git?a=commitdiff_plain;h=9c0ec54213a93d2b3552965a4bedccc0b7bd1e2c;p=empires-server

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).
---

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])