]> 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 ca67834ecc5790da0a1d125b9a0038d4a58a28cf..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;
   }
 
@@ -61,12 +65,9 @@ router.get('/events', (request, response) => {
 });
 
 exports.router = router;
-exports.name = engine_name;
 exports.Game = TicTacToe;
 
 TicTacToe.meta = {
   name: "Tic Tac Toe",
   identifier: "tictactoe"
 };
-
-exports.meta = TicTacToe.meta;