Skip to content

High card#63

Open
Lili00ani wants to merge 6 commits into
rocketacademy:mainfrom
Lili00ani:main
Open

High card#63
Lili00ani wants to merge 6 commits into
rocketacademy:mainfrom
Lili00ani:main

Conversation

@Lili00ani

Copy link
Copy Markdown

The commit about "updated bug - the winner was calculated using round 25 instead of round 26", I am still not 100% clear why it was behaving that way even though I managed to fix them.

I tried to console log this.state.player1Score in the checkWinner() and it is not showing the latest player1Score but it is showing the previous round player1Score. From my understanding, this.state.player1Score should have shown the latest updated number as whenever card is dealt it will trigger an update to the playerScore.

@Flashrob

Copy link
Copy Markdown

The commit about "updated bug - the winner was calculated using round 25 instead of round 26", I am still not 100% clear why it was behaving that way even though I managed to fix them.

I tried to console log this.state.player1Score in the checkWinner() and it is not showing the latest player1Score but it is showing the previous round player1Score. From my understanding, this.state.player1Score should have shown the latest updated number as whenever card is dealt it will trigger an update to the playerScore.

For this, we might want to jump on a call. That way I can fully understand what you mean here as you walk me through the code and behaviour.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Seems like you got all images twice in this repo. Once in this folder, once in the src/PNG folder.

Comment thread src/a.js

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

empty file? Can delete :)

Comment thread src/App copy.js
@@ -0,0 +1,184 @@
import React from "react";

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

this App copy file makes it confusing for me to review. Do I review the copy or the original? I will review the original only. Going forward I suggest removing such copy files before submitting code for review :)!

Comment thread src/App.js
<div key={`${name}${suit}`}>
{name} of {suit}
</div>
if (this.state.currCards === null) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Since the default state value for currCards is an empty array, I wouldn't assume that null is a possible value. If you reset the value, I would always use an empty array for that instead of null for example.

Comment thread src/App.js
Comment on lines +63 to +96
temp = 0;
} else if (this.state.currCards[1].rank > this.state.currCards[0].rank) {
temp = 1;
} else {
temp = 99;
}

//trigger function to count the total score
this.setState(
{
indexWinner: temp,
},
() => {
this.countScore();
}
);
};

countScore = () => {
//counting score for each player
const tempCounter = [0, 0];
//if indexWinner is 0, add +1 to count index 0, vice versa
if (this.state.indexWinner === 0) {
tempCounter[0]++;
} else if (this.state.indexWinner === 1) {
tempCounter[1]++;
}
//update all the scores
this.setState((prevState) => ({
player1Score: prevState.player1Score + tempCounter[0],
player2Score: prevState.player2Score + tempCounter[1],
overallPlayer1Score: prevState.overallPlayer1Score + tempCounter[0],
overallPlayer2Score: prevState.overallPlayer2Score + tempCounter[1],
}));

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This whole section seems a bit overengineered to me :)

You probably could do a simple:

Suggested change
temp = 0;
} else if (this.state.currCards[1].rank > this.state.currCards[0].rank) {
temp = 1;
} else {
temp = 99;
}
//trigger function to count the total score
this.setState(
{
indexWinner: temp,
},
() => {
this.countScore();
}
);
};
countScore = () => {
//counting score for each player
const tempCounter = [0, 0];
//if indexWinner is 0, add +1 to count index 0, vice versa
if (this.state.indexWinner === 0) {
tempCounter[0]++;
} else if (this.state.indexWinner === 1) {
tempCounter[1]++;
}
//update all the scores
this.setState((prevState) => ({
player1Score: prevState.player1Score + tempCounter[0],
player2Score: prevState.player2Score + tempCounter[1],
overallPlayer1Score: prevState.overallPlayer1Score + tempCounter[0],
overallPlayer2Score: prevState.overallPlayer2Score + tempCounter[1],
}));
else if (this.state.currCards[0].rank > this.state.currCards[1].rank) {
this.setState((prevState) => ({
player1Score: prevState.player1Score + 1,
overallPlayer1Score: prevState.overallPlayer1Score + 1,
}));
} else if (this.state.currCards[1].rank > this.state.currCards[0].rank) {
this.setState((prevState) => ({
player2Score: prevState.player2Score + 1,
overallPlayer2Score: prevState.overallPlayer2Score + 1,
}));
} else {
<do some tie thing>
}

Comment thread src/App.js
Comment on lines +123 to +127
<img
src={require(`./PNG/${name.toLowerCase()}_of_${suit.toLowerCase()}.png`)}
alt={`${name} of ${suit}`}
style={{ height: "200px" }}
/>

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

You could potentially make this an image component with name, suit, height and width props.

Comment thread src/App.js
Comment on lines +134 to +138
this.state.indexWinner === -1
? ""
: this.state.indexWinner === 99
? `Tie`
: `Player ${this.state.indexWinner + 1} won!`;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Please don't use nested ternary operators. This is very hard to read haha :D
I suggest multiple if statements, a function or possible a switch statement with the last else as default value.

Comment thread src/App.js
<p>Overall 🏆{this.state.overallPlayer2Score}</p>
</div>
</div>
{console.log("Player 1 Score:", this.state.player1Score)};

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Always clean up console logs in your code before submitting for review or pushing code to production

Comment thread src/App.js
</div>

<h2 className="text-center mt-2">
{numRoundsLeft > 0 && winnerMessage}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

If winnermessage is the empty string as above, you render the h2 unnecessarily. I would do it like so:

Suggested change
{numRoundsLeft > 0 && winnerMessage}
{numRoundsLeft > 0 && winnerMessage &&
(<h2 className="text-center mt-2">{winnerMessage}</h2>)
}

Comment thread src/App.js
</h2>

<h1 className="text-center mt-2">
{numRoundsLeft === 0 && gameWinnerMessage}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Same here. Plus we can improve the variable naming here. What is the difference between winner and gameWinner? I mean it can be clear, but also not

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.

2 participants