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
15,893 changes: 15,893 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

88 changes: 66 additions & 22 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import './App.css';

import Board from './components/Board';

const PLAYER_1 = 'X';
const PLAYER_2 = 'O';
const PLAYER_1 = 'x';
const PLAYER_2 = 'o';

const generateSquares = () => {
const squares = [];
Expand All @@ -27,42 +27,86 @@ const generateSquares = () => {

const App = () => {

// This starts state off as a 2D array of JS objects with
// empty value and unique ids.
const [squares, setSquares] = useState(generateSquares());
const [player, setPlayer] = useState(PLAYER_1);
const [winner, setWinner] = useState(null);

// 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 updateSquare = (squareId) => {

Choose a reason for hiding this comment

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

👍

if (winner !== null) return;
const squaresNew = generateSquares();
for (let row = 0; row < 3; row += 1) {
for (let col = 0; col < 3; col += 1) {
const square = squares[row][col];
if (square.id === squareId && square.value === ''){
squaresNew[row][col] = {id: squareId, value: player}
} else {
squaresNew[row][col] = {...square};
}
}
}
setPlayer(player === PLAYER_1 ? PLAYER_2 : PLAYER_1);
setSquares(squaresNew);
setWinner(checkForWinner(squaresNew));
}



const checkForWinner = (squares) => {
for (let row = 0; row < 3; row += 1){
const rowVals = [];
const colVals = [];
for (let col = 0; col < 3; col += 1){
rowVals.push(squares[row][col].value);
colVals.push(squares[col][row].value);
if (winnerHelper(rowVals)) {
return winnerHelper(rowVals);
Comment on lines +61 to +62

Choose a reason for hiding this comment

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

Do you need to call winnerHelper twice here?

}
if (winnerHelper(colVals)) {
return winnerHelper(colVals);
}
}
}

if (squares[0][0].value === squares[1][1].value && squares[1][1].value === squares[2][2].value && squares[0][0].value !==
'') {
return squares[0][0].value;
}

const checkForWinner = () => {
// Complete in Wave 3
// You will need to:
// 1. Go accross each row to see if
// 3 squares in the same row match
// i.e. same value
// 2. Go down each column to see if
// 3 squares in each column match
// 3. Go across each diagonal to see if
// all three squares have the same value.
if (squares[0][2].value === squares[1][1].value && squares[1][1].value === squares[2][0].value && squares[0][2].value !==
'') {
return squares[0][2].value;
}
return null
}

const winnerHelper = (array) => {

Choose a reason for hiding this comment

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

Nice helper function!

if (array.length === 3) {
if (array.every(v => v !== '')) {
if (array.every(v => v === array[0])){
return array[0];
}
}
}
return null
}



const resetGame = () => {
// Complete in Wave 4
setSquares(generateSquares());
setPlayer(PLAYER_1);
setWinner(null);
}

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>{winner === null ? `Current Player ${ player }` : `Winner is ${ winner }`}</h2>
<button onClick={resetGame}>Reset Game</button>
</header>
<main>
<Board squares={squares} />
<Board squares={squares} onClickCallback={updateSquare} />
</main>
</div>
);
Expand Down
6 changes: 3 additions & 3 deletions src/App.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('App', () => {
expect(buttons[buttonIndex].innerHTML).toEqual(expectedResult);
}

describe.skip('Wave 2: clicking on squares and rendering App', () => {
describe('Wave 2: clicking on squares and rendering App', () => {

test('App renders with a board of 9 empty buttons', () => {
// Arrange-Act - Render the app
Expand Down Expand Up @@ -85,7 +85,7 @@ describe('App', () => {
});


describe.skip('Wave 3: Winner tests', () => {
describe('Wave 3: Winner tests', () => {
describe('Prints "Winner is x" when x wins', () => {
test('that a winner will be identified when 3 Xs get in a row across the top', () => {
// Arrange
Expand Down Expand Up @@ -364,7 +364,7 @@ describe('App', () => {
});
});

describe.skip('Wave 4: reset game button', () => {
describe('Wave 4: reset game button', () => {
test('App has a "Reset Game" button', () => {
// Arrange-Act
render(<App />);
Expand Down
16 changes: 10 additions & 6 deletions src/components/Board.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@ import PropTypes from 'prop-types';


const generateSquareComponents = (squares, onClickCallback) => {
// Complete this for Wave 1
// squares is a 2D Array, but
// you need to return a 1D array
// of square components

const boardSquares = squares.flat()
return boardSquares.map((square) => {
return (
<Square
id={square.id}
value={square.value}
key={square.id}
onClickCallback={onClickCallback}
/>
)});
}

const Board = ({ squares, onClickCallback }) => {
const squareList = generateSquareComponents(squares, onClickCallback);
console.log(squareList);
return <div className="grid" >
{squareList}
</div>
Expand Down
94 changes: 47 additions & 47 deletions src/components/Board.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,54 +3,53 @@ import '@testing-library/jest-dom/extend-expect';
import Board from './Board';
import { render, screen, fireEvent} from '@testing-library/react'

// Sample input to the Board component
const SAMPLE_BOARD = [
[
{
value: 'X',
id: 0,
},
{
value: 'X',
id: 1,
},
{
value: 'O',
id: 2,
},
],
[
{
value: 'X',
id: 3,
},
{
value: 'X',
id: 4,
},
{
value: 'O',
id: 5,
},
],
[
{
value: 'O',
id: 6,
},
{
value: 'O',
id: 7,
},
{
value: 'X',
id: 8,
},
],
];

describe('Wave 1: Board', () => {
// Sample input to the Board component
const SAMPLE_BOARD = [
[
{
value: 'X',
id: 0,
},
{
value: 'X',
id: 1,
},
{
value: 'O',
id: 2,
},
],
[
{
value: 'X',
id: 3,
},
{
value: 'X',
id: 4,
},
{
value: 'O',
id: 5,
},
],
[
{
value: 'O',
id: 6,
},
{
value: 'O',
id: 7,
},
{
value: 'X',
id: 8,
},
],
];

test('that board will render with the proper number of Xs and Os', () => {
// Act
render(<Board squares={SAMPLE_BOARD} onClickCallback={() => { }} />);
Expand Down Expand Up @@ -81,7 +80,8 @@ describe('Wave 1: Board', () => {
const buttons = container.querySelectorAll('.grid button');
expect(buttons.length).toEqual(9);
});

});
describe('Wave 2: Board', () => {
describe('button click callbacks', () => {
test('that the callback is called for the 1st button', () => {
// Arrange
Expand Down
8 changes: 5 additions & 3 deletions src/components/Square.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import PropTypes from 'prop-types';
import './Square.css'

const Square = (props) => {
// For Wave 1 enable this
// Component to alert a parent
// component when it's clicked on.

const onButtonClick = () => {
props.onClickCallback(props.id);
}

return <button
className="square"
onClick={onButtonClick}
>
{props.value}
</button>
Expand Down
4 changes: 3 additions & 1 deletion src/components/Square.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ describe('Wave 1: Square', () => {

expect(button).toBeInTheDocument();
});
});

describe('Wave 2: Square', () => {
test('when clicked on it calls the callback function', async () => {
const callback = jest.fn();

Expand All @@ -33,4 +35,4 @@ describe('Wave 1: Square', () => {
fireEvent.click(button);
expect(callback).toHaveBeenCalled();
});
});
});
Loading