// Programmer: INSERT YOUR NAME // Assignment: Program 4 /******************************************************************** * BlackjackGame * * Member Variables: * INSERT DESCRIPTION OF MEMBER VARIABLES (IF ANY) * Private Methods: * INSERT DESCRIPTION OF PRIVATE METHODS (IF ANY) * Public Methods: * INSERT DESCRIPTION OF PUBLIC METHODS (IF ANY) *******************************************************************/ public class BlackjackGame { public final static int BLACKJACK = 21; public final static int MAX_CARDS_IN_HAND = 5; public final static int DEALER = 0; public final static int PLAYER = 1; public final static int TIE = 2; private static int wins; private static int losses; private static int ties; private static Deck deck; private static Hand hand; private static Hand dhand; public static void initGame() { deck = new Deck(); deck.shuffle(); hand = new Hand(MAX_CARDS_IN_HAND); dhand = new Hand(MAX_CARDS_IN_HAND); wins = losses = ties = 0; } public static Hand getPlayerHand() { return hand; } public static Hand getDealerHand() { return dhand; } public static void setupNewHand() { if (deck.cardsLeft() <= 2*MAX_CARDS_IN_HAND) deck.shuffle(); hand.resetHand(); dhand.resetHand(); hand.addCard(deck.dealFromTop()); dhand.addCard(deck.dealFromTop()); hand.addCard(deck.dealFromTop()); dhand.addCard(deck.dealFromTop()); } public static void addPlayerCard() { hand.addCard(deck.dealFromTop()); } public static void addDealerCards() { while (calcPoints(dhand) < 17 && dhand.getNumCards() < MAX_CARDS_IN_HAND) { dhand.addCard(deck.dealFromTop()); } } public static int decideWinner() { int playerTotal = calcPoints(hand); int dealerTotal = calcPoints(dhand); if (playerTotal > BLACKJACK || (playerTotal < dealerTotal && dealerTotal <= BLACKJACK)) { losses ++; return DEALER; } else if (dealerTotal > BLACKJACK || dealerTotal < playerTotal) { wins ++; return PLAYER; } else { ties++; return TIE; } } public static boolean playerCanHit() { return (calcPoints(hand) < BLACKJACK && hand.getNumCards() < MAX_CARDS_IN_HAND); } public static int getWins() { return wins; } public static int getLosses() { return losses; } public static int getTies() { return ties; } /****************************************************** * calcPoints * * This function returns the value of the hand * and the largest value of the hand less than BLACKJACK * if there are any Aces. ******************************************************/ public static int calcPoints(Hand h) { int total = 0; for (int i = 0; i < h.getNumCards(); i ++) { total += calcPoints(h.getCard(i)); } int cur = 0; while (total > BLACKJACK && cur < h.getNumCards()) { if (calcPoints(h.getCard(cur)) == 11) total -= 10; cur ++; } return total; } public static int calcPoints(Card card) { int face = card.getFace(); if (card.isFaceCard()) { String val = card.faceToString(); if (val.equals("Jack") || val.equals("Queen") || val.equals("King")) return 10; else return 11; } return face; } // end calcPoints method } // end of class BlackjackGame