Skip to content

Qianling - high-card-project#65

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

Qianling - high-card-project#65
Qiannnly wants to merge 3 commits into
rocketacademy:mainfrom
Qiannnly:main

Conversation

@Qiannnly

Copy link
Copy Markdown

No description provided.

Comment thread src/App.js
Comment on lines +42 to +61
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()
);
}

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 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.

Suggested change
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.

Comment thread src/App.js
Comment on lines +117 to +144
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>
</>
);

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 could make this a component with a winner prop

Comment thread src/App.js
Comment on lines +147 to +153
const dealCardsButton = (
<>
<Button variant="contained" color="secondary" onClick={this.dealCards}>
Deal
</Button>
</>
);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Can also be an own component

Comment thread src/App.js
Comment on lines +155 to +173
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>
</>
);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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={} />

Comment thread src/App.js
{currCardElems}
</Box>

{this.state.hasGameBegin === true && (

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.hasGameBegin === true && (
{this.state.hasGameBegin && (

boolean can be used with truthy/falsy assumption

Comment thread src/App.js

{this.state.cardDeck.length > 0 && dealCardsButton}
<p>{this.state.hasGameBegin && winnerPerRound}</p>
<p>{this.state.cardDeck.length === 0 && winnerPerMatch}</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>{this.state.cardDeck.length === 0 && winnerPerMatch}</p>
<p>{!this.state.cardDeck.length && winnerPerMatch}</p>

0 is falsy in JavaScript

Comment thread src/App.js
</Box>
)}

{this.state.cardDeck.length > 0 && dealCardsButton}

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.cardDeck.length > 0 && dealCardsButton}
{this.state.cardDeck.length && dealCardsButton}

Above 0 is truthy in JS

Comment thread src/App.js
alignItems: "center",
}}
>
{this.state.cardDeck.length === 0 && continueGameButton}

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.cardDeck.length === 0 && continueGameButton}
{!this.state.cardDeck.length&& continueGameButton}

Comment thread src/App.js
}}
>
{this.state.cardDeck.length === 0 && continueGameButton}
{this.state.cardDeck.length === 0 && exitGameButton}

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 summarize similar state lookups into a single one:

Suggested change
{this.state.cardDeck.length === 0 && exitGameButton}
{!this.state.cardDeck.length && (
<>
<Button ...continue game button/>
<Button ...exit game button/>
</>
)}

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