Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package simpleblackjack;
- import java.util.ArrayList;
- /**
- * SimpleBlackJack uses enum types to emulate a simple BlackJackGame. In this
- * game an ace is always 11 (to simplify the game mechanics).
- * The following enum-type creates 13 different cards, from ace through to king,
- * with their correct values and danish names. Several methods are created to
- * help get info about the cards.
- * @author hypesystem
- */
- public enum CardValue {
- ACE (11, "es"),
- TWO (2, "to"),
- THREE (3, "tre"),
- FOUR (4, "fire"),
- FIVE (5, "fem"),
- SIX (6, "seks"),
- SEVEN (7, "syv"),
- EIGHT (8, "otte"),
- NINE (9, "ni"),
- TEN (10, "ti"),
- JACK (10, "knægt"),
- QUEEN (10, "dronning"),
- KING (10, "konge");
- private int value;
- private String name;
- CardValue(int value, String name) {
- this.value = value;
- this.name = name;
- }
- /**
- * Gets the value of the specified card
- * @param card The card of which the value is needed.
- * @return The value of the specified card.
- */
- public static int getValue(CardValue card) {
- return card.value;
- }
- /**
- * Gets the name of the specified card
- * @param card The card of which the name is needed.
- * @return The value of the specified card.
- */
- public static String getName(CardValue card) {
- return card.name;
- }
- /**
- * Calculates and returns the sum of an array of cards.
- * @param cards Array of cards to be calculated.
- * @return The sum of the cards.
- */
- public static int sum(ArrayList<CardValue> cards) {
- int sum = 0;
- for(CardValue card : cards) {
- sum += card.value;
- }
- return sum;
- }
- /**
- * Checks whether the cards in the specified array brings the player above
- * the allowed amount of points.
- * @param cards Array of cards to be checked.
- * @return If player is bust.
- */
- public static boolean bust(ArrayList<CardValue> cards) {
- if(sum(cards) > 21) return true;
- else return false;
- }
- /**
- * Checks whether the player has a blackjack (as the game should stop in
- * this situation) with the specified array of cards.
- * @param cards Array of cards to be checked.
- * @return If player has blackjack.
- */
- public static boolean blackjack(ArrayList<CardValue> cards) {
- if(sum(cards) == 21) return true;
- else return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment