From 2b049fa2c736d7d35beb07a74469e71a56f637ac Mon Sep 17 00:00:00 2001 From: Carl Worth Date: Thu, 4 Jun 2020 16:15:42 -0700 Subject: [PATCH] Add an enum (or an enum-like object) for Team values 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. --- tictactoe/tictactoe.jsx | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/tictactoe/tictactoe.jsx b/tictactoe/tictactoe.jsx index d92b698..42c32ef 100644 --- a/tictactoe/tictactoe.jsx +++ b/tictactoe/tictactoe.jsx @@ -1,3 +1,12 @@ +const Team = { + X: 0, + O: 1, + properties: { + 0: {name: "X"}, + 1: {name: "O"} + } +}; + function undisplay(element) { element.style.display="none"; } @@ -110,7 +119,7 @@ class Game extends React.Component { } ], stepNumber: 0, - xIsNext: true + next_to_play: Team.X }; } @@ -126,7 +135,7 @@ class Game extends React.Component { } ], stepNumber: 0, - xIsNext: true + next_to_play: Team.X }); } @@ -137,7 +146,12 @@ class Game extends React.Component { 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([ { @@ -145,7 +159,7 @@ class Game extends React.Component { } ]), stepNumber: history.length, - xIsNext: !this.state.xIsNext + next_to_play: next_to_play }); } @@ -163,7 +177,7 @@ class Game extends React.Component { jumpTo(step) { this.setState({ stepNumber: step, - xIsNext: (step % 2) === 0 + next_to_play: (step % 2) === 0 }); } @@ -176,7 +190,7 @@ class Game extends React.Component { 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); } return ( -- 2.43.0