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

Large diffs are not rendered by default.

67 changes: 46 additions & 21 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ import './App.css';

import Board from './components/Board';

const PLAYER_1 = 'X';
const PLAYER_2 = 'O';
const PLAYER_1 = 'x';
const PLAYER_2 = 'o';
let count = 0
let winner = ''

const generateSquares = () => {
const squares = [];

let currentId = 0;

for (let row = 0; row < 3; row += 1) {
Expand All @@ -21,48 +22,72 @@ const generateSquares = () => {
currentId += 1;
}
}

return squares;
}

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);

// 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 = (updatedSquare) => {
const newSquares = [...squares]

for(let row of newSquares) {
for(let square of row) {
if (square.id === updatedSquare.id && square.value === '') {
count++
square.value = player;
}
}
};

setPlayer(player === PLAYER_1 ? PLAYER_2 : PLAYER_1)
setSquares(newSquares);

if (count > 4) {
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.

const winPossibilities = [[0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7], [2, 5, 8], [0, 4, 8], [2, 4, 6]]

Choose a reason for hiding this comment

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

This is a clever way to store the winning combinations. You could also consider how you could write loops to check for 3 across in rows and columns in the 2D array.

let allSquares = squares.flat()

winPossibilities.forEach((possibility) => {
let rowOfThree = []
possibility.forEach((i) => {
rowOfThree.push(allSquares[i])
})
if (rowOfThree.every(obj => obj.value === 'x')) {
winner = PLAYER_1
} else if (rowOfThree.every(obj => obj.value === 'o')) {
winner = PLAYER_2
} else if (allSquares.every(obj => obj.value !== '')) {
winner = 'tie'
}
})
return winner;
}

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

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 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
14 changes: 14 additions & 0 deletions src/components/Board.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,22 @@ const generateSquareComponents = (squares, onClickCallback) => {
// squares is a 2D Array, but
// you need to return a 1D array
// of square components
const oneDArray = [];
for(let row of squares) {
for(let square of row) {
oneDArray.push(
<Square
key={square.id}
id={square.id}
value={square.value}
onClickCallback={onClickCallback}
/>)
}
}

return oneDArray;
}


const Board = ({ squares, onClickCallback }) => {
const squareList = generateSquareComponents(squares, onClickCallback);
Expand Down
8 changes: 8 additions & 0 deletions src/components/Square.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,16 @@ const Square = (props) => {
// Component to alert a parent
// component when it's clicked on.

const onPlayClick = () => {
const updatedSquare = {
id: props.id,
}
props.onClickCallback(updatedSquare);
}

return <button
className="square"
onClick={onPlayClick}
>
{props.value}
</button>
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as serviceWorker from './serviceWorker';

ReactDOM.render(
<React.StrictMode>
<App />
<App key = {1}/>
</React.StrictMode>,
document.getElementById('root')
);
Expand Down
Loading