Qianling - high-card-project#65
Conversation
| if (cardDeck.length > 0) { | ||
| if (p1Card.rank > p2Card.rank) { | ||
| this.setState((prevState) => ({ | ||
| currWinner: 1, | ||
| player1Score: prevState.player1Score + 1, | ||
| })); | ||
| } else if (p1Card.rank < p2Card.rank) { | ||
| this.setState((prevState) => ({ | ||
| currWinner: 2, | ||
| player2Score: prevState.player2Score + 1, | ||
| })); | ||
| } | ||
| } else if (cardDeck.length === 0) { | ||
| this.setState( | ||
| { | ||
| hasGameBegin: false, | ||
| }, | ||
| () => this.determineFinalWinner() | ||
| ); | ||
| } |
There was a problem hiding this comment.
I think something for the future would be to rewrite if/else spaghetti whenever we can. I like to return early if possible. Here maybe a suggestion, though bit tricky here.
| if (cardDeck.length > 0) { | |
| if (p1Card.rank > p2Card.rank) { | |
| this.setState((prevState) => ({ | |
| currWinner: 1, | |
| player1Score: prevState.player1Score + 1, | |
| })); | |
| } else if (p1Card.rank < p2Card.rank) { | |
| this.setState((prevState) => ({ | |
| currWinner: 2, | |
| player2Score: prevState.player2Score + 1, | |
| })); | |
| } | |
| } else if (cardDeck.length === 0) { | |
| this.setState( | |
| { | |
| hasGameBegin: false, | |
| }, | |
| () => this.determineFinalWinner() | |
| ); | |
| } | |
| if (!card.length) { | |
| return this.setState( | |
| { | |
| hasGameBegin: false, | |
| }, | |
| () => this.determineFinalWinner() | |
| ); | |
| } | |
| if (p1Card.rank > p2Card.rank) { | |
| this.setState((prevState) => ({ | |
| currWinner: 1, | |
| player1Score: prevState.player1Score + 1, | |
| })); | |
| } else if (p1Card.rank < p2Card.rank) { | |
| this.setState((prevState) => ({ | |
| currWinner: 2, | |
| player2Score: prevState.player2Score + 1, | |
| })); | |
| } |
In cases where we wouldn't have to worry about the tie situation, we probably could simplify this even further.
| let winnerPerMatch; | ||
| if (player1Score > player2Score) { | ||
| winnerPerMatch = ( | ||
| <> | ||
| <p> | ||
| Game over! <br /> | ||
| Player 1 won this match. Would you like to continue? | ||
| </p> | ||
| </> | ||
| ); | ||
| } else if (player1Score < player2Score) { | ||
| winnerPerMatch = ( | ||
| <> | ||
| <p> | ||
| Game over! | ||
| <br /> Player 2 won this match. Would you like to continue? | ||
| </p> | ||
| </> | ||
| ); | ||
| } else { | ||
| winnerPerMatch = ( | ||
| <> | ||
| <p> | ||
| Game over! <br /> | ||
| Both tie. Would you like to continue? | ||
| </p> | ||
| </> | ||
| ); |
There was a problem hiding this comment.
I think we could make this a component with a winner prop
| const dealCardsButton = ( | ||
| <> | ||
| <Button variant="contained" color="secondary" onClick={this.dealCards}> | ||
| Deal | ||
| </Button> | ||
| </> | ||
| ); |
| const continueGameButton = ( | ||
| <> | ||
| <Button | ||
| variant="contained" | ||
| color="success" | ||
| onClick={this.startNewMatch} | ||
| > | ||
| Yes, Enter Game | ||
| </Button> | ||
| </> | ||
| ); | ||
|
|
||
| const exitGameButton = ( | ||
| <> | ||
| <Button variant="contained" color="error" onClick={this.restartGame}> | ||
| No, Exit game | ||
| </Button> | ||
| </> | ||
| ); |
There was a problem hiding this comment.
Component, and component :) Since we got a few button components, maybe we can make a generic button component?
Maybe used like so:
<Button variant={} color={} onClick={} text={} />
| {currCardElems} | ||
| </Box> | ||
|
|
||
| {this.state.hasGameBegin === true && ( |
There was a problem hiding this comment.
| {this.state.hasGameBegin === true && ( | |
| {this.state.hasGameBegin && ( |
boolean can be used with truthy/falsy assumption
|
|
||
| {this.state.cardDeck.length > 0 && dealCardsButton} | ||
| <p>{this.state.hasGameBegin && winnerPerRound}</p> | ||
| <p>{this.state.cardDeck.length === 0 && winnerPerMatch}</p> |
There was a problem hiding this comment.
| <p>{this.state.cardDeck.length === 0 && winnerPerMatch}</p> | |
| <p>{!this.state.cardDeck.length && winnerPerMatch}</p> |
0 is falsy in JavaScript
| </Box> | ||
| )} | ||
|
|
||
| {this.state.cardDeck.length > 0 && dealCardsButton} |
There was a problem hiding this comment.
| {this.state.cardDeck.length > 0 && dealCardsButton} | |
| {this.state.cardDeck.length && dealCardsButton} |
Above 0 is truthy in JS
| alignItems: "center", | ||
| }} | ||
| > | ||
| {this.state.cardDeck.length === 0 && continueGameButton} |
There was a problem hiding this comment.
| {this.state.cardDeck.length === 0 && continueGameButton} | |
| {!this.state.cardDeck.length&& continueGameButton} |
| }} | ||
| > | ||
| {this.state.cardDeck.length === 0 && continueGameButton} | ||
| {this.state.cardDeck.length === 0 && exitGameButton} |
There was a problem hiding this comment.
You can summarize similar state lookups into a single one:
| {this.state.cardDeck.length === 0 && exitGameButton} | |
| {!this.state.cardDeck.length && ( | |
| <> | |
| <Button ...continue game button/> | |
| <Button ...exit game button/> | |
| </> | |
| )} |
No description provided.