X-Git-Url: https://git.cworth.org/git?p=lmno.games;a=blobdiff_plain;f=tictactoe%2Ftictactoe.jsx;h=42c32ef29041345a03d2ca2ddb7ae5cbea5fd2c2;hp=d92b698fe57b913e8d8625643b18376bf0262947;hb=2b049fa2c736d7d35beb07a74469e71a56f637ac;hpb=cd1247338dd987b8796ad5ca6d0f655ececa22ee 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 (