Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 65 additions & 5 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,34 +25,94 @@ const generateSquares = () => {
return squares;
}


const App = () => {


const [squares, setSquares] = useState(generateSquares());

const [player, setPlayer] = useState(PLAYER_1);
const [winner, setWinner] = useState("No one! it's a tie!!");

// Wave 2
// You will need to create a method to change the square
// When it is clicked on.
// Then pass it into the squares as a callback
const onClickCallback = (id) => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Nice!

const newSquares = [];

for (let row of squares) {
const newRow = [];
newSquares.push(newRow);
for (let col of row) {
const newCol = {...col}
if (newCol.id === id) {
if (newCol.value !== "" ||
winner === "Player 1" ||
winner === "Player 2") {
return;
}
newCol.value = player
};
newRow.push(newCol);
};
};

setSquares(newSquares);
checkForWinner(newSquares);

const newPlayer = (player === PLAYER_1) ? PLAYER_2 : PLAYER_1
setPlayer(newPlayer);
}

const checkForWinner = () => {
const checkForWinner = (squares) => {
// Complete in Wave 3
const values = squares.flat().map((square) => square.value);

const threeX = ['X','X','X'];
const threeO = ['O','O','O'];

if (arrayEquals([values[0],values[1],values[2]], threeX) ||
arrayEquals([values[3],values[4],values[5]], threeX) ||
arrayEquals([values[6],values[7],values[8]], threeX) ||
arrayEquals([values[0],values[3],values[6]], threeX) ||
arrayEquals([values[1],values[4],values[7]], threeX) ||
arrayEquals([values[2],values[5],values[8]], threeX) ||
arrayEquals([values[0],values[4],values[8]], threeX) ||
arrayEquals([values[2],values[4],values[6]], threeX)) {
return setWinner("Player 1")
} else if (arrayEquals([values[0],values[1],values[2]], threeO) ||
arrayEquals([values[3],values[4],values[5]], threeO) ||
arrayEquals([values[6],values[7],values[8]], threeO) ||
arrayEquals([values[0],values[3],values[6]], threeO) ||
arrayEquals([values[1],values[4],values[7]], threeO) ||
arrayEquals([values[2],values[5],values[8]], threeO) ||
arrayEquals([values[0],values[4],values[8]], threeO) ||
arrayEquals([values[2],values[4],values[6]], threeO)) {
return setWinner("Player 2")
} else {
return setWinner("No one! it's a tie!!")
};
}

const arrayEquals = (one, two) => {
return JSON.stringify(one) === JSON.stringify(two);
Comment on lines +97 to +98

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see you saw the post in Slack. nice!

}

const resetGame = () => {
// Complete in Wave 4
window.location.reload();
// found this on upmostly.com
Comment on lines +103 to +104

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That works, but you could also just do setSquares(generateSquares()); and set the player to player 1.

}

return (
<div className="App">
<header className="App-header">
<h1>React Tic Tac Toe</h1>
<h2>The winner is ... -- Fill in for wave 3 </h2>
<button>Reset Game</button>
<h2>The winner is {winner} </h2>
<button onClick={resetGame}>Reset Game</button>
</header>
<main>
<Board squares={squares} />
<Board squares={squares} onClickCallback={onClickCallback} />
</main>
</div>
);
Expand Down
18 changes: 18 additions & 0 deletions src/components/Board.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,27 @@ import Square from './Square';
import PropTypes from 'prop-types';



const generateSquareComponents = (squares, onClickCallback) => {
// Complete this for Wave 1

const squareBoxes = [];

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works, but you could also use the .flat function.


for (let row of squares) {
for (let col of row) {
squareBoxes.push(
<Square
id={col.id}
value={col.value}
onClickCallback={onClickCallback}
key={col.id}
/>
);
};
};

return squareBoxes;

}

const Board = ({ squares, onClickCallback }) => {
Expand Down
3 changes: 2 additions & 1 deletion src/components/Square.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ const Square = (props) => {
// For Wave 1 enable this
// Component to alert a parent
// component when it's clicked on.
const buttonClick = () => props.onClickCallback(props.id);

return <button
return <button onClick={buttonClick}
className="square"
>
{props.value}
Expand Down