Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Paper - Heidi Zhang #53

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
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
80 changes: 56 additions & 24 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import React, { useState } from 'react';
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 @@ -26,43 +25,76 @@ 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 [currentPlayer, setCurrentPlayer] = useState('x');
const [winner, setWinner] = useState(null)
let newBoard = [...squares]

// 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 = (squareToUpdate) => {
const row = Math.floor(squareToUpdate.id/3)
const colume = squareToUpdate.id%3
Comment on lines +34 to +35

Choose a reason for hiding this comment

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

This is a really smart way to find the indexes!


if (squareToUpdate.value === '') {
squareToUpdate.value = currentPlayer
newBoard[row][colume] = squareToUpdate
if (currentPlayer === 'x') {

Choose a reason for hiding this comment

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

This would be clearer if you used the constants:

Suggested change
if (currentPlayer === 'x') {
if (currentPlayer === PLAYER_1) {

setCurrentPlayer(PLAYER_2);
} else {
setCurrentPlayer(PLAYER_1)
};
setSquares(newBoard)
}
setWinner(checkForWinner())
}

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.
let i = 0;

while (i < 3) {
if (newBoard[i][0].value === newBoard[i][1].value &&
newBoard[i][2].value === newBoard[i][1].value &&
newBoard[i][0].value !== '') {
return newBoard[i][0].value;
} else if (newBoard[0][i].value === newBoard[1][i].value &&
newBoard[2][i].value === newBoard[1][i].value &&
newBoard[0][i].value !== '') {
return newBoard[0][i].value;
}
i += 1;
}
if (newBoard[0][0].value === newBoard[1][1].value &&
newBoard[2][2].value === newBoard[1][1].value &&
newBoard[1][1].value !== '') {
return newBoard[0][0].value;
}

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

return null;
}

const resetGame = () => {
// Complete in Wave 4
setSquares(generateSquares())
setCurrentPlayer('x')
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 is ${currentPlayer}` : `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
25 changes: 18 additions & 7 deletions src/components/Board.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,29 @@ import './Board.css';
import Square from './Square';
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
let onedSquares = [];

for (const row of squares) {
for (const square of row) {
onedSquares.push(square)
}
}

return onedSquares.map((square) => {
return (
<Square
value={ square.value }
id={ square.id }
onClickCallback={ onClickCallback }
/>
)
})
Comment on lines +7 to +23

Choose a reason for hiding this comment

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

You can use a few built in methods to clean this up.

You can use flat() instead of your first loop:

Suggested change
let onedSquares = [];
for (const row of squares) {
for (const square of row) {
onedSquares.push(square)
}
}
return onedSquares.map((square) => {
return (
<Square
value={ square.value }
id={ square.id }
onClickCallback={ onClickCallback }
/>
)
})
let onedSquares = squares.flat();
return onedSquares.map((square) => {
return (
<Square
value={ square.value }
id={ square.id }
onClickCallback={ onClickCallback }
/>
)
})

And then JS actually includes the built in method flatMap that does a flat followed by a map all in one step:

Suggested change
let onedSquares = [];
for (const row of squares) {
for (const square of row) {
onedSquares.push(square)
}
}
return onedSquares.map((square) => {
return (
<Square
value={ square.value }
id={ square.id }
onClickCallback={ onClickCallback }
/>
)
})
return squares.flatMap((square) => {
return (
<Square
value={ square.value }
id={ square.id }
onClickCallback={ onClickCallback }
/>
)
})


}

const Board = ({ squares, onClickCallback }) => {
const squareList = generateSquareComponents(squares, onClickCallback);
console.log(squareList);
return <div className="grid" >
{squareList}
</div>
Expand All @@ -25,7 +36,7 @@ Board.propTypes = {
PropTypes.arrayOf(
PropTypes.shape({
id: PropTypes.number.isRequired,
value: PropTypes.string.isRequired
value: PropTypes.string.isRequired,
})
)
),
Expand Down
96 changes: 48 additions & 48 deletions src/components/Board.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,54 +3,54 @@ 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 @@ -84,7 +84,7 @@ describe('Wave 1: Board', () => {
});
describe('Wave 2: Board', () => {
describe('button click callbacks', () => {
test.skip('that the callback is called for the 1st button', () => {
test('that the callback is called for the 1st button', () => {
// Arrange
const callback = jest.fn();
const { container } = render(<Board squares={SAMPLE_BOARD} onClickCallback={callback} />);
Expand All @@ -97,7 +97,7 @@ describe('Wave 2: Board', () => {
expect(callback).toHaveBeenCalled();
});

test.skip('that the callback is called for the last button', () => {
test('that the callback is called for the last button', () => {
// Arrange
const callback = jest.fn();
const { container } = render(<Board squares={SAMPLE_BOARD} onClickCallback={callback} />);
Expand Down
22 changes: 12 additions & 10 deletions src/components/Square.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import React from 'react';
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 Square = ({value, id, onClickCallback}) => {
const markSquare = () => {
return onClickCallback({
id: id,
value: value
})
}

return <button
className="square"
>
{props.value}
</button>
return (
<button onClick={markSquare} className="square">
{value}
</button>
)
}

Square.propTypes = {
Expand Down
2 changes: 1 addition & 1 deletion src/components/Square.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('Wave 1: Square', () => {
});

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

render(<Square value="X" id={1} onClickCallback={callback} />);
Expand Down