Lee Zhao Yi Charles - High Card PR#64
Conversation
| // 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; |
There was a problem hiding this comment.
Please note that if the score is the same, the result will always be 2. So your second player has an advantage here :)
| player1NumRoundsWon: | ||
| newRoundWinner === 1 | ||
| ? state.player1NumRoundsWon + 1 | ||
| : state.player1NumRoundsWon, |
There was a problem hiding this comment.
You can use an optional object property here.
| player1NumRoundsWon: | |
| newRoundWinner === 1 | |
| ? state.player1NumRoundsWon + 1 | |
| : state.player1NumRoundsWon, | |
| ...(newRoundWinner === 1 && { player1NumRoundsWon: state.player1NumRoundsWon + 1 }) |
| ? `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.`; |
There was a problem hiding this comment.
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
| 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!"; | ||
| } |
There was a problem hiding this comment.
Why not combine it into a single statement?
| 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!"; | |
| } |
| let dealButtonText; | ||
| if (numRoundsLeft === 0) { | ||
| dealButtonText = "Reset Game"; | ||
| } else { | ||
| dealButtonText = "Deal"; | ||
| } |
There was a problem hiding this comment.
| let dealButtonText; | |
| if (numRoundsLeft === 0) { | |
| dealButtonText = "Reset Game"; | |
| } else { | |
| dealButtonText = "Deal"; | |
| } | |
| const dealButtonText = !numRoundsLeft ? "Reset Game" : "Deal" |
| <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} |
There was a problem hiding this comment.
| onClick={numRoundsLeft === 0 ? this.resetGame : this.dealCards} | |
| onClick={!numRoundsLeft ? this.resetGame : this.dealCards} |
0 is falsy in JavaScript
| {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> |
There was a problem hiding this comment.
| <p>{numRoundsLeft === 0 && gameWinnerMessage}</p> | |
| <p>{!numRoundsLeft && gameWinnerMessage}</p> |
| {this.state.hasGameStarted && <p>{roundWinnerMessage}</p>} | ||
| {this.state.hasGameStarted && <p>{player1RoundsWonMessage}</p>} | ||
| {this.state.hasGameStarted && <p>{player2RoundsWonMessage}</p>} | ||
| {this.state.hasGameStarted && <p>{numRoundsLeftMessage}</p>} |
There was a problem hiding this comment.
| {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 :)
No description provided.