hypesystem

SimpleBlackJack - SimpleBlackJackExample.java

Oct 19th, 2011
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.22 KB | None | 0 0
  1. package simpleblackjack;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. /**
  6.  * This is an example of how to implement the enum-type created in
  7.  * CardValue.java (http://pastebin.com/acEf6ary).
  8.  * A simple example has been created for each method, for you to see how it is
  9.  * implremented.
  10.  **-------
  11.  *
  12.  * @author hypesystem
  13.  */
  14. public class SimpleBlackJackExample {
  15.     private CardValue card;
  16.     private int card_value;
  17.     private String card_name;
  18.     private ArrayList<CardValue> hand;
  19.    
  20.     public SimpleBlackJackExample() {
  21.         //Assign a card to a variable
  22.         card = CardValue.ACE;
  23.        
  24.         //Get the value of a card
  25.         card_value = CardValue.getValue(card);
  26.        
  27.         //Get the name of a card
  28.         card_name = CardValue.getName(card);
  29.        
  30.         //Print out name and value
  31.         System.out.println(card_name+" ("+card_value+")"); //prints "es (11)"
  32.        
  33.         //Create array of cards, for example to represent the player's hand
  34.         hand = new ArrayList<CardValue>();
  35.         hand.add(CardValue.ACE);
  36.         hand.add(CardValue.TWO);
  37.         hand.add(CardValue.THREE);
  38.         hand.add(CardValue.FOUR);
  39.        
  40.         //Get and print the sum of the hand
  41.         int hand_sum = CardValue.sum(hand);
  42.         System.out.println(hand_sum); //prints "20"
  43.        
  44.         //Check if hand makes player bust (if value > 21)
  45.         if(CardValue.bust(hand)) System.out.println("Bust!");
  46.         else System.out.println("Not bust."); //prints "Not bust."
  47.        
  48.         //Change a card in hand, check bust again:
  49.         hand.set(3, CardValue.TEN); //four changed to ten; total is now 26
  50.         if(CardValue.bust(hand)) System.out.println("Bust!");
  51.         else System.out.println("Not bust."); //prints "Bust!"
  52.        
  53.         //Check if hand has blackjack
  54.         if(CardValue.blackjack(hand)) System.out.println("Blackjack!");
  55.         else System.out.println("No blackjack."); //prints "No blackjack."
  56.        
  57.         //Change card, check if blackjack again
  58.         hand.set(3, CardValue.FIVE);
  59.         if(CardValue.blackjack(hand)) System.out.println("Blackjack!");
  60.         else System.out.println("No blackjack."); //prints "Blackjack!"
  61.        
  62.     }
  63.    
  64. }
Advertisement
Add Comment
Please, Sign In to add comment