Skip to content

Alf - Spruce #71

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

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open

Alf - Spruce #71

wants to merge 21 commits into from

Conversation

asliathman
Copy link

No description provided.

spitsfire and others added 21 commits December 13, 2021 11:20
…t the current player, and 'updateSquares' that updates the game state to mark the squares the have been clicked by the respective players
…ked on square to the onClickCallback function in app
…1 instead of col +=1, so it was looping forever git add .! dumb dummy
…e, and stop the clicks on the board if a winner is found
…sage depending on if the game is in progress, or if someone wins
Copy link

@audreyandoy audreyandoy left a comment

Choose a reason for hiding this comment

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

Great work Alf! You met all the learnings goals for this project, passed most of the tests, and I loved the CSS additions to your project!

The only tests you were failing had to do with a minor string mismatch using uppercase 'X' and 'O' rather than lowercase 'x' and 'o''s like the tests were expecting. No biggie and an easy fix.

In your PR, you'll find comments containing compliments and ways to refactor.

Project Grade: 🟢

Comment on lines +6 to +7
const player1 = 'X';
const player2 = 'O';

Choose a reason for hiding this comment

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

Annoyingly enough, our tests look for lowercase 'x' and 'o', which was our fault in not updating our tests.

This was the reason why it appeared you failed tests, despite your project working as expected.

// Then pass it into the squares as a callback
const [isX, setIsX] = useState(false);

const currentPlayer = !isX ? player1 : player2;

Choose a reason for hiding this comment

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

Nice ternary!

Comment on lines +38 to +53
let newSquares = [];

for (let row = 0; row < 3; row += 1) {
newSquares.push([]);
for (let col = 0; col < 3; col += 1) {
if (id === squares[row][col].id) {
if (!squares[row][col].value) {
squares[row][col].value = currentPlayer;
setIsX(!isX);
}
}
newSquares[row].push(squares[row][col]);
}
}
setSquares(newSquares);
};

Choose a reason for hiding this comment

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

Great job in making a copy of the squares data! Here's a reminder on why we need to make the copy:

To trigger the update stage for this component, React needs to notice a change in state data. We could mutate state directly, however, this doesn't scale well and doesn't follow React best practices.

Instead, we should pass in a copy of our state data to the setState function setSquares() which will have React compare the object references between the copy and the old state data. If the object references are different, React will then trigger the update stage.

Choose a reason for hiding this comment

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

Rather than creating an empty list and pushing values into it with every iteration, we can make a shallow copy with the ... spread operator:

Suggested change
let newSquares = [];
for (let row = 0; row < 3; row += 1) {
newSquares.push([]);
for (let col = 0; col < 3; col += 1) {
if (id === squares[row][col].id) {
if (!squares[row][col].value) {
squares[row][col].value = currentPlayer;
setIsX(!isX);
}
}
newSquares[row].push(squares[row][col]);
}
}
setSquares(newSquares);
};
let newSquares = [...squares];
for (let row = 0; row < 3; row += 1) {
for (let col = 0; col < 3; col += 1) {
if (id === squares[row][col].id && !squares[row][col].value) {
squares[row][col].value = currentPlayer;
setIsX(!isX);
}
}
}
setSquares(newSquares);
};

Choose a reason for hiding this comment

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

I've also combined the conditional to reduce how nested this section of code looks.

};

const headerMessage = checkForWinner() ? `The winner is ${checkForWinner()}!!!` : `Current player: ${currentPlayer}`;

Choose a reason for hiding this comment

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

Great ternary!

</header>
<main>
<Board squares={squares} />
<Board squares={squares} onClickCallback={checkForWinner() ? () => {} : updateSquare} />

Choose a reason for hiding this comment

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

Clever use of a ternary operator to change the functionality of the click.

@@ -1,12 +1,33 @@
.square
{
border: 4px solid #2c3e50;
border: 3px solid black;

Choose a reason for hiding this comment

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

LOVE the css hover effects! The overall color palette is super cute and portfolio ready ✨

Comment on lines +9 to +19
if (props.id === 0 || props.id === 5) {
color = 'blue';
} else if (props.id === 1 || props.id === 6) {
color = 'yellow';
} else if (props.id === 2 || props.id === 7) {
color = 'green';
} else if (props.id === 3 || props.id === 8) {
color = 'pink';
} else if (props.id === 4) {
color = 'orange';
}

Choose a reason for hiding this comment

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

Great use of the || or operator to determine the color of the hover effects!

/>
);
});
};

const Board = ({ squares, onClickCallback }) => {
const squareList = generateSquareComponents(squares, onClickCallback);
console.log(squareList);

Choose a reason for hiding this comment

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

Generally, we should remove all console logs and print statements when submitting code for review. And our team should definitely take that advice as well, I believe this was meant to be removed before shipping to y'all.

Suggested change
console.log(squareList);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants