]> 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 5b5270d21c41a001e0df9c81ca5deda95ba4778b..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;
   }
 
@@ -38,15 +42,6 @@ class TicTacToe extends Game {
   }
 }
 
-router.get('/', (request, response) => {
-  const game = request.game;
-
-  if (! request.session.nickname)
-    response.render('choose-nickname.html', { game_name: game.meta.name });
-  else
-    response.render(`${game.meta.identifier}-game.html`);
-});
-
 router.post('/move', (request, response) => {
   const game = request.game;
   const square = request.body.square;
@@ -70,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;