Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For this question you will implement a game of blackjack. An (abridged)1 version

ID: 3768058 • Letter: F

Question

For this question you will implement a game of blackjack. An (abridged)1 version of the rules follows. A player is playing the game against the casino (dealer). Each card is assigned point values (see below), and the goal is to have a hand of cards with as close to a score of 21 as possible without going over. A score of 21 is the best possible score. If it is obtained with only two cards, then it is called blackjack. Both the player and the dealer are each initially given two cards. The player can see both their own cards, but only the second of the dealer's cards. After the cards are dealt, the player chooses whether to hit or stay. If they choose to hit, then they get an additional card. If they choose to stay, then the player's turn is over and the dealer gets to play. The player will continue to choose until they either stay or go over 21. If the player goes over 21 (this is known as "busting"), then the player loses automatically no matter what the dealer does. If the player stays at a score of 21 or lower, the dealer then plays. The dealer will always hit until their total is 18 or higher, at which point they will always stay. If neither player busts, then the player whose score is closest to 21, but not over 21 wins. If both the player and the dealer have the same score, then they tie (also known as a "push"). One last rule is that a player who is dealt "blackjack" (2 cards that total 21) automatically wins without the dealer getting to play at all unless the dealer is also dealt "blackjack" (2 cards that add up to 21) in which case it is a tie. This means that if the player gets "blackjack," the dealer does not get a chance to play to try to get 21 as well. Point values of each card: Two through Nine are worth 2-9 points each. 10, Jack, Queen, and King are each worth 10 points. Ace is worth either 1 or 11, whichever gives a better score. The suit is irrelevant. For example, a hand of: 2 of Spades, 8 of Diamonds, King of Hearts is worth 20 points. A hand of Ace of Spades, 8 of Diamonds, King of Hearts is worth 19 points. A hand of Ace of Spades, King of Hearts is worth 21 points (and is blackjack). A hand of Ace of Spades, Ace of Spades, Nine of Clubs is worth 21 points. For further details and examples see: http://uwu.pagat.com/banking/blackjack.html. Remember that you are only required to implement this simplified version of the game for this assignment. In order to do implement this, you will define a class and are required to write the following helper methods in addition to your main method: A static method getScore that takes as input a Card object and returns its score in blackjack. In this method, a card whose value is ACE should return 11. A static method countValues that takes as input a CardPile object and returns its total blackjack score. This method should return the best possible score for a CardPile where "best" means as close to 21 as possible without going over. A static method playRound that takes a CardPile as input and returns nothing. This method should execute one round of blackjack and in doing so will remove cards from the CardPile that was input. It must also use a Scanner to obtain user input, as the user needs to decide after each new card is dealt, whether to hit or stay. It is required that this method print all of the information that the user should have access to as well as the result of each round (including the full hand of the dealer). In your main method, you must create a CardPile of 4 complete decks of cards and then play blackjack until there are fewer than or equal (

Explanation / Answer

Card.Java


public class Card
{

    public final static int SPADES = 0,    
                            HEARTS = 1,
                            DIAMONDS = 2,
                            CLUBS = 3;
                          
    public final static int ACE = 1,       
                            JACK = 11,   
                            QUEEN = 12,  
                            KING = 13;
                          
    private final int suit;
                           
                            
    private final int value;
                           
    public Card(int theValue, int theSuit)
    {
          
        value = theValue;
        suit = theSuit;
    }
      
    public int getSuit()
    {
         
        return suit;
    }
  
    public int getValue()
    {
         
        return value;
    }
  
    public String getSuitAsString()
    {
         
        switch ( suit )
        {
           case SPADES:   return "Spades";
           case HEARTS:   return "Hearts";
           case DIAMONDS: return "Diamonds";
           case CLUBS:    return "Clubs";
           default:       return "??";
        }
    }
  
    public String getValueAsString()
    {
        switch ( value ) {
           case 1:   return "Ace";
           case 2:   return "2";
           case 3:   return "3";
           case 4:   return "4";
           case 5:   return "5";
           case 6:   return "6";
           case 7:   return "7";
           case 8:   return "8";
           case 9:   return "9";
           case 10: return "10";
           case 11: return "Jack";
           case 12: return "Queen";
           case 13: return "King";
           default: return "??";
        }
    }
  
    public String toString()
    {
  
        return getValueAsString() + " of " + getSuitAsString();
    }


}

Hand.Java


import java.util.Vector;

public class Hand
{

   private Vector hand;

   public Hand()
   {
         
      hand = new Vector();
   }

   public void clear()
   {
       
      hand.removeAllElements();
   }

   public void addCard(Card c)
   {
       
      if (c != null)
         hand.addElement(c);
   }

   public void removeCard(Card c)
   {
      
      hand.removeElement(c);
   }

   public void removeCard(int position) {
     
      if (position >= 0 && position < hand.size())
         hand.removeElementAt(position);
   }

   public int getCardCount()
   {
      
      return hand.size();
   }

   public Card getCard(int position)
   {
      
      if (position >= 0 && position < hand.size())
         return (Card)hand.elementAt(position);
      else
         return null;
   }

   public void sortBySuit()
   {
       
      Vector newHand = new Vector();
      while (hand.size() > 0) {
         int pos = 0;
         Card c = (Card)hand.elementAt(0);
         for (int i = 1; i < hand.size(); i++) {
            Card c1 = (Card)hand.elementAt(i);
            if ( c1.getSuit() < c.getSuit() ||
                    (c1.getSuit() == c.getSuit() && c1.getValue() < c.getValue()) )
                    {
                pos = i;
                c = c1;
            }
         }
         hand.removeElementAt(pos);
         newHand.addElement(c);
      }
      hand = newHand;
   }

   public void sortByValue()
   {

      Vector newHand = new Vector();
      while (hand.size() > 0) {
         int pos = 0;
         Card c = (Card)hand.elementAt(0);
         for (int i = 1; i < hand.size(); i++) {
            Card c1 = (Card)hand.elementAt(i);
            if ( c1.getValue() < c.getValue() ||
                    (c1.getValue() == c.getValue() && c1.getSuit() < c.getSuit()) )
                    {
                pos = i;
                c = c1;
            }
         }
         hand.removeElementAt(pos);
         newHand.addElement(c);
      }
      hand = newHand;
   }

}

BlackjackHand.Java

public class BlackjackHand extends Hand
{

     public int getBlackjackValue()
     {
          
         int val;    
         boolean ace;
                    
         int cards;

         val = 0;
         ace = false;
         cards = getCardCount();

         for ( int i = 0; i < cards; i++ ) {
              
             Card card;
             int cardVal;
             card = getCard(i);
             cardVal = card.getValue();
             if (cardVal > 10)
             {
                 cardVal = 10;
             }
             if (cardVal == 1)
             {
                 ace = true;   
             }
             val = val + cardVal;
          }

          if ( ace == true && val + 10 <= 21 )
              val = val + 10;

          return val;

     }
   

}

Deck.Java


public class Deck
{

    private Card[] deck;
    private int cardsUsed;
  
    public Deck()
    {
         
       deck = new Card[52];
       int cardCt = 0;
       for ( int suit = 0; suit <= 3; suit++ )
       {
          for ( int value = 1; value <= 13; value++ )
          {
             deck[cardCt] = new Card(value,suit);
             cardCt++;
          }
       }
       cardsUsed = 0;
    }
  
    public void shuffle() {
        
        for ( int i = 51; i > 0; i-- )
        {
            int rand = (int)(Math.random()*(i+1));
            Card temp = deck[i];
            deck[i] = deck[rand];
            deck[rand] = temp;
        }
        cardsUsed = 0;
    }
  
    public int cardsLeft()
    {
        
        return 52 - cardsUsed;
    }
  
    public Card dealCard()
    {
        
        if (cardsUsed == 52)
           shuffle();
        cardsUsed++;
        return deck[cardsUsed - 1];
    }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote