]> git.cworth.org Git - lmno-server/blobdiff - tictactoe.js
tictactoe: Actually toggle between X and O as the current player
[lmno-server] / tictactoe.js
index 12bf6801ac0f20e4f36e444bbb9fab1909fce116..8efc70f8e791fe20d1aa68233667516a52ed5af0 100644 (file)
@@ -1,15 +1,14 @@
 const express = require("express");
 const Game = require("./game.js");
 
-const engine_name = "tictactoe";
-
 const router = express.Router();
 
 class TicTacToe extends Game {
-  constructor() {
-    super(engine_name);
+  constructor(id) {
+    super(id);
     this.moves = [];
-    this.board = Array(9).fill(null);
+    this.board = Array(9).fill("");
+    this.next_player = "X";
   }
 
   /* Returns Boolean indicating whether move was legal and added. */
@@ -18,9 +17,14 @@ class TicTacToe extends Game {
     if (this.board[square])
       return false;
 
-    this.board[square] = 'X';
+    this.board[square] = this.next_player;
     this.moves.push(square);
 
+    if (this.next_player === "X")
+      this.next_player = "O";
+    else
+      this.next_player = "X";
+
     return true;
   }
 
@@ -67,5 +71,3 @@ TicTacToe.meta = {
   name: "Tic Tac Toe",
   identifier: "tictactoe"
 };
-
-exports.meta = TicTacToe.meta;