We replace the existing xIsNext Boolean in the state with a new
next_to_play property takign a value from this enum.
And we will soon also be using this enum to track the team from whose
point of view the game is being shown.
+const Team = {
+ X: 0,
+ O: 1,
+ properties: {
+ 0: {name: "X"},
+ 1: {name: "O"}
+ }
+};
+
function undisplay(element) {
element.style.display="none";
}
function undisplay(element) {
element.style.display="none";
}
if (calculateWinner(squares) || squares[i]) {
return;
}
if (calculateWinner(squares) || squares[i]) {
return;
}
- squares[i] = this.state.xIsNext ? "X" : "O";
+ squares[i] = Team.properties[this.state.next_to_play].name;
+ let next_to_play;
+ if (this.state.next_to_play === Team.X)
+ next_to_play = Team.O;
+ else
+ next_to_play = Team.X;
this.setState({
history: history.concat([
{
this.setState({
history: history.concat([
{
}
]),
stepNumber: history.length,
}
]),
stepNumber: history.length,
- xIsNext: !this.state.xIsNext
+ next_to_play: next_to_play
jumpTo(step) {
this.setState({
stepNumber: step,
jumpTo(step) {
this.setState({
stepNumber: step,
- xIsNext: (step % 2) === 0
+ next_to_play: (step % 2) === 0
if (winner) {
status = "Winner: " + winner;
} else {
if (winner) {
status = "Winner: " + winner;
} else {
- status = "Next player: " + (this.state.xIsNext ? "X" : "O");
+ status = "Next player: " + (Team.properties[this.state.next_to_play].name);