hypesystem

SimpleBlackJack - CardValue.java

Oct 19th, 2011
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.52 KB | None | 0 0
  1. package simpleblackjack;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. /**
  6.  * SimpleBlackJack uses enum types to emulate a simple BlackJackGame. In this
  7.  * game an ace is always 11 (to simplify the game mechanics).
  8.  * The following enum-type creates 13 different cards, from ace through to king,
  9.  * with their correct values and danish names. Several methods are created to
  10.  * help get info about the cards.
  11.  * @author hypesystem
  12.  */
  13. public enum CardValue {
  14.    
  15.     ACE (11, "es"),
  16.     TWO (2, "to"),
  17.     THREE (3, "tre"),
  18.     FOUR (4, "fire"),
  19.     FIVE (5, "fem"),
  20.     SIX (6, "seks"),
  21.     SEVEN (7, "syv"),
  22.     EIGHT (8, "otte"),
  23.     NINE (9, "ni"),
  24.     TEN (10, "ti"),
  25.     JACK (10, "knægt"),
  26.     QUEEN (10, "dronning"),
  27.     KING (10, "konge");
  28.  
  29.     private int value;
  30.     private String name;
  31.    
  32.     CardValue(int value, String name) {
  33.         this.value = value;
  34.         this.name = name;
  35.     }
  36.    
  37.     /**
  38.      * Gets the value of the specified card
  39.      * @param card The card of which the value is needed.
  40.      * @return The value of the specified card.
  41.      */
  42.     public static int getValue(CardValue card) {
  43.         return card.value;
  44.     }
  45.    
  46.     /**
  47.      * Gets the name of the specified card
  48.      * @param card The card of which the name is needed.
  49.      * @return The value of the specified card.
  50.      */
  51.     public static String getName(CardValue card) {
  52.         return card.name;
  53.     }
  54.    
  55.     /**
  56.      * Calculates and returns the sum of an array of cards.
  57.      * @param cards Array of cards to be calculated.
  58.      * @return The sum of the cards.
  59.      */
  60.     public static int sum(ArrayList<CardValue> cards) {
  61.         int sum = 0;
  62.         for(CardValue card : cards) {
  63.             sum += card.value;
  64.         }
  65.         return sum;
  66.     }
  67.    
  68.     /**
  69.      * Checks whether the cards in the specified array brings the player above
  70.      * the allowed amount of points.
  71.      * @param cards Array of cards to be checked.
  72.      * @return If player is bust.
  73.      */
  74.     public static boolean bust(ArrayList<CardValue> cards) {
  75.         if(sum(cards) > 21) return true;
  76.         else return false;
  77.     }
  78.    
  79.     /**
  80.      * Checks whether the player has a blackjack (as the game should stop in
  81.      * this situation) with the specified array of cards.
  82.      * @param cards Array of cards to be checked.
  83.      * @return If player has blackjack.
  84.      */
  85.     public static boolean blackjack(ArrayList<CardValue> cards) {
  86.         if(sum(cards) == 21) return true;
  87.         else return false;
  88.     }
  89.    
  90. }
  91.  
Advertisement
Add Comment
Please, Sign In to add comment