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

Pine: Kayla H. & Mac P. #58

Open
wants to merge 12 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@ yarn-error.log*

.vscode

# so I can git pull
yarn.lock
112 changes: 90 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 @@ -21,45 +21,113 @@ 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);
const [winner, setWinner] = useState('...');

// 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 onSquareClickCallback = (id) => {
const boardCopy = squares.map((row) => {
row.forEach((column) => {
if (column.id === id) {
if (column.value === '') {
if (player === PLAYER_1) {
column.value = PLAYER_1;
} else {
column.value = PLAYER_2;
}
}
}
});
return row;
Comment on lines +33 to +45

Choose a reason for hiding this comment

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

Nice use of map!

});
setSquares(boardCopy);
if (player === PLAYER_1) {
setPlayer(PLAYER_2);
} else {
setPlayer(PLAYER_1);
}
Comment on lines +47 to +52

Choose a reason for hiding this comment

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

Good job updating state.


checkForWinner();
};

const checkForWinner = () => {

Choose a reason for hiding this comment

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

Consider additional logic that prevents the game from continuing once someone has won.

// 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 r = 0;
let c = 0;
let winner = '...';

Choose a reason for hiding this comment

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

Consider having this be a top-level const like PLAYER_1 or PLAYER_2.


// checking for descending diagnol winner
if (
squares[r][c].value === squares[r + 1][c + 1].value &&
squares[r + 1][c + 1].value === squares[r + 2][c + 2].value &&
squares[r][c].value != ''
) {
winner = squares[r][c].value;
setWinner(winner);
}

// checking for ascending diagnol winner
if (
squares[r][c + 2].value === squares[r + 1][c + 1].value &&
squares[r + 1][c + 1].value === squares[r + 2][c].value &&
squares[r][c].value != ''

Choose a reason for hiding this comment

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

Make sure to use strict inequality !== instead of normal inequality !=.

) {
winner = squares[r][c + 2].value;
setWinner(winner);
}

// checking for horizontal winner
while (r < 3) {
if (
squares[r][c].value === squares[r][c + 1].value &&
squares[r][c + 1].value === squares[r][c + 2].value &&
squares[r][c].value != ''
) {
winner = squares[r][c].value;
setWinner(winner);
return winner;
Comment on lines +90 to +91

Choose a reason for hiding this comment

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

Unclear why some winning lines return the winner and others do not.

}
r += 1;
}
// checking for vertical winner
while (c < 3) {
r = 0;
if (
squares[r][c].value === squares[r + 1][c].value &&
squares[r + 1][c].value === squares[r + 2][c].value &&
squares[r][c].value != ''
) {
winner = squares[r][c].value;
setWinner(winner);
return winner;
}
c += 1;
}
};

const resetGame = () => {
// Complete in Wave 4
const boardCopy = squares.map((row) => {
row.forEach((column) => {
column.value = '';
});
return row;
});
Comment on lines +112 to +117

Choose a reason for hiding this comment

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

Nice!

setSquares(boardCopy);
setWinner('...');
};

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={onSquareClickCallback} />

Choose a reason for hiding this comment

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

Good job passing callback!

</main>
</div>
);
Expand Down
Loading