From: Carl Worth <cworth@cworth.org>
Date: Mon, 1 Jun 2020 14:30:06 +0000 (-0700)
Subject: tictactoe: Actually toggle between X and O as the current player
X-Git-Url: https://git.cworth.org/git?a=commitdiff_plain;h=8adcf9df208ac8d303706f89d359f69bbdc0a7a2;p=lmno-server

tictactoe: Actually toggle between X and O as the current player

It is kind of hilarious that I hadn't even noticed that up to now
every move committed to the board was an "X". I hadn't noticed this
because the server state is currently only used to determine if a new
move is legal, (in that the board is unoccupied). The code currently
doesn't test for a win, for example.

And meanwhile, the client has been tracking toggling X and O on its
own just fine.

But this will certainly be cleaner going forward, and is strictly
necessary before we serve an event that exposes the entire board
state.
---

diff --git a/tictactoe.js b/tictactoe.js
index f9b357b..8efc70f 100644
--- a/tictactoe.js
+++ b/tictactoe.js
@@ -8,6 +8,7 @@ class TicTacToe extends Game {
     super(id);
     this.moves = [];
     this.board = Array(9).fill("");
+    this.next_player = "X";
   }
 
   /* Returns Boolean indicating whether move was legal and added. */
@@ -16,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;
   }