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.
super(id);
this.moves = [];
this.board = Array(9).fill("");
super(id);
this.moves = [];
this.board = Array(9).fill("");
+ this.next_player = "X";
}
/* Returns Boolean indicating whether move was legal and added. */
}
/* Returns Boolean indicating whether move was legal and added. */
if (this.board[square])
return false;
if (this.board[square])
return false;
- this.board[square] = 'X';
+ this.board[square] = this.next_player;
+ if (this.next_player === "X")
+ this.next_player = "O";
+ else
+ this.next_player = "X";
+