Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package simpleblackjack;
- import java.util.ArrayList;
- /**
- * This is an example of how to implement the enum-type created in
- * CardValue.java (http://pastebin.com/acEf6ary).
- * A simple example has been created for each method, for you to see how it is
- * implremented.
- **-------
- *
- * @author hypesystem
- */
- public class SimpleBlackJackExample {
- private CardValue card;
- private int card_value;
- private String card_name;
- private ArrayList<CardValue> hand;
- public SimpleBlackJackExample() {
- //Assign a card to a variable
- card = CardValue.ACE;
- //Get the value of a card
- card_value = CardValue.getValue(card);
- //Get the name of a card
- card_name = CardValue.getName(card);
- //Print out name and value
- System.out.println(card_name+" ("+card_value+")"); //prints "es (11)"
- //Create array of cards, for example to represent the player's hand
- hand = new ArrayList<CardValue>();
- hand.add(CardValue.ACE);
- hand.add(CardValue.TWO);
- hand.add(CardValue.THREE);
- hand.add(CardValue.FOUR);
- //Get and print the sum of the hand
- int hand_sum = CardValue.sum(hand);
- System.out.println(hand_sum); //prints "20"
- //Check if hand makes player bust (if value > 21)
- if(CardValue.bust(hand)) System.out.println("Bust!");
- else System.out.println("Not bust."); //prints "Not bust."
- //Change a card in hand, check bust again:
- hand.set(3, CardValue.TEN); //four changed to ten; total is now 26
- if(CardValue.bust(hand)) System.out.println("Bust!");
- else System.out.println("Not bust."); //prints "Bust!"
- //Check if hand has blackjack
- if(CardValue.blackjack(hand)) System.out.println("Blackjack!");
- else System.out.println("No blackjack."); //prints "No blackjack."
- //Change card, check if blackjack again
- hand.set(3, CardValue.FIVE);
- if(CardValue.blackjack(hand)) System.out.println("Blackjack!");
- else System.out.println("No blackjack."); //prints "Blackjack!"
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment