librejack

Blackjack clone written in java
git clone https://github.com/michaelneuper/librejack.git
README | Files | Log | Refs | LICENSE

commit d06e12ece93556475f9581b5653dafc1737bceae
parent f4501753f1bd9cf82c83f34981c598a938de2949
Author: Michael Neuper <73108749+michaelneuper@users.noreply.github.com>
Date:   Fri,  7 Oct 2022 18:59:20 +0200

Add shuffle method

Diffstat:
Msrc/model/Deck.java | 66+++++++++++++++++++++++++++++++++---------------------------------
1 file changed, 33 insertions(+), 33 deletions(-)

diff --git a/src/model/Deck.java b/src/model/Deck.java @@ -26,39 +26,39 @@ public class Deck { * Method that populates an empty deck with 52 cards * by looping over each rank and value */ - public void populate() { - for(Suit cardSuit : Suit.values()) { - for (Rank cardRank : Rank.values()) { - // adds a new card to the deck - this.deck.add(new Card(cardSuit, cardRank)); - } - } - } - -// /** -// * Method that shuffles the deck -// */ -// public void shuffle() { -// ArrayList<Card> shufDeck = new ArrayList<Card>(); // temporary deck for storing cards in random order -// -// Random random = new Random(); -// int randomCardIndex = 0; -// int originalSize = this.deck.size(); -// for(int i = 0; i < originalSize; i++) { -// // Generate random index -// /* -// Formula for generating random number: -// = random.nextInt((max - min) + 1) + min -// = random.nextInt(((deckSize - 1) - 0) + 1) + 0 -// */ -// randomCardIndex = random.nextInt(this.deck.size()); -// shufDeck.add(this.deck.get(randomCardIndex)); // add card to random index of temporary deck -// this.deck.remove(randomCardIndex); // remove card from original deck -// } -// -// this.deck = shufDeck; -// } -// + public void populate() { + for(Suit cardSuit : Suit.values()) { + for (Rank cardRank : Rank.values()) { + // adds a new card to the deck + this.deck.add(new Card(cardSuit, cardRank)); + } + } + } + + /** + * Method that shuffles the deck + */ + public void shuffle() { + ArrayList<Card> shufDeck = new ArrayList<Card>(); // temporary deck for storing cards in random order + + Random random = new Random(); + int randomCardIndex = 0; + int originalSize = this.deck.size(); + for(int i = 0; i < originalSize; i++) { + // Generate random index + /* + Formula for generating random number: + = random.nextInt((max - min) + 1) + min + = random.nextInt(((deckSize - 1) - 0) + 1) + 0 + */ + randomCardIndex = random.nextInt(this.deck.size()); + shufDeck.add(this.deck.get(randomCardIndex)); // add card to random index of temporary deck + this.deck.remove(randomCardIndex); // remove card from original deck + } + + this.deck = shufDeck; + } + // /** // * Method to draw card from one deck to another // * @param comingFromDeck deck the card comes from