Last active
February 6, 2019 21:37
-
-
Save thiskevinwang/a5d32ed4dd3b111a413c53273d7d53fc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Game component: | |
handleClick(i) { | |
const locations = [ | |
[1, 1], | |
[2, 1], | |
[3, 1], | |
[1, 2], | |
[2, 2], | |
[3, 2], | |
[1, 3], | |
[2, 3], | |
[3, 3] | |
]; | |
const history = this.state.history.slice(0, this.state.stepNumber + 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"; | |
this.setState({ | |
history: history.concat([ | |
{ | |
squares: squares, | |
location: locations[i] | |
} | |
]), | |
stepNumber: history.length, | |
xIsNext: !this.state.xIsNext | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment