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

Cedar - Kit/Laurel #59

Open
wants to merge 5 commits into
base: main
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
120 changes: 97 additions & 23 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 @@ -26,40 +26,114 @@ 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());

// 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 [squares, setSquares] = useState(generateSquares());
const[winner, setWinner] = useState(null);
const [currentPlayer, setCurrentPlayer] = useState(PLAYER_1);
Comment on lines +30 to +32

Choose a reason for hiding this comment

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

Nice use of state!

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) {
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.

Consider using a for loop here instead.

// Check all the rows and columns for a winner
if (
squares[i][0].value === squares[i][1].value &&
squares[i][2].value === squares[i][1].value &&
squares[i][0].value !== ''
) {
return squares[i][0].value;
} else if (
squares[0][i].value === squares[1][i].value &&
squares[2][i].value === squares[1][i].value &&
squares[0][i].value !== ''
) {
return squares[0][i].value;
}
i += 1;
}
// Check Top-Left to bottom-right diagonal
if (
squares[0][0].value === squares[1][1].value &&
squares[2][2].value === squares[1][1].value &&
squares[1][1].value !== ''
) {
return squares[0][0].value;
}

// Check Top-right to bottom-left diagonal
if (
squares[0][2].value === squares[1][1].value &&
squares[2][0].value === squares[1][1].value &&
squares[1][1].value !== ''
) {
return squares[0][2].value;
}

return null;
};

const onClickCallback = (id) => {
setSquares((squares) => {
console.log(`squares in callback: ${squares}`)
let newBoard = squares.map((square)=>{
for (let property of square){
if (property.id === id && property.value === ''){
if (currentPlayer === PLAYER_1){
property.value = PLAYER_1;

} else if (currentPlayer === PLAYER_2){
property.value = PLAYER_2;
}
}
}
return square;
});
Comment on lines +76 to +88

Choose a reason for hiding this comment

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

Nice map!


//CHECK FOR WINNER HERE
setWinner(checkForWinner());

return newBoard;
});
//CHANGE PLAYER
if (currentPlayer === PLAYER_1) {
setCurrentPlayer(PLAYER_2);
}else {
setCurrentPlayer(PLAYER_1)
}

};



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


let header;
let boardCallback;
// let finalWinner;
if (winner != null) {
// if (winner === PLAYER_1) {
// finalWinner = 'Player 1';
// } else {
// finalWinner = 'Player 2';
// }
header = <h2>Winner is {winner}</h2>;
} else {
header = <h2>The Current Player is {currentPlayer}</h2>;
boardCallback = onClickCallback;

Choose a reason for hiding this comment

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

Because this callback is only set if there's not a winner weird bugs can arise. For example, if someone wins the game, and then a square is clicked again, your whole React app will crash (not even the reset button will work anymore). Consider ways you can resolve this by always passing a callback but having it do different things if the game is won or not.

}

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>
{header}

Choose a reason for hiding this comment

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

Nice use of state!

<button onClick = {resetGame} >Reset Game</button>
</header>
<main>
<Board squares={squares} />
<Board squares={squares} onClickCallback={boardCallback}/>
</main>
</div>
);
Expand Down
4 changes: 2 additions & 2 deletions src/App.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ describe('App', () => {
const clickButtonAndVerifyResult = (container, buttonIndex, expectedResult) => {
let buttons = container.querySelectorAll('.grid button');
fireEvent.click(buttons[buttonIndex]);

buttons = container.querySelectorAll('.grid button');
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
24 changes: 15 additions & 9 deletions src/components/Board.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,26 @@ 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 singleSquares = [].concat(...squares);

Choose a reason for hiding this comment

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

This can be done slightly simpler with const singleSquares = [...squares];

return (
singleSquares.map((square) => {
return (
<Square value={square.value}
id={square.id}
onClickCallback={onClickCallback}
key={square.id}

Choose a reason for hiding this comment

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

Good job remembering to use key!

/>
);
}));
};

const Board = ({ squares, onClickCallback }) => {
const squareList = generateSquareComponents(squares, onClickCallback);
console.log(squareList);
return <div className="grid" >
{squareList}
</div>
}
</div>;
};

Board.propTypes = {
squares: PropTypes.arrayOf(
Expand Down
4 changes: 2 additions & 2 deletions src/components/Board.test.js
Original file line number Diff line number Diff line change
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
13 changes: 7 additions & 6 deletions src/components/Square.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ 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 hardestButtonToButton = () => {

Choose a reason for hiding this comment

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

Fun function name 😛

props.onClickCallback(props.id)
};
return <button
className="square"
onClick ={hardestButtonToButton}
>
{props.value}
</button>
}
</button>;
};

Square.propTypes = {
value: PropTypes.string.isRequired,
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
Loading