Skip to content

Commit

Permalink
1st step: Added highlight last move functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mrcel97 committed Aug 23, 2018
1 parent ab17d17 commit a9ecb61
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,8 @@ ol, ul {

.coordinates {
list-style: none;
}

.selected {
color: greenyellow;
}
17 changes: 14 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Game extends React.Component {
history: [{
squares: Array(9).fill(null),
coordinates: Array(9).fill(null),
lastPush: null
}],
stepNumber: 0,
xIsNext: true,
Expand Down Expand Up @@ -90,14 +91,14 @@ class Game extends React.Component {
} else {
status = 'Next Player: ' + (this.state.xIsNext ? 'X' : 'O');
}

return (
<div className="game">
<div className="game-board">
<Board
<Board
squares={this.current.squares}
winnerLine={winnerResult}
onClick={(i, pos) => this.handleClick(i, pos)}
lastPush={this.state.history[this.state.stepNumber].lastPush}
/>
</div>
<div className="game-info">
Expand All @@ -120,6 +121,7 @@ class Board extends React.Component {
var pos = calculatePos(i);
return (
<Square
selected={this.props.lastPush === i}
winner={isWinner(i, this.props.winnerLine)}
value={this.props.squares[i]}
onClick={() => this.props.onClick(i, pos)}
Expand Down Expand Up @@ -153,7 +155,7 @@ class Board extends React.Component {
function Square(props) {
var liClasses = "square"

if (props.winner) { liClasses = liClasses.concat(" winner") }
liClasses = applyClasses(props, liClasses);

return (
<button className={liClasses} onClick={props.onClick}>
Expand All @@ -162,6 +164,15 @@ function Square(props) {
);
}

function applyClasses(props, liClasses) {
if (props.winner) {
liClasses = liClasses.concat(" winner")
} else if (props.selected) {
liClasses = liClasses.concat(" selected")
}
return liClasses;
}

function calculateWinner(squares) {
const winnerLine = [];
const lines = [
Expand Down

0 comments on commit a9ecb61

Please sign in to comment.