Black Jack MVC By Jeremy DiPaolo
Introduction Goal: To create a Black Jack game that takes advantage of the MVC framework. Uses many of the components we have discussed in class, and follows very close with idea of event-driven programming. Components used: Jpanel, Jlabel, JTextArea, Jbutton, Jspinner.
Introduction MVC Framwork: This program has 4 different classes: BlackJackView (the view) BlackJackModel (the model) Hand (the controllers) Card (the controllers)
Program Design: Card Class This is the simplest of all classes. This class is very portable and can be used for any card game that uses a standard deck of 52 playing cards. Uses enumerations to store the rank and suit. Each Card Object has two atrributes, rank and suit. Each card object also an images attached, based on the suit and rank. Card Images are stored in a two-dimensional String Array, with the ranks and suits as the indices,respectively. There is also a setDeck(), shuffleDeck(), and getCard() method.
Coding Examples: Card Class public class Card { public enum Rank { DEUCE, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE } public enum Suit { DIAMONDS, HEARTS, SPADES, CLUBS } static String[][] cardImages = new String[13][4]; static List deck = new ArrayList (); Rank rank; Suit suit; public Rank rank() { return rank; } public Suit suit() { return suit; } private Card(Rank rank, Suit suit) { this.rank = rank; this.suit = suit; }
Coding Examples: Card Class static public void setCardImages() { for (Suit s : Suit.values()) { for (Rank r : Rank.values()) { cardImages[r.ordinal()][s.ordinal()] = "C:\\Users\\Jeremy\\Desktop\\Fall 2010\\Window System Programming\\ProjectCards\\" + r + "_" + s + ".png"; } public static void setDeck() { for (Suit s : Suit.values()) { for (Rank r : Rank.values()) { deck.add(new Card(r, s)); } public static void shuffleDeck(){ Collections.shuffle(deck); } public static Card getCard(){ return deck.get(1); }
Program Design: Hand Class Another important class is the Hand Class. This class handles every situation that would arise in the game involving “Hands”., including the following methods: getCardValue(Card card) setHandValue( Card hand) & getHandValue() isBusted() compareHands (int dealerHandValue, int playerHandValue) clearCards() and clearHandValue() aceHandler()
Coding Examples: Hand Class public class Hand { static int handValue; static boolean aceFlag; public int getHandValue() { return handValue; } public void setHandValue(List hand) { for (int i = 0; i < hand.size(); i++) { handValue = handValue + getCardValue(hand.get(i)); } public boolean isBusted() { if (handValue > 21) { return true; } else { return false; } public static int aceHandler() { if (handValue + 11 > 21) { return 1; } else { return 11; } public boolean compareHands(int dealerHandValue, int playerHandValue) { if (playerHandValue > dealerHandValue) { return true; } else { return false; }
public static void clearCards() { Card.setDeck(); Card.shuffleDeck(); BlackJackModel.dealerHandValue = 0; BlackJackModel.playerHandValue = 0; for (int i = 0; i < BlackJackModel.dealerHand.size(); i++) { BlackJackModel.dealerHand.remove(i); } for (int i = 0; i < BlackJackModel.playerHand.size(); i++) { BlackJackModel.playerHand.remove(i); } public static void clearHandValue() { handValue = 0; } public static int getCardValue(Card card) { if (card.rank() == Card.Rank.ACE) { //if (aceFlag == true) { return aceHandler(); } else if (card.rank() == Card.Rank.DEUCE) { return 2; } else if (card.rank() == Card.Rank.THREE) { return 3; } else if (card.rank() == Card.Rank.FOUR) { return 4; } else if (card.rank() == Card.Rank.FIVE) { return 5; } else if (card.rank() == Card.Rank.SIX) { return 6; } else if (card.rank() == Card.Rank.SEVEN) { return 7; } else if (card.rank() == Card.Rank.EIGHT) { return 8; } else if (card.rank() == Card.Rank.NINE) { return 9; } else { return 10; }
Program Design: The View Uses several components the user can interact with: Jpanel - Represents the area where the dealer’s cards and player’s cards are displayed. JLabel - Represents one Card. JTextArea - Displays the current balance of the player Jspinner - Represents the amount the player wishes to bet each round. Jbutton – Represents each option the player has during the course of a game: Deal, Hit, and Stand.
Coding Examples: The View ActionListener dealActionListener = new ActionListener() { public void actionPerformed(ActionEvent dealActionEvent) { BlackJackModel.setBetSize(Integer.parseInt(betSpinner.getValue().toString())); BlackJackModel.deal(); balanceTextArea.setText(Integer.toString(BlackJackModel.balance)); dealButton.setEnabled(false); hitButton.setEnabled(true); standButton.setEnabled(true); betSpinner.setEnabled(false); Hand.clearHandValue(); for (int i = 0; i <= 1; i++) { Card pHand = BlackJackModel.playerHand.get(i); Card dHand = BlackJackModel.dealerHand.get(i); Deal ActionListener:
Coding Examples: The View (cont’d) JLabel pcard = new JLabel(new ImageIcon(Card.cardImages[pHand.rank().ordinal()][pHand.suit().ordinal()])); playerPane.add(pcard, FlowLayout.LEFT); playerPane.revalidate(); JLabel dcard = new JLabel(new ImageIcon(Card.cardImages[dHand.rank().ordinal()][dHand.suit().ordinal()])); dealerPane.add(dcard, FlowLayout.LEFT); dealerPane.revalidate(); } System.out.println(BlackJackModel.playerHandValue); } };
Program Design: The Model The model holds all the methods that dictate the rules of the game and the actions taken by the user during the game. It contains such methods as deal(), hit(), stand(), setBetSize(int betSize),updateBalance(int Value). All of these methods are attached to the view in some way, but the view simply displays the results of each of these methods.
Coding Examples: The Model public static void deal() { //Get the betSize from the spinner value //Deduct that amount from the player's balance balance = balance - betSize; playerCurrent = true; Card.setDeck(); Card.shuffleDeck(); Card playerCard1 = Card.getCard(); playerHand.add(playerCard1); Card.deck.remove(playerCard1); Card dealerCard1 = Card.getCard(); dealerHand.add(dealerCard1); Card.deck.remove(dealerCard1); Card playerCard2 = Card.getCard(); playerHand.add(playerCard2); Card.deck.remove(playerCard2); Card dealerCard2 = Card.getCard(); dealerHand.add(dealerCard2); Card.deck.remove(dealerCard2); hand.setHandValue(playerHand); //hand.setHandValue(dealerHand); playerHandValue = hand.getHandValue(); } Deal() Method:
Coding Examples: The Model (cont’d) public static void hit() { if (playerCurrent == true) { Card newCard = Card.getCard(); playerHand.add(newCard); Card.deck.remove(newCard); Hand.handValue = 0; hand.setHandValue(playerHand); playerHandValue = hand.getHandValue(); if (hand.isBusted() == true) { isBusted = true; } } else { Card newCard = Card.getCard(); dealerHand.add(newCard); Card.deck.remove(newCard); Hand.handValue = 0; hand.setHandValue(dealerHand); dealerHandValue = hand.getHandValue(); if (hand.isBusted() == true) { isBusted = true; } Hit() Method:
Conclusion Overall, I enjoyed this project as it allowed me to use a lot of the components that we learned in class. It made me appreciate the MVC framework and event driven paradigm, and how much more flexible it is to use such programming theories.
Conclussion Things to add to the program: Surrender option Double Down option I would also like to develop a similar project for other card games using the MVC framework already in place.