-
Notifications
You must be signed in to change notification settings - Fork 97
At long last 😀 #60
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?
At long last 😀 #60
Changes from all commits
397f680
41c4978
ba7a54a
4c72a79
446f86a
b987f98
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 | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,171 @@ | ||||||||||||
| import React from "react"; | ||||||||||||
| import "./App.css"; | ||||||||||||
| import { makeShuffledDeck } from "./utils.js"; | ||||||||||||
|
|
||||||||||||
| import { Container, Row, Col } from "react-bootstrap"; | ||||||||||||
| // | ||||||||||||
|
|
||||||||||||
| // REFER TO IMPORT PNG IN REACT IN CHATGPT TO SOLVE IMAGES ISSUE | ||||||||||||
|
|
||||||||||||
| class App extends React.Component { | ||||||||||||
| constructor(props) { | ||||||||||||
| // Always call super with props in constructor to initialise parent class | ||||||||||||
| super(props); | ||||||||||||
| this.state = { | ||||||||||||
| // Set default value of card deck to new shuffled deck | ||||||||||||
| cardDeck: makeShuffledDeck(), | ||||||||||||
| // currCards holds the cards from the current round | ||||||||||||
| currCards: [], | ||||||||||||
| winner: null, // Add a winner state variable | ||||||||||||
| player1Score: 0, | ||||||||||||
| player2Score: 0, | ||||||||||||
| }; | ||||||||||||
|
|
||||||||||||
| // Define newCurrCards as a class property so it can be accessed outside dealCards() | ||||||||||||
| this.newCurrCards = []; | ||||||||||||
| this.player1 = {}; | ||||||||||||
| this.player2 = {}; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| dealCards = () => { | ||||||||||||
| if (this.state.cardDeck.length < 2) { | ||||||||||||
| // Check if there are enough cards in the deck | ||||||||||||
| return; | ||||||||||||
|
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 this check is good, but you are not handling it however. A simple return would leave the user with a blank screen. Maybe do something else, like making a new deck, rendering some text inform the user about what happens etc. |
||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| // this.state.cardDeck.pop() modifies this.state.cardDeck array | ||||||||||||
| this.newCurrCards = [this.state.cardDeck.pop(), this.state.cardDeck.pop()]; | ||||||||||||
| this.setState({ | ||||||||||||
| currCards: this.newCurrCards, | ||||||||||||
| winner: null, // Resets the winner when dealing new cards | ||||||||||||
| }); | ||||||||||||
|
|
||||||||||||
| console.log(this.newCurrCards); | ||||||||||||
|
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. Always remove console.logs before pushing code |
||||||||||||
| }; | ||||||||||||
|
|
||||||||||||
| ///////////////////////////// | ||||||||||||
| // IMPORTANT! DETERMINES WINNER BASED ON PLAYERSCORE | ||||||||||||
| whoWon = () => { | ||||||||||||
| if (this.state.cardDeck.length < 2) { | ||||||||||||
| return; // guard clause | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| ///////////// | ||||||||||||
| // Extract cards and determine winner | ||||||||||||
| // extracts the two cards and saves the 2 objects into the class properties, player1 and player2 | ||||||||||||
| [this.player1, this.player2] = this.newCurrCards; | ||||||||||||
|
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. Does this work? All I see this that you define the variables as this.player1. It is not exactly a Why do you not store the players in state by the way? Especially since these values get updated and need to be persistent values? |
||||||||||||
| console.log( | ||||||||||||
| this.player1.suit.toLowerCase(), | ||||||||||||
| this.player2.suit.toLowerCase() | ||||||||||||
| ); | ||||||||||||
|
|
||||||||||||
| //////////// | ||||||||||||
| // Comparing card ranks | ||||||||||||
| let winner = null; | ||||||||||||
| if (this.player1.rank > this.player2.rank) { | ||||||||||||
| winner = this.player1; // player1 is declared as the winner | ||||||||||||
|
|
||||||||||||
| // Incrementing overall player1Score | ||||||||||||
| this.setState((prevState) => ({ | ||||||||||||
| player1Score: prevState.player1Score + 1, | ||||||||||||
|
Comment on lines
+69
to
+70
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 could just add the winner state update into the respective player's updates.
Suggested change
|
||||||||||||
| /** Breakdown of incrementing player1Score | ||||||||||||
| 1. this.setState is a method provided by React to update state of a component. | ||||||||||||
| When setState is called, React re-renders the component with the updated state and reflects it in rendered UI | ||||||||||||
| 2. (prevState) => {...}: setState takes a function as an argument, which is the previous state, prevState. | ||||||||||||
| This ensures we are working with the most up-to-date state | ||||||||||||
| 3. player1Score: prevState.player1Score + 1 -> we then increment the player1Score property by 1 based on the previous state's value. This ensures only that part of the state is updated while the rest of the state is unchanged | ||||||||||||
| */ | ||||||||||||
| })); | ||||||||||||
| } else if (this.player1.rank < this.player2.rank) { | ||||||||||||
| winner = this.player2; | ||||||||||||
| // Incrementing overall player2Score | ||||||||||||
| this.setState((prevState) => ({ | ||||||||||||
| player2Score: prevState.player2Score + 1, | ||||||||||||
| })); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| ///////////// | ||||||||||||
| // Update the winner state variable | ||||||||||||
| this.setState({ winner }); // updates the component's state to indicate the winner of the current round. | ||||||||||||
| }; | ||||||||||||
|
|
||||||||||||
| render() { | ||||||||||||
| // You can write JavaScript here, just don't try and set your state! | ||||||||||||
|
|
||||||||||||
| // 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> | ||||||||||||
| )); | ||||||||||||
|
|
||||||||||||
| ////////////////// | ||||||||||||
| // FIX THE IMAGES! | ||||||||||||
| // if (this.player1.name && this.player1.suit) { | ||||||||||||
| // var player1ImageSrc = `./PNG-cards-1.3/${ | ||||||||||||
| // this.player1.name | ||||||||||||
| // }_of_${this.player1.suit.toLowerCase()}.png`; | ||||||||||||
| // } | ||||||||||||
|
|
||||||||||||
| return ( | ||||||||||||
| <Container fluid> | ||||||||||||
| <div className="App"> | ||||||||||||
| <header className="App-header"> | ||||||||||||
| <h3>High Card 🚀</h3> | ||||||||||||
| {currCardElems} | ||||||||||||
| <div> | ||||||||||||
| <div> | ||||||||||||
| {/* <img | ||||||||||||
| src={player1ImageSrc} | ||||||||||||
| alt={`Player1 = ${this.player1.name} of ${this.player1.suit}`} | ||||||||||||
| /> | ||||||||||||
| <img src={logoA} alt="Blackjack card" /> */} | ||||||||||||
| </div> | ||||||||||||
| </div> | ||||||||||||
| <br /> | ||||||||||||
| <button onClick={this.dealCards} className="button-spacing"> | ||||||||||||
| Deal | ||||||||||||
| </button> | ||||||||||||
| <button onClick={this.whoWon} className="button-spacing"> | ||||||||||||
| Who is the winner | ||||||||||||
| </button> | ||||||||||||
| {this.state.cardDeck.length < 2 ? ( | ||||||||||||
| <h1> | ||||||||||||
| The overall winner is:{" "} | ||||||||||||
| {this.state.player1Score > this.state.player2Score | ||||||||||||
| ? "Player 1" | ||||||||||||
| : "Player 2"} | ||||||||||||
|
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. So for a tie, Player 2 would be the overall winner? |
||||||||||||
| </h1> | ||||||||||||
| ) : ( | ||||||||||||
| <h1> | ||||||||||||
| The player that won is:{" "} | ||||||||||||
| {this.state.winner | ||||||||||||
| ? this.state.winner.name + " of " + this.state.winner.suit | ||||||||||||
| : "No winner yet"} | ||||||||||||
| </h1> | ||||||||||||
| )} | ||||||||||||
| <Row className="border1"> | ||||||||||||
| <div className="scores"> | ||||||||||||
| <Row className="border1"> | ||||||||||||
| The number of cards left in the deck is:{" "} | ||||||||||||
| <Col>{this.state.cardDeck.length}</Col> | ||||||||||||
| </Row> | ||||||||||||
| <Row className="border1"> | ||||||||||||
| <Col>Player 1's Score :</Col> | ||||||||||||
| <Col>{this.state.player1Score}</Col> | ||||||||||||
| </Row> | ||||||||||||
| <Row className="border1"> | ||||||||||||
| <Col>Player 2's Score :</Col> | ||||||||||||
| <Col>{this.state.player2Score}</Col> | ||||||||||||
| </Row> | ||||||||||||
| </div> | ||||||||||||
| </Row> | ||||||||||||
| </header> | ||||||||||||
| </div> | ||||||||||||
| </Container> | ||||||||||||
| ); | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| export default App; | ||||||||||||
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.
I am a bit confused. What is the difference between App.js and AppOriginal.js? I only just noticed those are 2 different files, which makes my review a bit unreliable. Please only keep 1 copy of a component in your repositories, as this would confuse anyone reviewing or reading your code