Created
January 30, 2019 23:47
-
-
Save iagox86/0c594c8b47da18df152bf639aa39609d 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
import React from 'react'; | |
import ReactDOM from 'react-dom'; | |
import _ from 'lodash'; | |
import './index.css'; | |
function Square(props) { | |
return ( | |
<button className="square" onClick={props.onClick}> | |
{props.value} | |
</button> | |
); | |
} | |
function calculateWinner(squares) { | |
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], | |
]; | |
for(var i = 0; i < lines.length; i++) { | |
const e = lines[i]; | |
if(squares[e[0]] && squares[e[0]] === squares[e[1]] && squares[e[1]] === squares[e[2]]) { | |
return squares[e[0]]; | |
} | |
}; | |
return null; | |
} | |
function calculateMovesLeft(squares) { | |
return _.reject(squares, (s) => s).length | |
} | |
class Board extends React.Component { | |
renderSquare(i) { | |
return ( | |
<Square | |
value={this.props.squares[i]} | |
onClick={() => this.props.onClick(i)} | |
/> | |
); | |
} | |
render() { | |
return ( | |
<div> | |
<div className="board-row"> | |
{this.renderSquare(0)} | |
{this.renderSquare(1)} | |
{this.renderSquare(2)} | |
</div> | |
<div className="board-row"> | |
{this.renderSquare(3)} | |
{this.renderSquare(4)} | |
{this.renderSquare(5)} | |
</div> | |
<div className="board-row"> | |
{this.renderSquare(6)} | |
{this.renderSquare(7)} | |
{this.renderSquare(8)} | |
</div> | |
</div> | |
); | |
} | |
} | |
class Game extends React.Component { | |
handleClick(i) { | |
const new_squares = this.state.history[0].slice(); | |
if(calculateWinner(new_squares)) { | |
alert("There is already a winner!"); | |
return; | |
} else if(new_squares[i]) { | |
alert("There is already something in that square!"); | |
return; | |
} | |
new_squares[i] = this.state.currentPlayer; | |
this.setState({ | |
history: [new_squares, ...this.state.history], | |
currentPlayer: this.state.currentPlayer === 'X' ? 'O' : 'X', | |
}); | |
} | |
reset() { | |
this.setState({ | |
history: [Array(9).fill(null)], | |
currentPlayer: 'X', | |
}); | |
} | |
undo(n=1) { | |
if(this.state.history.length < n + 1) { | |
alert("Not enough state!"); | |
return; | |
} | |
this.setState({ | |
history: this.state.history.slice(n), | |
currentPlayer: (this.state.history.length - n) % 2 ? 'X' : 'O', | |
}); | |
} | |
constructor(props) { | |
super(props); | |
this.state = { | |
history: [Array(9).fill(null)], | |
currentPlayer: 'X', | |
}; | |
} | |
render() { | |
const winner = calculateWinner(this.state.history[0]); | |
let status = 'Next player: ' + this.state.currentPlayer; | |
if(winner) { | |
status = "WINNER: " + winner; | |
} else if(calculateMovesLeft(this.state.history[0]) === 0) { | |
status = "DRAW!"; | |
} | |
const moves = _.map(this.state.history.slice(1), (move, index) => { | |
return ( | |
<li key={move}> | |
<button onClick={() => this.undo(this.state.history.length - index - 1)}>Go back to move {index}</button> | |
</li> | |
) | |
}); | |
return ( | |
<div> | |
<div className="game"> | |
<div className="game-board"> | |
<Board | |
squares={this.state.history[0]} | |
currentPlayer={this.state.currentPlayer} | |
onClick={(i) => this.handleClick(i)} | |
/> | |
</div> | |
<div className="game-info"> | |
<div>{ status }</div> | |
<ul>{ moves }</ul> | |
</div> | |
</div> | |
<button onClick={() => this.reset()}>Reset</button> | |
<button onClick={() => this.undo()}>Undo</button> | |
</div> | |
); | |
} | |
} | |
// ======================================== | |
ReactDOM.render( | |
<Game />, | |
document.getElementById('root') | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment