Skip to content
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

Brandon High-card 3.2 #10

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added cards/10-C.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added cards/10-D.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added cards/10-H.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added cards/10-S.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added cards/2-C.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added cards/2-D.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added cards/2-H.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added cards/2-S.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added cards/3-C.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added cards/3-D.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added cards/3-H.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added cards/3-S.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added cards/4-C.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added cards/4-D.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added cards/4-H.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added cards/4-S.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added cards/5-C.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added cards/5-D.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added cards/5-H.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added cards/5-S.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added cards/6-C.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added cards/6-D.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added cards/6-H.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added cards/6-S.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added cards/7-C.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added cards/7-D.png
Binary file added cards/7-H.png
Binary file added cards/7-S.png
Binary file added cards/8-C.png
Binary file added cards/8-D.png
Binary file added cards/8-H.png
Binary file added cards/8-S.png
Binary file added cards/9-C.png
Binary file added cards/9-D.png
Binary file added cards/9-H.png
Binary file added cards/9-S.png
Binary file added cards/A-C.png
Binary file added cards/A-D.png
Binary file added cards/A-H.png
Binary file added cards/A-S.png
Binary file added cards/BACK.png
Binary file added cards/J-B.png
Binary file added cards/J-C.png
Binary file added cards/J-D.png
Binary file added cards/J-H.png
Binary file added cards/J-R.png
Binary file added cards/J-S.png
Binary file added cards/K-C.png
Binary file added cards/K-D.png
Binary file added cards/K-H.png
Binary file added cards/K-S.png
Binary file added cards/Q-C.png
Binary file added cards/Q-D.png
Binary file added cards/Q-H.png
Binary file added cards/Q-S.png
22 changes: 22 additions & 0 deletions src/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,25 @@
.read-the-docs {
color: #888;
}

.scoreboard{
display: flex;
align-items: center;
flex-direction: row;
}

h3{
margin: 5px
}

img{
height: 175px;
width: 125px;
}

.cardimg{
display: flex;
justify-content: center;
flex-direction: row;
margin: 5px;
}
98 changes: 85 additions & 13 deletions src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,87 @@
import logo from "/logo.png";
import "./App.css";
import logo from "/logo.png";
import { makeShuffledDeck } from "./utils.jsx";
import { useState } from "react";

function App(props) {
function App() {
// 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([]);
const [player1Cards, setplayer1Cards] = useState(null);
const [player2Cards, setplayer2Cards] = useState(null);
const [player1Score, setplayer1Score] = useState(0);
const [player2Score, setplayer2Score] = useState(0);
Comment on lines +10 to +13

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 unify this in a single state variable?

const [playerTies, setPlayerTies] = useState(0);
const [winner , setWinner] = useState(null);
const [gameOver, setGameOver] = useState(false)

const resetGame = () => {
setCardDeck(makeShuffledDeck())
setplayer1Cards(null);
setplayer2Cards(null);
setplayer1Score(0);
setplayer2Score(0);
setPlayerTies(0);
setGameOver(false);
setWinner(null);
}

const dealCards = () => {
const newCurrCards = [cardDeck.pop(), cardDeck.pop()];
setCurrCards(newCurrCards);
if (cardDeck.length < 2) {
setGameOver(true);
finalWinner();
return
}
const player1Draw = cardDeck[0];
const player2Draw = cardDeck[1];

setplayer1Cards(player1Draw);
setplayer2Cards(player2Draw);

setCardDeck(cardDeck.slice(2));

checkWinner(player1Draw,player2Draw)
};

const checkWinner = (card1, card2) => {
if (card1.rank > card2.rank){
setplayer1Score(player1Score + 1)
} else if (card2.rank > card1.rank){
setplayer2Score(player2Score + 1)
} else {
setPlayerTies(playerTies + 1)
}
}

const finalWinner = () =>{
if (player1Score > player2Score) {
setWinner("Player 1 Wins")
} else if (player1Score < player2Score){
setWinner("Player 2 Wins")
} else {
setWinner("It is Ties")
}
}
// 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 = currCards.map(({ name, suit }) => (
// Give each list element a unique key
<div key={`${name}${suit}`}>
{name} of {suit}
const currCardElems = player1Cards && player2Cards && (
<div className="cardimg">
<div>
Player 1:
<div>
<img src={player1Cards.img}/>
</div>
</div>
<div>
Player 2:
<div>
<img src={player2Cards.img}/>
</div>
</div>

</div>
));
)

return (
<>
Expand All @@ -30,10 +90,22 @@ function App(props) {
</div>
<div className="card">
<h2>React High Card 🚀</h2>
{currCardElems}
<br />
<button onClick={dealCards}>Deal</button>
</div>
{gameOver && (
<div>
<h1>Game Over</h1>
<h1>{winner}</h1>
</div>
)}
<div className="scoreboard">
<h3>Player 1 Score: {player1Score}</h3>
<h3>Player 2 Score: {player2Score}</h3>
<h3>Player Ties: {playerTies}</h3>
</div>
<div>
<button onClick={gameOver? resetGame : dealCards}>{gameOver ? "Reset Game" : "Deal"}</button>
</div>
<div>{currCardElems}</div>
</>
);
}
Expand Down
13 changes: 8 additions & 5 deletions src/utils.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const makeDeck = () => {
// Initialise an empty deck array
const newDeck = [];
// Initialise an array of the 4 suits in our deck. We will loop over this array.
const suits = ["Hearts", "Diamonds", "Clubs", "Spades"];
const suits = ["H", "D", "C", "S"];

// Loop over the suits array
for (let suitIndex = 0; suitIndex < suits.length; suitIndex += 1) {
Expand All @@ -40,22 +40,25 @@ const makeDeck = () => {

// If rank is 1, 11, 12, or 13, set cardName to the ace or face card's name
if (cardName === "1") {
cardName = "Ace";
cardName = "A";
// Ace has higher rank than all other cards
cardRank = 14;
} else if (cardName === "11") {
cardName = "Jack";
cardName = "J";
} else if (cardName === "12") {
cardName = "Queen";
cardName = "Q";
} else if (cardName === "13") {
cardName = "King";
cardName = "K";
}

const imgPath = `./cards/${cardName}-${currentSuit}.png`;

// Create a new card with the current name, suit, and rank
const card = {
name: cardName,
suit: currentSuit,
rank: cardRank,
img: imgPath,
};

// Add the new card to the deck
Expand Down