this.handleClick(i)}/>
}
render() {
- const status = 'Next player: X';
+ const winner = calculateWinner(this.state.squares);
+ let status;
+
+ if (winner) {
+ status = 'Winner: ' + winner;
+ }
+ else {
+ status = `Next player: ${this.state.xIsNext ? 'X' : 'O'}`;
+ }
return(
@@ -64,4 +113,28 @@ class Game extends React.Component {
);
}
}
+
+function calculateWinner(squares: any) {
+ const lines = [
+ [0, 1, 2],
+ [3, 4, 5],
+ [6, 7, 8],
+ [0, 3, 6],
+ [1, 4, 7],
+ [2, 5, 8],
+ [0, 4, 8],
+ [2, 4, 6],
+ ];
+
+ lines.forEach((i: any) => {
+ const [a, b, c] = i;
+
+ if (squares[a] && squares[b] === squares[a] && squares[c]) {
+ return squares[a];
+ }
+ });
+
+ return null;
+}
+
export default App;