public Card(String rank, String suit){ this.rank = rank; this.suit = suit; this.score = set_score(rank); } private int set_score(String rank){ if (rank.equals("Ace")){ return 1; } else if (rank.equals("Jack") || rank.equals("Queen") || rank.equals("King")){ return 10; } else { return Integer.parseInt(rank); } }
public String toString(){ return rank + " of " + suit; }
public int score(){ return score; }
public boolean is_ace(){ return rank.equals("Ace"); }
public class CardTest { public static void main (String[] args){ String [] ranks = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"}; for (int i = 0; i < ranks.length; i++){ Card c = new Card(ranks[i], "Spades"); System.out.println(c + ", score: " + c.score() + ", is_ace: " + c.is_ace()); } } } $ java CardTest Ace of Spades, score: 1, is_ace: true 2 of Spades, score: 2, is_ace: false 3 of Spades, score: 3, is_ace: false 4 of Spades, score: 4, is_ace: false 5 of Spades, score: 5, is_ace: false 6 of Spades, score: 6, is_ace: false 7 of Spades, score: 7, is_ace: false 8 of Spades, score: 8, is_ace: false 9 of Spades, score: 9, is_ace: false 10 of Spades, score: 10, is_ace: false Jack of Spades, score: 10, is_ace: false Queen of Spades, score: 10, is_ace: false King of Spades, score: 10, is_ace: false
private final String[] suits = {"Clubs", "Diamonds", "Hearts", "Spades"}; private final String[] ranks = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
private ArrayList<Card> deck = new ArrayList<>();
for
looppublic Deck (){ for (int i = 0; i < suits.length; i++){ for (int j =0; j < ranks.length; j++){ deck.add(new Card(ranks[j], suits[i])); } } }
public String toString(){ String value = ""; for (int i = 0; i < deck.size(); i++){ value += deck.get(i) + " "; } return value; }
$ java DeckTest1 Ace of Clubs 2 of Clubs 3 of Clubs 4 of Clubs 5 of Clubs 6 of Clubs 7 of Clubs 8 of Clubs ...
public void shuffle(){ Random rand = new Random(); int size = deck.size(); ArrayList<Card< temp = new ArrayList<>(); while (size > 0){ int i = rand.nextInt(size); Card c = deck.get(i); deck.remove(i); temp.add(c); size = deck.size(); } for (int i = 0; i < temp.size(); i++){ Card c = temp.get(i); deck.add(c); } }
public class DeckTest2 { public static void main (String[] args){ Deck2 d = new Deck2(); System.out.println(d); d.shuffle(); System.out.println("After shuffle"); System.out.println(d); } } $ java DeckTest2 Ace of Clubs 2 of Clubs 3 of Clubs 4 of Clubs 5 of Clubs 6 of Clubs 7 of Clubs 8 of Clubs ... After shuffle Ace of Hearts 5 of Diamonds King of Clubs 7 of Spades 6 of Clubs King of Hearts Jack of Hearts ...
public Card deal_card(){ Card card = deck.get(0); deck.remove(0); return card; }
public class DeckTest2 { public static void main (String[] args){ Deck2 d = new Deck2(); System.out.println(d); d.shuffle(); System.out.println("After shuffle"); System.out.println(d); } } $ java DeckTest3 King of Diamonds 10 of Hearts 3 of Diamonds 7 of Spades Queen of Spades 2 of Clubs ... Dealt card King of Diamonds 10 of Hearts 3 of Diamonds 7 of Spades Queen of Spades 2 of Clubs 6 of Hearts 4 of Clubs
private ArrayList<Card< cards; private String player; private boolean has_ace; private boolean face_up;
public Hand (String player){ this.cards = new ArrayList<>(); this.player = player; this.has_ace = false; this.face_up = false; }
public String toString(){ String value = player + "\n" ; for (int i = 0; i < cards.size(); i++) { value += cards.get(i) + SPACER; } return value; }
private final String SPACER = " ";
public class HandTest1 { public static void main (String[] args){ Hand hand = new Hand("User"); System.out.println(hand); } } $ java HandTest1 User
public void add_card(Card1 card){ cards.add(card); }
public int score(){ int total = 0; for (int i = 0; i < cards.size(); i++) { total += cards.get(i).score(); } return total; }
public class HandTest2 { public static void main (String[] args){ Hand2 hand = new Hand2("User"); Card1 c1 = new Card1("5", "Clubs"); hand.add_card(c1); Card1 c2 = new Card1("King", "Spades"); hand.add_card(c2); System.out.println(hand); System.out.println(hand.score()); } } $ java HandTest2 User 5 of Clubs King of Spades 15
public String toString(){
String value = player + "\n" ;
for (int i = 0; i < cards.size(); i++) {
Card c = cards.get(i);
if (player.equals("Dealer") && cards.size() == 2 && i == 1){ // second card
value += FACE_DOWN + SPACER;
} else {
value += cards.get(i) + SPACER;
}
}
return value;
}
private final String FACE_DOWN = "XXXX";
public class HandTest3 { public static void main (String[] args){ Hand3 hand = new Hand3("Dealer"); Card c1 = new Card("5", "Clubs"); hand.add_card(c1); System.out.println(hand); System.out.println(); Card c2 = new Card("King", "Spades"); hand.add_card(c2); System.out.println(hand); System.out.println(); Card c3 = new Card("10", "Spades"); hand.add_card(c3); System.out.println(hand); } } $ java HandTest3 Dealer 5 of Clubs Dealer 5 of Clubs XXXX Dealer 5 of Clubs King of Spades 10 of Spades
private boolean has_ace;
false
by the constructortrue
whenever and Ace is added
public void add_card(Card card){
cards.add(card);
if (card.is_ace()) {
has_ace = true;
}
}
public class HandTest4 { public static void main (String[] args){ Hand4 hand = new Hand4("User"); Card c1 = new Card("5", "Clubs"); hand.add_card(c1); System.out.println(hand); System.out.println("Hand has ace? " + hand.has_ace()); System.out.println(); Card c2 = new Card("Ace", "Spades"); hand.add_card(c2); System.out.println(hand); System.out.println("Hand has ace? " + hand.has_ace()); } } $ java HandTest4 User 5 of Clubs Hand has ace? false User 5 of Clubs Ace of Spades Hand has ace? true
public int score(){
int total = 0;
for (int i = 0; i < cards.size(); i++) {
total += cards.get(i).score();
}
if (has_ace && total < 12 && player.equals("User")){
total += 10;
}
return total;
}
public class HandTest5 { public static void main (String[] args){ System.out.println("User hand"); System.out.println(); Hand5 hand1 = new Hand5("User"); Card c1 = new Card("Ace", "Clubs"); hand1.add_card(c1); System.out.println(hand1); System.out.println("Score: " + hand1.score()); System.out.println(); Card c2 = new Card("9", "Spades"); hand1.add_card(c2); System.out.println(hand1); System.out.println("Score: " + hand1.score()); System.out.println(); Card c3 = new Card("6", "Spades"); hand1.add_card(c3); System.out.println(hand1); System.out.println("Score: " + hand1.score()); System.out.println(); System.out.println("Dealer hand"); Hand5 hand2 = new Hand5("Dealer"); System.out.println(); Card c4 = new Card("Ace", "Spades"); hand2.add_card(c4); System.out.println(hand2); System.out.println("Score: " + hand2.score()); } } $ java HandTest5 User hand User Ace of Clubs Score: 11 User Ace of Clubs 9 of Spades Score: 20 User Ace of Clubs 9 of Spades 6 of Spades Score: 16 Dealer hand Dealer Ace of Spades Score: 1
public boolean is_busted(){ return score() > 21; }
public class HandTest6 { public static void main (String[] args){ Hand hand = new Hand("Dealer"); Card c1 = new Card("5", "Clubs"); hand.add_card(c1); System.out.println(hand); System.out.println("Hand has ace? " + hand.has_ace()); System.out.println("Score: " + hand.score()); System.out.println("Busted? " + hand.is_busted()); System.out.println(); Card c2 = new Card("King", "Spades"); hand.add_card(c2); System.out.println(hand); System.out.println("Hand has ace? " + hand.has_ace()); System.out.println("Score: " + hand.score()); System.out.println("Busted? " + hand.is_busted()); System.out.println(); Card c3 = new Card("Ace", "Spades"); hand.add_card(c3); System.out.println(hand); System.out.println("Hand has ace? " + hand.has_ace()); System.out.println("Score: " + hand.score()); System.out.println("Busted? " + hand.is_busted()); System.out.println(); Card c4 = new Card("6", "Clubs"); hand.add_card(c4); System.out.println(hand); System.out.println("Hand has ace? " + hand.has_ace()); System.out.println("Score: " + hand.score()); System.out.println("Busted? " + hand.is_busted()); } } $ java HandTest6 Dealer 5 of Clubs Hand has ace? false Score: 5 Busted? false Dealer 5 of Clubs XXXX Hand has ace? false Score: 15 Busted? false Dealer 5 of Clubs King of Spades Ace of Spades Hand has ace? true Score: 16 Busted? false Dealer 5 of Clubs King of Spades Ace of Spades 6 of Clubs Hand has ace? true Score: 22 Busted? true
private boolean face_up;
false
by the constructorpublic void set_face_up(){ face_up = true; }
if
statementpublic String toString(){ String value = player + "\n" ; for (int i = 0; i < cards.size(); i++) { Card c = cards.get(i); if (player.equals("Dealer") && cards.size() == 2 && i == 1 && ! face_up){ value += FACE_DOWN + SPACER; } else { value += cards.get(i) + SPACER; } } return value; }
public class HandTest7 { public static void main (String[] args){ Hand7 hand = new Hand7("Dealer"); Card c1 = new Card("5", "Clubs"); hand.add_card(c1); Card c2 = new Card("King", "Spades"); hand.add_card(c2); System.out.println("After first card dealt"); System.out.println(hand); System.out.println(); System.out.println("After set_face_up called"); hand.set_face_up(); System.out.println(hand); } } $ java HandTest7 After first card dealt Dealer 5 of Clubs XXXX After set_face_up called Dealer 5 of Clubs King of Spades
public class BlackJack1 { public static void main (String[] args){ Deck deck = new Deck(); Hand dealer_hand = new Hand("Dealer"); Hand user_hand = new Hand("User"); deck.shuffle(); System.out.println(deck); System.out.println(); System.out.println(dealer_hand); System.out.println(); System.out.println(user_hand); } } $ java BlackJack1 8 of Spades 3 of Diamonds 5 of Hearts 7 of Diamonds 4 of Spades 9 of Diamonds ... Dealer User
public class BlackJack2 {
public static void main (String[] args){
Deck deck = new Deck();
Hand dealer_hand = new Hand("Dealer");
Hand user_hand = new Hand("User");
deck.shuffle();
System.out.println("Dealing two cards to each player");
Card c1 = deck.deal_card();
user_hand.add_card(c1);
Card c2 = deck.deal_card();
dealer_hand.add_card(c2);
Card c3 = deck.deal_card();
user_hand.add_card(c3);
Card c4 = deck.deal_card();
dealer_hand.add_card(c4);
System.out.println();
System.out.println(dealer_hand);
System.out.println();
System.out.println(user_hand);
}
}
$ java BlackJack2
Dealing two cards to each player
Dealer
2 of Spades XXXX
User
4 of Clubs Ace of Spades
public class BlackJack3 { private static Deck deck; private static Hand dealer_hand; private static Hand user_hand; public static void main (String[] args){ setup_game(); initial_deal(); } private static void setup_game(){ deck = new Deck(); dealer_hand = new Hand("Dealer"); user_hand = new Hand("User"); deck.shuffle(); } private static void initial_deal(){ System.out.println("Dealing two cards to each player"); Card c1 = deck.deal_card(); user_hand.add_card(c1); Card c2 = deck.deal_card(); dealer_hand.add_card(c2); Card c3 = deck.deal_card(); user_hand.add_card(c3); Card c4 = deck.deal_card(); dealer_hand.add_card(c4); System.out.println(); System.out.println(dealer_hand); System.out.println(); System.out.println(user_hand); } } $ java BlackJack3 Dealing two cards to each player Dealer 7 of Diamonds XXXX User Queen of Clubs Ace of Diamonds
while
loop looking for a valid response from the userprivate static String ask_user(){ String reply = ""; try { Scanner console = new Scanner(System.in); while (! reply.equals("h") && ! reply.equals("s")){ System.out.println(); System.out.println("H(it) S(tand)"); reply = console.nextLine(); reply = reply.trim(); reply = reply.toLowerCase(); } } catch (Exception e){ System.out.println(e); } return reply; }
while
looppublic static void main (String[] args){ setup_game(); initial_deal(); boolean user_done = false; while (! user_done){ String action = ask_user(); if (action.equals("s")) user_done = true; } }
$ java BlackJack4 Dealing two cards to each player Dealer 7 of Hearts XXXX User 8 of Hearts 7 of Clubs H(it) S(tand) h H(it) S(tand) s
else if
clause to the if
statement inside the while loop
public static void main (String[] args){
setup_game();
initial_deal();
boolean user_done = false;
while (! user_done){
String action = ask_user();
if (action.equals("s")){
user_done = true;
} else if (action.equals("h")) {
Card c = deck.deal_card();
user_hand.add_card(c);
System.out.println(user_hand);
}
}
}
$ java BlackJack5 Dealing two cards to each player Dealer 7 of Hearts XXXX User 6 of Hearts 3 of Diamonds H(it) S(tand) h User 6 of Hearts 3 of Diamonds Queen of Clubs H(it) S(tand) h User 6 of Hearts 3 of Diamonds Queen of Clubs 2 of Hearts H(it) S(tand) s
private static void show_hands(){ System.out.println(); System.out.println(dealer_hand); System.out.println(); System.out.println(user_hand); }
public static void main (String[] args){ setup_game(); initial_deal(); show_hands(); boolean user_done = false; while (! user_done){ String action = ask_user(); if (action.equals("s")){ user_done = true; } else if (action.equals("h")) { Card c = deck.deal_card(); user_hand.add_card(c); show_hands(); } } show_hands(); }
$ java BlackJack6 Dealing two cards to each player Dealer 6 of Hearts XXXX User 9 of Spades Ace of Clubs H(it) S(tand) h Dealer 6 of Hearts XXXX User 9 of Spades Ace of Clubs King of Clubs H(it) S(tand) s Dealer 6 of Hearts XXXX User 9 of Spades Ace of Clubs King of Clubs
while
loop
while (! user_done && ! user_hand.is_busted()){
if
statement to the bottom of main
if (user_hand.is_busted()){ System.out.println(); System.out.println("You're busted!!!"); }
$ java BlackJack7 Dealing two cards to each player Dealer 7 of Spades XXXX User 10 of Diamonds Queen of Hearts H(it) S(tand) h Dealer 7 of Spades XXXX User 10 of Diamonds Queen of Hearts King of Clubs Dealer 7 of Spades XXXX User 10 of Diamonds Queen of Hearts King of Clubs You're busted!!!
while
loop
dealer_hand.set_face_up(); show_hands();
$ java BlackJack8
Dealing two cards to each player
Dealer
7 of Clubs XXXX
User
5 of Spades 8 of Spades
H(it) S(tand)
h
Dealer
7 of Clubs XXXX
User
5 of Spades 8 of Spades 6 of Clubs
H(it) S(tand)
s
Dealer
7 of Clubs 9 of Hearts
User
5 of Spades 8 of Spades 6 of Clubs
else
clause after testing whether the user went bust
while
if (user_hand.is_busted()){
System.out.println();
System.out.println("You're busted!!!");
} else {
while (dealer_hand.score() < 17) {
System.out.println("Dealer takes a card");
Card c = deck.deal_card();
dealer_hand.add_card(c);
show_hands();
}
}
$ java BlackJack9 Dealing two cards to each player Dealer 7 of Clubs XXXX User 4 of Hearts 6 of Clubs H(it) S(tand) h Dealer 7 of Clubs XXXX User 4 of Hearts 6 of Clubs 7 of Spades H(it) S(tand) s Dealer 7 of Clubs Ace of Clubs User 4 of Hearts 6 of Clubs 7 of Spades Dealer takes a card Dealer 7 of Clubs Ace of Clubs 10 of Hearts User 4 of Hearts 6 of Clubs 7 of Spades
// copied from https://www.baeldung.com/java-delay-code-execution private static void sleep(int seconds){ try { Thread.sleep(seconds * 1000); } catch (InterruptedException ie) { Thread.currentThread().interrupt(); } }
if (user_hand.is_busted()){ System.out.println(); System.out.println("You're busted!!!"); } else { sleep(1); while (dealer_hand.score() < 17) { System.out.println("Dealer takes a card"); Card c = deck.deal_card(); dealer_hand.add_card(c); show_hands(); sleep(2); } }
while
loop
if (dealer_hand.is_busted()){ System.out.println("Dealer busted\nUser wins!!"); } else if (user_hand.score() > dealer_hand.score()) { System.out.println("User wins!!"); } else { System.out.println("User loses"); }
$ jv BlackJack.java Dealing two cards to each player Dealer 2 of Hearts XXXX User 4 of Hearts 9 of Diamonds H(it) S(tand) h Dealer 2 of Hearts XXXX User 4 of Hearts 9 of Diamonds 4 of Clubs H(it) S(tand) s Dealer 2 of Hearts 8 of Diamonds User 4 of Hearts 9 of Diamonds 4 of Clubs Dealer takes a card Dealer 2 of Hearts 8 of Diamonds 6 of Clubs User 4 of Hearts 9 of Diamonds 4 of Clubs Dealer takes a card Dealer 2 of Hearts 8 of Diamonds 6 of Clubs 5 of Hearts User 4 of Hearts 9 of Diamonds 4 of Clubs User loses