mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-22 07:10:42 -08:00
Implement shuffled deck
This commit is contained in:
@@ -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:");
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user