]> git.cworth.org Git - empires-server/blobdiff - scribe.js
Initial implementation of Scribe
[empires-server] / scribe.js
diff --git a/scribe.js b/scribe.js
new file mode 100644 (file)
index 0000000..46be6a4
--- /dev/null
+++ b/scribe.js
@@ -0,0 +1,60 @@
+const express = require("express");
+const Game = require("./game.js");
+
+class Scribe extends Game {
+  constructor(id) {
+    super(id);
+    this.teams = [{id:0, name:"+"}, {id:1, name:"o"}];
+    this.state = {
+      moves: [],
+      squares: Array(9).fill(null).map(() => Array(9).fill(null)),
+      team_to_play: this.teams[0],
+    };
+  }
+
+  /* Returns true if move was legal and added, false otherwise. */
+  add_move(player, move) {
+
+    const state = this.state;
+    const i = move[0];
+    const j = move[1];
+    const result = super.add_move(player, move);
+
+    /* If the generic Game class can reject this move, then we don't
+     * need to look at it any further. */
+    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). */
+
+    /* Cannot move to an occupied square. */
+    if (state.squares[i][j])
+    {
+      return { legal: false,
+               message: "Square is already occupied" };
+    }
+
+    state.squares[i][j] = state.team_to_play;
+    state.moves.push(move);
+
+    if (state.team_to_play.id === 0)
+      state.team_to_play = this.teams[1];
+    else
+      state.team_to_play = this.teams[0];
+
+    return { legal: true };
+  }
+}
+
+Scribe.router = express.Router();
+
+Scribe.meta = {
+  name: "Scribe",
+  identifier: "scribe",
+  options: {
+    allow_guest: true
+  }
+};
+
+exports.Game = Scribe;