From 080d6ccee4dd0862b913b97189f50b9e9f37d2ca Mon Sep 17 00:00:00 2001 From: Dave Burke Date: Sun, 23 Jan 2022 21:36:57 -0600 Subject: [PATCH] Implement shuffled deck --- 10_Blackjack/java/src/Blackjack.java | 13 +++++++++++++ 10_Blackjack/java/src/Card.java | 20 ++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/10_Blackjack/java/src/Blackjack.java b/10_Blackjack/java/src/Blackjack.java index dfb49548..cc8ec827 100644 --- a/10_Blackjack/java/src/Blackjack.java +++ b/10_Blackjack/java/src/Blackjack.java @@ -1,4 +1,8 @@ +import static java.util.stream.Collectors.joining; + import java.util.Arrays; +import java.util.Collections; +import java.util.LinkedList; public class Blackjack { public static void main(String[] args) { @@ -24,6 +28,15 @@ public class Blackjack { nPlayers = promptInt("NUMBER OF PLAYERS"); } + System.out.println("RESHUFFLING"); + LinkedList deck = new LinkedList<>(); + for(Card.Suit suit : Card.Suit.values()) { + for(int value = 1; value < 14; value++) { + deck.add(new Card(value, suit)); + } + } + Collections.shuffle(deck); + int[] bets = new int[nPlayers]; // empty array initialized with all '0' valuses. while(!betsAreValid(bets)){ System.out.println("BETS:"); diff --git a/10_Blackjack/java/src/Card.java b/10_Blackjack/java/src/Card.java index aa151742..8fd36ad0 100644 --- a/10_Blackjack/java/src/Card.java +++ b/10_Blackjack/java/src/Card.java @@ -31,4 +31,24 @@ public final class Card { return this.value; } + public Suit getSuit() { + return this.suit; + } + + public String toString() { + StringBuilder result = new StringBuilder(2); + if(value < 11) { + result.append(value); + } else if(value == 11) { + result.append('J'); + } else if(value == 12) { + result.append('Q'); + } else if(value == 13) { + result.append('K'); + } + // Uncomment to include the suit in output. Useful for debugging, but + // doesn't match the original BASIC behavior. + // result.append(suit.name().charAt(0)); + return result.toString(); + } } \ No newline at end of file