Permalink
Browse files

Complete tic-tac-toe

  • Loading branch information...
Jeff-Enriquez committed Feb 14, 2020
1 parent f8b22e7 commit 99aa2839f16de4fb190ed857a26f2f77f422d357
Showing with 30 additions and 6 deletions.
  1. +15 −0 .vscode/launch.json
  2. +15 −6 src/index.js
@@ -0,0 +1,15 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "chrome",
"request": "launch",
"name": "Launch Chrome against localhost",
"url": "http://localhost:3000",
"webRoot": "${workspaceFolder}"
}
]
}
@@ -50,35 +50,44 @@ class Game extends React.Component {
history: [{
squares: Array(9).fill(null),
}],
stepNumber: 0,
isXNext: true,
};
}

handleClick(i) {
const history = this.state.history;
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';
squares[i] = this.state.isXNext ? 'X' : 'O';
this.setState({
history: history.concat([{
squares: squares,
}]),
xIsNext: !this.state.xIsNext,
stepNumber: history.length,
isXNext: !this.state.isXNext,
})
}

jumpTo(step) {
this.setState({
stepNumber: step,
isXNext: (step % 2) === 0,
});
}

render() {
const history = this.state.history;
const current = history[history.length - 1];
const current = history[this.state.stepNumber];
const winner = calculateWinner(current.squares);

const moves = history.map((step, move) => {
const desc = move ? 'Go to move #' + move : 'Go to game start';
return (
<li>
<li key={move}>
<button onClick={() => this.jumpTo(move)}>{desc}</button>
</li>
)
@@ -88,7 +97,7 @@ class Game extends React.Component {
if (winner) {
status = 'Winner: ' + winner;
} else {
status = 'Next player: ' + (this.state.xIsNext ? 'X' : 'O');
status = 'Next player: ' + (this.state.isXNext ? 'X' : 'O');
}

return (

0 comments on commit 99aa283

Please sign in to comment.