-
Notifications
You must be signed in to change notification settings - Fork 97
BC High Card - Hong Yun #62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
673aa49
ae562d0
054deaa
27807fe
9e16ae7
2945faa
822a7b0
57482ff
d7efb9b
d3e008d
50ed8d4
2b9245c
d23c5c9
ac69dff
09347b4
63a5066
8fb2ff6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,6 +1,14 @@ | ||||||
| import React from "react"; | ||||||
| import "./App.css"; | ||||||
| import { makeShuffledDeck } from "./utils.js"; | ||||||
| import PlayingCard from "./cardimages"; | ||||||
| import "@fontsource/roboto/300.css"; | ||||||
| import "@fontsource/roboto/400.css"; | ||||||
| import "@fontsource/roboto/500.css"; | ||||||
| import "@fontsource/roboto/700.css"; | ||||||
| import Button from "@mui/material/Button"; | ||||||
| import CasinoRoundedIcon from "@mui/icons-material/CasinoRounded"; | ||||||
| import RocketLaunchRoundedIcon from "@mui/icons-material/RocketLaunchRounded"; | ||||||
|
|
||||||
| class App extends React.Component { | ||||||
| constructor(props) { | ||||||
|
|
@@ -11,14 +19,73 @@ class App extends React.Component { | |||||
| cardDeck: makeShuffledDeck(), | ||||||
| // currCards holds the cards from the current round | ||||||
| currCards: [], | ||||||
| currComputerCards: [], | ||||||
| roundWinner: "", | ||||||
| score: [0, 0], | ||||||
| overallWinner: "", | ||||||
| roundsLeft: 26, | ||||||
| }; | ||||||
| } | ||||||
|
|
||||||
| dealCards = () => { | ||||||
| // this.state.cardDeck.pop() modifies this.state.cardDeck array | ||||||
| const newCurrCards = [this.state.cardDeck.pop(), this.state.cardDeck.pop()]; | ||||||
| const newCurrCards = [this.state.cardDeck.pop()]; | ||||||
| const newComputerCurrCards = [this.state.cardDeck.pop()]; | ||||||
| this.setState( | ||||||
| { | ||||||
| currCards: newCurrCards, | ||||||
| currComputerCards: newComputerCurrCards, | ||||||
| roundsLeft: this.state.roundsLeft - 1, | ||||||
| }, | ||||||
| () => this.determineWinner() | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I think you should be able to just pass the function itself like so |
||||||
| ); | ||||||
| if (this.state.cardDeck.length === 0) { | ||||||
| this.setState(() => this.determineOverallWinner()); | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||
| } | ||||||
| }; | ||||||
|
|
||||||
| determineWinner = () => { | ||||||
| const playerRank = this.state.currCards[0].rank; | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could currCards be empty? If so, you could have an edge case bug here, trying to access .rank on undefined |
||||||
| const computerRank = this.state.currComputerCards[0].rank; | ||||||
| let playerScore = this.state.score[0]; | ||||||
| let computerScore = this.state.score[1]; | ||||||
| if (playerRank > computerRank) { | ||||||
| this.setState({ | ||||||
| roundWinner: "Player", | ||||||
| score: [(playerScore += 1), computerScore], | ||||||
| }); | ||||||
| } else if (playerRank === computerRank) { | ||||||
| this.setState({ roundWinner: "Draw" }); | ||||||
| } else { | ||||||
| this.setState({ | ||||||
| roundWinner: "Computer", | ||||||
| score: [playerScore, (computerScore += 1)], | ||||||
| }); | ||||||
| } | ||||||
| }; | ||||||
|
|
||||||
| determineOverallWinner = () => { | ||||||
| const playerFinalScore = this.state.score[0]; | ||||||
| const computerFinalScore = this.state.score[1]; | ||||||
|
|
||||||
| if (playerFinalScore > computerFinalScore) { | ||||||
| this.setState({ overallWinner: "Player wins" }); | ||||||
| } else if (playerFinalScore === computerFinalScore) { | ||||||
| this.setState({ overallWinner: "It's a draw" }); | ||||||
| } else { | ||||||
| this.setState({ overallWinner: "Computer wins" }); | ||||||
| } | ||||||
| }; | ||||||
|
|
||||||
| restartGame = () => { | ||||||
| this.setState({ | ||||||
| currCards: newCurrCards, | ||||||
| cardDeck: makeShuffledDeck(), | ||||||
| currCards: [], | ||||||
| currComputerCards: [], | ||||||
| roundWinner: "", | ||||||
| score: [0, 0], | ||||||
| overallWinner: "", | ||||||
| roundsLeft: 26, | ||||||
| }); | ||||||
| }; | ||||||
|
|
||||||
|
|
@@ -28,18 +95,61 @@ class App extends React.Component { | |||||
| // You can access your current components state here, as indicated below | ||||||
| const currCardElems = this.state.currCards.map(({ name, suit }) => ( | ||||||
| // Give each list element a unique key | ||||||
| <div key={`${name}${suit}`}> | ||||||
| {name} of {suit} | ||||||
| </div> | ||||||
| <> | ||||||
| <div key={`${name}${suit}`}>{/*{name} of {suit}*/}</div> | ||||||
| <div> | ||||||
| <PlayingCard value={name} suit={suit} /> | ||||||
| </div> | ||||||
| </> | ||||||
| )); | ||||||
|
|
||||||
| const currComputerCardElems = this.state.currComputerCards.map( | ||||||
| ({ name, suit }) => ( | ||||||
| // Give each list element a unique key | ||||||
| <> | ||||||
| <div key={`${name}${suit}`}>{/* {name} of {suit}*/}</div> | ||||||
| <div> | ||||||
| <PlayingCard value={name} suit={suit} /> | ||||||
| </div> | ||||||
| </> | ||||||
| ) | ||||||
| ); | ||||||
|
Comment on lines
+98
to
+115
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We repeat the same html here. Could this be a separate component, into which we pass the name and suit prop? |
||||||
| const currWinner = this.state.roundWinner; | ||||||
| const overallWinner = this.state.overallWinner; | ||||||
| return ( | ||||||
| <div className="App"> | ||||||
| <header className="App-header"> | ||||||
| <h3>High Card 🚀</h3> | ||||||
| {currCardElems} | ||||||
| <br /> | ||||||
| <button onClick={this.dealCards}>Deal</button> | ||||||
| <h3> | ||||||
| <CasinoRoundedIcon /> High Card <RocketLaunchRoundedIcon /> | ||||||
| </h3> | ||||||
| {this.state.roundsLeft !== 26 && ( | ||||||
| <div className="card-hand"> | ||||||
| <p>Player Hand {currCardElems}</p> | ||||||
| <p>Computer Hand {currComputerCardElems}</p> | ||||||
| </div> | ||||||
| )} | ||||||
| {this.state.roundsLeft !== 26 && ( | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what is the 26? It seems to be some kind of game logic, but is not explained here. Maybe it would be better to pack it into a variable, say |
||||||
| <div className="winning-hand"> | ||||||
| <p>Winner: {currWinner}</p> | ||||||
| <p> | ||||||
| Player score: {this.state.score[0]} | ||||||
| <br /> | ||||||
| Computer score: {this.state.score[1]} | ||||||
| </p> | ||||||
| <p>Rounds Left: {this.state.roundsLeft}</p> | ||||||
| <p>{overallWinner}</p> | ||||||
| </div> | ||||||
| )} | ||||||
| <div> | ||||||
| {overallWinner ? ( | ||||||
| <Button variant="contained" onClick={this.restartGame}> | ||||||
| Restart | ||||||
| </Button> | ||||||
| ) : ( | ||||||
| <Button variant="contained" onClick={this.dealCards}> | ||||||
| Deal | ||||||
| </Button> | ||||||
| )} | ||||||
| </div> | ||||||
| </header> | ||||||
| </div> | ||||||
| ); | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import React from "react"; | ||
|
|
||
| const PlayingCard = ({ value, suit }) => { | ||
| const cardImage = require(`./images/SVG-cards-1.3/${value}_of_${suit}.svg`); | ||
| return ( | ||
| <div> | ||
| <img src={cardImage} alt={`${value}_of_${suit}`} /> | ||
| </div> | ||
| ); | ||
| }; | ||
| export default PlayingCard; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since it is just a single card, do we need an array? Also, does the variable need to be in plural?