-
Notifications
You must be signed in to change notification settings - Fork 97
high-card-bootcamp Patrick #59
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
5f2ed4a
c2c4570
0eb1781
d708bdc
b2e9e7d
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,12 @@ | ||||||||||||||||
| import React from "react"; | ||||||||||||||||
| import "./App.css"; | ||||||||||||||||
| import { makeShuffledDeck } from "./utils.js"; | ||||||||||||||||
| import "bootstrap/dist/css/bootstrap.min.css"; | ||||||||||||||||
| import Container from "react-bootstrap/Container"; | ||||||||||||||||
| import Row from "react-bootstrap/Row"; | ||||||||||||||||
| import Col from "react-bootstrap/Col"; | ||||||||||||||||
| import Button from "react-bootstrap/Button"; | ||||||||||||||||
| import CardDisplay from "./CardDisplay"; | ||||||||||||||||
|
|
||||||||||||||||
| class App extends React.Component { | ||||||||||||||||
| constructor(props) { | ||||||||||||||||
|
|
@@ -11,35 +17,152 @@ class App extends React.Component { | |||||||||||||||
| cardDeck: makeShuffledDeck(), | ||||||||||||||||
| // currCards holds the cards from the current round | ||||||||||||||||
| currCards: [], | ||||||||||||||||
| cardsLeft: 52, | ||||||||||||||||
| gameStarted: false, | ||||||||||||||||
|
|
||||||||||||||||
| playerOneCurrWinner: false, | ||||||||||||||||
| playerOneScore: 0, | ||||||||||||||||
| playerTwoScore: 0, | ||||||||||||||||
| }; | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| playerOneIsHigher = (list) => { | ||||||||||||||||
| const suits = ["Diamonds", "Clubs", "Hearts", "Spades"]; | ||||||||||||||||
| if (list.length === 0) { | ||||||||||||||||
| console.log(list); | ||||||||||||||||
| return null; | ||||||||||||||||
| } | ||||||||||||||||
| if (list[0].rank === list[1].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 list[0] or list[1] possibly be undefined? If so, you might face a bug here |
||||||||||||||||
| if (suits.indexOf(list[0].suit) > suits.indexOf(list[1].suit)) { | ||||||||||||||||
| return true; | ||||||||||||||||
| } else { | ||||||||||||||||
| return false; | ||||||||||||||||
| } | ||||||||||||||||
|
Comment on lines
+36
to
+40
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
|
||||||||||||||||
| } else if (list[0].rank > list[1].rank) { | ||||||||||||||||
| return true; | ||||||||||||||||
| } else { | ||||||||||||||||
| return false; | ||||||||||||||||
| } | ||||||||||||||||
| }; | ||||||||||||||||
|
|
||||||||||||||||
| dealCards = () => { | ||||||||||||||||
| // this.state.cardDeck.pop() modifies this.state.cardDeck array | ||||||||||||||||
| const newCurrCards = [this.state.cardDeck.pop(), this.state.cardDeck.pop()]; | ||||||||||||||||
| const newPlayerOneCurrWinner = this.playerOneIsHigher(newCurrCards); | ||||||||||||||||
| this.setState({ | ||||||||||||||||
| currCards: newCurrCards, | ||||||||||||||||
| cardsLeft: this.state.cardsLeft - 2, | ||||||||||||||||
| gameStarted: true, | ||||||||||||||||
| playerOneCurrWinner: newPlayerOneCurrWinner, | ||||||||||||||||
| }); | ||||||||||||||||
| if (newPlayerOneCurrWinner) { | ||||||||||||||||
| this.setState({ | ||||||||||||||||
| playerOneScore: this.state.playerOneScore + 1, | ||||||||||||||||
| }); | ||||||||||||||||
| } else { | ||||||||||||||||
| this.setState({ | ||||||||||||||||
| playerTwoScore: this.state.playerTwoScore + 1, | ||||||||||||||||
| }); | ||||||||||||||||
| } | ||||||||||||||||
|
Comment on lines
52
to
+66
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. I think you should consolidate these into a single state update. You could look up how to conditionally set a object property |
||||||||||||||||
| }; | ||||||||||||||||
|
|
||||||||||||||||
| calculateWinningStatement = (p1Points, p2Points) => { | ||||||||||||||||
| if (this.state.cardsLeft > 0) { | ||||||||||||||||
| const currWinner = this.state.playerOneCurrWinner | ||||||||||||||||
| ? "Player 1" | ||||||||||||||||
| : "Player 2"; | ||||||||||||||||
| return "Winner of this round is: " + currWinner; | ||||||||||||||||
| } | ||||||||||||||||
| if (p1Points === p2Points) { | ||||||||||||||||
| return "This game is a draw!"; | ||||||||||||||||
| } | ||||||||||||||||
| let winnerString = "Winner of the game is: Player "; | ||||||||||||||||
| return winnerString + (p1Points > p2Points ? 1 : 2); | ||||||||||||||||
| }; | ||||||||||||||||
|
|
||||||||||||||||
| refreshPage = () => { | ||||||||||||||||
| window.location.reload(); | ||||||||||||||||
|
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 should avoid hard refreshes like this in a React app. This goes against what React has been developed for :) |
||||||||||||||||
| }; | ||||||||||||||||
|
|
||||||||||||||||
| render() { | ||||||||||||||||
| // You can write JavaScript here, just don't try and set your state! | ||||||||||||||||
| const instructions = ( | ||||||||||||||||
|
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. Can we make this a separate component? |
||||||||||||||||
| <div> | ||||||||||||||||
| 1. Assign yourselves player 1 and player 2<br /> | ||||||||||||||||
| 2. Press "Start Game" to start a round of High Card between 2 players | ||||||||||||||||
| <br /> | ||||||||||||||||
| 3. Suit value is ordered like this from highest to lowest | ||||||||||||||||
| <br /> | ||||||||||||||||
| {"largest Spades > Hearts > Clubs > Diamonds smallest"} | ||||||||||||||||
| </div> | ||||||||||||||||
| ); | ||||||||||||||||
|
|
||||||||||||||||
| const currCardElems = ( | ||||||||||||||||
| <Container className="mb-2"> | ||||||||||||||||
| <Row> | ||||||||||||||||
| <Col>Player 1</Col> | ||||||||||||||||
| <Col>Player 2</Col> | ||||||||||||||||
| </Row> | ||||||||||||||||
| <Row> | ||||||||||||||||
| <Col> | ||||||||||||||||
| <CardDisplay card={this.state.currCards[0]} /> | ||||||||||||||||
| </Col> | ||||||||||||||||
| <Col> | ||||||||||||||||
| <CardDisplay card={this.state.currCards[1]} /> | ||||||||||||||||
| </Col> | ||||||||||||||||
| </Row> | ||||||||||||||||
| <Row> | ||||||||||||||||
| {this.state.currCards.map(({ name, suit }, index) => ( | ||||||||||||||||
| // Give each list element a unique key | ||||||||||||||||
| <Col key={`${name} ${suit}`} className="col-6"> | ||||||||||||||||
| Player {index + 1}: {name} of {suit} | ||||||||||||||||
| </Col> | ||||||||||||||||
| ))} | ||||||||||||||||
| </Row> | ||||||||||||||||
| </Container> | ||||||||||||||||
| ); | ||||||||||||||||
|
|
||||||||||||||||
| const gameTable = ( | ||||||||||||||||
| <div> | ||||||||||||||||
| <h3> | ||||||||||||||||
| {this.calculateWinningStatement( | ||||||||||||||||
| this.state.playerOneScore, | ||||||||||||||||
| this.state.playerTwoScore | ||||||||||||||||
| )} | ||||||||||||||||
| </h3> | ||||||||||||||||
| <div>Player 1's Score: {this.state.playerOneScore}</div> | ||||||||||||||||
| <div>Player 2's Score: {this.state.playerTwoScore}</div> | ||||||||||||||||
| <h4> | ||||||||||||||||
| {this.state.cardsLeft > 0 | ||||||||||||||||
| ? `Card left in deck: ${this.state.cardsLeft}` | ||||||||||||||||
| : "Press the button below to play again"} | ||||||||||||||||
| </h4> | ||||||||||||||||
| </div> | ||||||||||||||||
| ); | ||||||||||||||||
|
|
||||||||||||||||
| // 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} | ||||||||||||||||
| const gameDisplay = ( | ||||||||||||||||
|
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. I feel like this warrants its own component/page as well |
||||||||||||||||
| <div className="col-11"> | ||||||||||||||||
| {currCardElems} {gameTable} | ||||||||||||||||
| </div> | ||||||||||||||||
| )); | ||||||||||||||||
| ); | ||||||||||||||||
|
|
||||||||||||||||
| const dealButtonText = | ||||||||||||||||
| this.state.cardsLeft === 0 ? "Start next round" : "Deal"; | ||||||||||||||||
|
|
||||||||||||||||
| return ( | ||||||||||||||||
| <div className="App"> | ||||||||||||||||
| <header className="App-header"> | ||||||||||||||||
| <h3>High Card 🚀</h3> | ||||||||||||||||
| {currCardElems} | ||||||||||||||||
| {this.state.gameStarted ? gameDisplay : instructions} | ||||||||||||||||
| <br /> | ||||||||||||||||
| <button onClick={this.dealCards}>Deal</button> | ||||||||||||||||
| <Button | ||||||||||||||||
| className="btn btn-light" | ||||||||||||||||
| onClick={ | ||||||||||||||||
| this.state.cardsLeft === 0 ? this.refreshPage : this.dealCards | ||||||||||||||||
|
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
0 is falsy |
||||||||||||||||
| } | ||||||||||||||||
| > | ||||||||||||||||
| {this.state.gameStarted ? dealButtonText : "Start Game"} | ||||||||||||||||
| </Button> | ||||||||||||||||
| </header> | ||||||||||||||||
| </div> | ||||||||||||||||
| ); | ||||||||||||||||
|
|
||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import React from "react"; | ||
| import "./App.css"; | ||
|
|
||
| export default class CardDisplay extends React.Component { | ||
| render() { | ||
| const imgName = `./PNG-cards/${this.props.card.name.toLowerCase()}_of_${this.props.card.suit.toLowerCase()}.png`; | ||
| return <img className="col-2" src={require(`${imgName}`)} alt="" />; | ||
| } | ||
| } |
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.