Skip to content

Lee Zhao Yi Charles - High Card PR#64

Open
charlesleezhaoyi wants to merge 3 commits into
rocketacademy:mainfrom
charlesleezhaoyi:main
Open

Lee Zhao Yi Charles - High Card PR#64
charlesleezhaoyi wants to merge 3 commits into
rocketacademy:mainfrom
charlesleezhaoyi:main

Conversation

@charlesleezhaoyi
Copy link
Copy Markdown

No description provided.

Comment thread src/App.js
// Deal last 2 cards to currCards
const newCurrCards = this.state.cardDeck.slice(-2);
// Determine round winner based on card rank
const newRoundWinner = newCurrCards[0].rank > newCurrCards[1].rank ? 1 : 2;
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 note that if the score is the same, the result will always be 2. So your second player has an advantage here :)

Comment thread src/App.js
Comment on lines +47 to +50
player1NumRoundsWon:
newRoundWinner === 1
? state.player1NumRoundsWon + 1
: state.player1NumRoundsWon,
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 can use an optional object property here.

Suggested change
player1NumRoundsWon:
newRoundWinner === 1
? state.player1NumRoundsWon + 1
: state.player1NumRoundsWon,
...(newRoundWinner === 1 && { player1NumRoundsWon: state.player1NumRoundsWon + 1 })

Comment thread src/App.js
? `Player ${this.state.roundWinner} won this round.`
: `This rounds is a tie!`;
//Placeholder text when player 1 wins a round
const player1RoundsWonMessage = `Player 1 has won ${this.state.player1NumRoundsWon} rounds this game.`;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I think we don't need to store all these strings in their own variables. We can just place them as is into the html

Comment thread src/App.js
Comment on lines +78 to +92
let gameWinner = null;
if (this.state.player1NumRoundsWon > this.state.player2NumRoundsWon) {
gameWinner = 1;
} else if (
this.state.player2NumRoundsWon > this.state.player1NumRoundsWon
) {
gameWinner = 2;
}

let gameWinnerMessage;
if (gameWinner) {
gameWinnerMessage = `Player ${gameWinner} won this game!`;
} else {
gameWinnerMessage = "It's a draw!";
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Why not combine it into a single statement?

Suggested change
let gameWinner = null;
if (this.state.player1NumRoundsWon > this.state.player2NumRoundsWon) {
gameWinner = 1;
} else if (
this.state.player2NumRoundsWon > this.state.player1NumRoundsWon
) {
gameWinner = 2;
}
let gameWinnerMessage;
if (gameWinner) {
gameWinnerMessage = `Player ${gameWinner} won this game!`;
} else {
gameWinnerMessage = "It's a draw!";
}
let gameWinnerMessage;
if (this.state.player1NumRoundsWon > this.state.player2NumRoundsWon) {
gameWinnerMessage = `Player 1 won this game!`;
} else if (
this.state.player2NumRoundsWon > this.state.player1NumRoundsWon
) {
gameWinnerMessage = `Player 2 won this game!`;
} else {
gameWinnerMessage = "It's a draw!";
}

Comment thread src/App.js
Comment on lines +95 to +100
let dealButtonText;
if (numRoundsLeft === 0) {
dealButtonText = "Reset Game";
} else {
dealButtonText = "Deal";
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Suggested change
let dealButtonText;
if (numRoundsLeft === 0) {
dealButtonText = "Reset Game";
} else {
dealButtonText = "Deal";
}
const dealButtonText = !numRoundsLeft ? "Reset Game" : "Deal"

Comment thread src/App.js
<button onClick={this.dealCards}>Deal</button>
{/* Button changes functionality depending on game state. When number of rounds = 0, button prompts game reset, else deal cards. */}
<button
onClick={numRoundsLeft === 0 ? this.resetGame : this.dealCards}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Suggested change
onClick={numRoundsLeft === 0 ? this.resetGame : this.dealCards}
onClick={!numRoundsLeft ? this.resetGame : this.dealCards}

0 is falsy in JavaScript

Comment thread src/App.js
{this.state.hasGameStarted && <p>{player2RoundsWonMessage}</p>}
{this.state.hasGameStarted && <p>{numRoundsLeftMessage}</p>}
{/* Render winner message if the game is over */}
<p>{numRoundsLeft === 0 && gameWinnerMessage}</p>
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Suggested change
<p>{numRoundsLeft === 0 && gameWinnerMessage}</p>
<p>{!numRoundsLeft && gameWinnerMessage}</p>

Comment thread src/App.js
Comment on lines +117 to +120
{this.state.hasGameStarted && <p>{roundWinnerMessage}</p>}
{this.state.hasGameStarted && <p>{player1RoundsWonMessage}</p>}
{this.state.hasGameStarted && <p>{player2RoundsWonMessage}</p>}
{this.state.hasGameStarted && <p>{numRoundsLeftMessage}</p>}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Suggested change
{this.state.hasGameStarted && <p>{roundWinnerMessage}</p>}
{this.state.hasGameStarted && <p>{player1RoundsWonMessage}</p>}
{this.state.hasGameStarted && <p>{player2RoundsWonMessage}</p>}
{this.state.hasGameStarted && <p>{numRoundsLeftMessage}</p>}
{this.state.hasGameStarted && (
<>
<p>{roundWinnerMessage}</p>
<p>{player1RoundsWonMessage}</p>
<p>{player2RoundsWonMessage}</p>
<p>{numRoundsLeftMessage}</p>
</>
)

We don't need to access the same state property multiple times. It won't change value :)

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