]> git.cworth.org Git - lmno-server/blobdiff - tictactoe.js
Rename "next_player" property to "team_to_play"
[lmno-server] / tictactoe.js
index d5a07b25506b79618993ed9ec67befb6ee289936..256afecde34d86fd12af215d089c0b6cae9edcee 100644 (file)
@@ -7,25 +7,44 @@ class TicTacToe extends Game {
     this.state = {
       moves: [],
       board: Array(9).fill(""),
-      next_player: "X",
+      team_to_play: "X",
     };
+    this.teams = ["X", "O"];
   }
 
   /* Returns true if move was legal and added, false otherwise. */
-  add_move(square) {
+  add_move(player, square) {
+
+    /* Cannot move if you are not on a team. */
+    if (player.team === "")
+    {
+      return { legal: false,
+               message: "You must be on a team to take a turn" };
+    }
+
+    /* Cannot move if it's not this player's team's turn. */
+    if (player.team !== this.state.team_to_play)
+    {
+      return { legal: false,
+               message: "It's not your turn to move" };
+    }
+
     /* Cannot move to an occupied square. */
     if (this.state.board[square])
-      return false;
+    {
+      return { legal: false,
+               message: "Square is already occupied" };
+    }
 
-    this.state.board[square] = this.state.next_player;
+    this.state.board[square] = this.state.team_to_play;
     this.state.moves.push(square);
 
-    if (this.state.next_player === "X")
-      this.state.next_player = "O";
+    if (this.state.team_to_play === "X")
+      this.state.team_to_play = "O";
     else
-      this.state.next_player = "X";
+      this.state.team_to_play = "X";
 
-    return true;
+    return { legal: true };
   }
 }