-
Notifications
You must be signed in to change notification settings - Fork 19
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
update script #12
Open
Cnkh6069
wants to merge
2
commits into
rocketacademy:main
Choose a base branch
from
Cnkh6069:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
update script #12
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,39 +2,115 @@ import logo from "/logo.png"; | |
import "./App.css"; | ||
import { makeShuffledDeck } from "./utils.jsx"; | ||
import { useState } from "react"; | ||
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 { Alert, Image } from "react-bootstrap"; | ||
|
||
function App(props) { | ||
// Set default value of card deck to new shuffled deck | ||
const [cardDeck, setCardDeck] = useState(makeShuffledDeck()); | ||
// currCards holds the cards from the current round | ||
const [currCards, setCurrCards] = useState([]); | ||
//set state of number of rounds P1 and P2 won | ||
const [p1RoundsWon, setp1RoundWon] = useState(0); | ||
const [p2RoundsWon, setp2RoundWon] = useState(0); | ||
|
||
//Function to deal cards | ||
const dealCards = () => { | ||
const newCurrCards = [cardDeck.pop(), cardDeck.pop()]; | ||
setCurrCards(newCurrCards); | ||
|
||
// get the ranks of P1 and P2 cards and compare | ||
|
||
if (newCurrCards[0].rank > newCurrCards[1].rank) { | ||
setp1RoundWon(p1RoundsWon + 1); | ||
} | ||
if (newCurrCards[0].rank < newCurrCards[1].rank) { | ||
const newP2Score = p2RoundsWon + 1; | ||
setp2RoundWon(newP2Score); | ||
} | ||
}; | ||
// You can write JavaScript here, just don't try and set your state! | ||
let numGamesLeft = cardDeck.length / 2; | ||
let gameWinner = null; | ||
if (p1RoundsWon > p2RoundsWon) { | ||
gameWinner = 1; | ||
} else if (p2RoundsWon > p1RoundsWon) { | ||
gameWinner = 2; | ||
} | ||
|
||
const endGameMsg = gameWinner ? ( | ||
<p>Player {gameWinner} won the game!</p> | ||
) : ( | ||
<p>It is a draw</p> | ||
); | ||
|
||
// You can access your current components state here, as indicated below | ||
const currCardElems = currCards.map(({ name, suit }) => ( | ||
// Give each list element a unique key | ||
<div key={`${name}${suit}`}> | ||
{name} of {suit} | ||
</div> | ||
)); | ||
const ButtonText = cardDeck.length === 0 ? "Restart Game" : "Reset"; | ||
const ButtonText1 = cardDeck.length === 0 ? "" : "Deal"; | ||
|
||
const handleReset = () => { | ||
setCardDeck(makeShuffledDeck()); | ||
setp1RoundWon(0); | ||
setp2RoundWon(0); | ||
setCurrCards([]); | ||
}; | ||
// You can write JavaScript here, just don't try and set your state! | ||
// - Determine who has won each round (P1 or P2) | ||
//Keep score during each game (how many rounds has each player won) | ||
//Declare a winner at the end of each game when deck has run out of cards, and give the players the option to restart the game. | ||
|
||
// You can access your current components state here, as indicated below | ||
|
||
return ( | ||
<> | ||
<div> | ||
<img src={logo} className="logo" alt="Rocket logo" /> | ||
</div> | ||
<div className="card"> | ||
<Container> | ||
<Row> | ||
<Col> | ||
<Image width="50%" src={logo} alt="Rocket logo" /> | ||
</Col> | ||
</Row> | ||
<Row> | ||
<h2>React High Card 🚀</h2> | ||
{currCardElems} | ||
<br /> | ||
<button onClick={dealCards}>Deal</button> | ||
</div> | ||
</> | ||
<h3>{cardDeck.length} cards left!</h3> | ||
</Row> | ||
<Row> | ||
<Col className="column">{numGamesLeft === 0 && endGameMsg}</Col> | ||
</Row> | ||
<Row>{currCardElems}</Row> | ||
<Row> | ||
<Col className="column">Player 1 drew {currCardElems[0]}</Col> | ||
<Col className="column">and</Col> | ||
<Col className="column">Player 2 drew {currCardElems[1]}</Col> | ||
</Row> | ||
<Row> | ||
<Col className="column">Player 1 Score</Col> | ||
<Col className="column">vs.</Col> | ||
<Col className="column">Player 2 Score</Col> | ||
</Row> | ||
<Row> | ||
<Col className="column">{p1RoundsWon}</Col> | ||
<Col className="column">vs.</Col> | ||
<Col className="column">{p2RoundsWon}</Col> | ||
</Row> | ||
<Row> | ||
<Col className="column"> | ||
<Button variant="primary" onClick={dealCards}> | ||
{ButtonText1} | ||
</Button> | ||
</Col> | ||
<Col className="column"> | ||
<Button variant="secondary" onClick={handleReset}> | ||
{ButtonText} | ||
</Button> | ||
</Col> | ||
</Row> | ||
Comment on lines
+86
to
+112
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 do have some opportunity here to create components for the repeating elements. By not doing that, your App component is getting a bit messy |
||
</Container> | ||
); | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Do you think we can use a single state variable for player 1 and 2?