diff --git a/src/App.js b/src/App.js index 899cefc..22f6857 100644 --- a/src/App.js +++ b/src/App.js @@ -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
+ Player {i + 1} got {name} of {suit} +