Implement shuffled deck

This commit is contained in:
Dave Burke
2022-01-23 21:36:57 -06:00
parent 51f173c9da
commit 080d6ccee4
2 changed files with 33 additions and 0 deletions

View File

@@ -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<Card> 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:");

View File

@@ -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();
}
}