-
Notifications
You must be signed in to change notification settings - Fork 97
Marina - high card game #61
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
Open
Mannushka
wants to merge
8
commits into
rocketacademy:main
Choose a base branch
from
Mannushka: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
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d0bb325
step 1: declaring and dislpaying the winner of the round done
Mannushka f06370a
declaring a
Mannushka e5a08fe
number of rounds left and current round added
Mannushka 4bdd6d8
determing the game winner added
Mannushka b6b3cfe
game reset added, console.logs removed
Mannushka d831f81
p tages changed to br, position of the button changed
Mannushka f6d4c96
size of the button changed
Mannushka c4bb5e3
game directions changed, button text change added
Mannushka 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 hidden or 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 |
|---|---|---|
|
|
@@ -11,35 +11,121 @@ class App extends React.Component { | |
| cardDeck: makeShuffledDeck(), | ||
| // currCards holds the cards from the current round | ||
| currCards: [], | ||
| hasGameStarted: false, | ||
| currRoundWinner: null, | ||
| playerOneNumOfWins: 0, | ||
| playerTwoNumOfWins: 0, | ||
| isItLastRound: false, | ||
| }; | ||
| } | ||
|
|
||
| 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(), this.state.cardDeck.pop()]; // each player gets one card | ||
| this.setState({ | ||
| currCards: newCurrCards, | ||
| }); | ||
| this.determineThisRoundWinner(newCurrCards); | ||
| this.determineLastRound(); | ||
| }; | ||
| //function to determine the winner of the round | ||
| determineThisRoundWinner = (currCards) => { | ||
| let newCurrRoundWinner = null; | ||
| if (currCards[0].rank > currCards[1].rank) newCurrRoundWinner = 1; | ||
| if (currCards[1].rank > currCards[0].rank) newCurrRoundWinner = 2; | ||
| this.setState({ | ||
| hasGameStarted: true, | ||
| currRoundWinner: newCurrRoundWinner, | ||
| roundNumber: this.state.roundNumber + 1, | ||
| }); | ||
|
|
||
| this.keepScore(newCurrRoundWinner); | ||
| }; | ||
| //function to keep track of the score | ||
| keepScore = (currRoundWinner) => { | ||
| if (currRoundWinner === 1) { | ||
| this.setState({ playerOneNumOfWins: this.state.playerOneNumOfWins + 1 }); | ||
| } | ||
|
|
||
| if (currRoundWinner === 2) { | ||
| this.setState({ playerTwoNumOfWins: this.state.playerTwoNumOfWins + 1 }); | ||
| } | ||
| }; | ||
| //function to keep track of rounds and determine the las round | ||
| determineLastRound = () => { | ||
| let numOfRoundsLeft = this.state.cardDeck.length / 2; | ||
| if (numOfRoundsLeft === 0) { | ||
| this.setState({ isItLastRound: true }); | ||
| } | ||
| }; | ||
| resetGame = () => { | ||
| this.setState({ | ||
| cardDeck: makeShuffledDeck(), | ||
| currCards: [], | ||
| hasGameStarted: false, | ||
| currRoundWinner: null, | ||
| playerOneNumOfWins: 0, | ||
| playerTwoNumOfWins: 0, | ||
| isItLastRound: false, | ||
| }); | ||
| }; | ||
| 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 }) => ( | ||
| const currCardElems = this.state.currCards.map(({ name, suit }, i) => ( | ||
| // Give each list element a unique key | ||
| <div key={`${name}${suit}`}> | ||
| {name} of {suit} | ||
| <p> | ||
| Player {i + 1} got {name} of {suit} | ||
| </p> | ||
| </div> | ||
| )); | ||
| const gameDirections = this.state.isItLastRound | ||
| ? `Click "Reset the game" to start a new game.` | ||
| : `Click "Deal" to draw cards.`; | ||
| const currRoundWinnerOutput = this.state.currRoundWinner | ||
| ? `Player ${this.state.currRoundWinner} won this round.` | ||
| : `It's a tie!`; | ||
| const playerOneNumOfWinsOutput = `Player 1 has ${this.state.playerOneNumOfWins} wins.`; | ||
| const playerTwoNumOfWinsOutput = `Player 2 has ${this.state.playerTwoNumOfWins} wins.`; | ||
|
|
||
| const numOfRoundsLeftOutput = `Number of rounds left: ${ | ||
| this.state.cardDeck.length / 2 | ||
| }`; | ||
| //logic to determine the game winner | ||
| let gameWinner = null; | ||
| if (this.state.playerOneNumOfWins > this.state.playerTwoNumOfWins) { | ||
| gameWinner = 1; | ||
| } | ||
| if (this.state.playerTwoNumOfWins > this.state.playerOneNumOfWins) { | ||
| gameWinner = 2; | ||
| } | ||
| const gameWinnerOutput = gameWinner | ||
| ? `Player ${gameWinner} won the game!` | ||
| : `It's a tie!`; | ||
|
Comment on lines
+84
to
+106
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 am not exactly a fan of all these evaluations. I would rather use conditional rendering for these instead of storing all this in variables and redefining it constantly. |
||
|
|
||
| return ( | ||
| <div className="App"> | ||
| <header className="App-header"> | ||
| <h3>High Card 🚀</h3> | ||
| <h3>Welcome to High Card game!🚀</h3> | ||
| <h5>{gameDirections}</h5> | ||
| <br /> | ||
| <button | ||
| style={{ width: "100px", height: "50px" }} | ||
| onClick={this.state.isItLastRound ? this.resetGame : this.dealCards} | ||
| > | ||
| {this.state.isItLastRound ? "Reset the game" : "Deal"} | ||
| </button> | ||
| <br /> | ||
| {currCardElems} | ||
| <br /> | ||
| <button onClick={this.dealCards}>Deal</button> | ||
| {this.state.hasGameStarted && currRoundWinnerOutput} <br /> <br /> | ||
| {this.state.hasGameStarted && playerOneNumOfWinsOutput} <br /> <br /> | ||
| {this.state.hasGameStarted && playerTwoNumOfWinsOutput} | ||
| <br /> <br /> | ||
| {numOfRoundsLeftOutput} <br /> <br /> | ||
| {this.state.isItLastRound && gameWinnerOutput} | ||
| </header> | ||
| </div> | ||
| ); | ||
|
|
||
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.