X-Git-Url: https://git.cworth.org/git?p=lmno.games;a=blobdiff_plain;f=tictactoe%2Ftictactoe.jsx;h=d462ae14f164f34b74037d4820f919beaa60d3f1;hp=61a828eb751d6662e63681aa6e313cb6ed95dd20;hb=0602929f86c1939b1a52188ecf91096d087f98c7;hpb=9c92603bbe96c1bb54db5247ba73e7ea447845c1 diff --git a/tictactoe/tictactoe.jsx b/tictactoe/tictactoe.jsx index 61a828e..d462ae1 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"; } @@ -36,18 +45,31 @@ events.addEventListener("game-state", event => { }); function Square(props) { + let className = "square"; + + if (props.value) { + className += " occupied"; + } else if (props.active) { + className += " open"; + } + + const onClick = props.active ? props.onClick : null; + return ( - + ); } class Board extends React.Component { renderSquare(i) { + const value = this.props.squares[i]; return ( this.props.onClick(i)} /> ); @@ -96,8 +118,8 @@ class Game extends React.Component { squares: Array(9).fill(null) } ], - stepNumber: 0, - xIsNext: true + step_number: 0, + next_to_play: Team.X }; } @@ -112,27 +134,32 @@ class Game extends React.Component { squares: Array(9).fill(null) } ], - stepNumber: 0, - xIsNext: true + step_number: 0, + next_to_play: Team.X }); } receiveMove(i) { - const history = this.state.history.slice(0, this.state.stepNumber + 1); + const history = this.state.history.slice(0, this.state.step_number + 1); const current = history[history.length - 1]; const squares = current.squares.slice(); 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([ { squares: squares } ]), - stepNumber: history.length, - xIsNext: !this.state.xIsNext + step_number: history.length, + next_to_play: next_to_play }); } @@ -147,23 +174,16 @@ class Game extends React.Component { } } - jumpTo(step) { - this.setState({ - stepNumber: step, - xIsNext: (step % 2) === 0 - }); - } - render() { const history = this.state.history; - const current = history[this.state.stepNumber]; + const current = history[this.state.step_number]; const winner = calculateWinner(current.squares); let status; 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 ( @@ -173,6 +193,7 @@ class Game extends React.Component {
this.handleClick(i)} />