Skip to content

Instantly share code, notes, and snippets.

@thiskevinwang
Last active February 6, 2019 21:37
Show Gist options
  • Save thiskevinwang/a5d32ed4dd3b111a413c53273d7d53fc to your computer and use it in GitHub Desktop.
Save thiskevinwang/a5d32ed4dd3b111a413c53273d7d53fc to your computer and use it in GitHub Desktop.
// 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