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

Assignment 5 - Shuffle In this lab you will simulate the playing of a simple car

ID: 3801556 • Letter: A

Question

Assignment 5 - Shuffle

In this lab you will simulate the playing of a simple card game. Start by downloading two starter files: Card.java and Deck.java. The Card class should not be changed. You will add one method to the Deck class.

This assignment should be submitted in two classes, each with a separate code runner box. The first class, Deck, will be a modification of the class we have provided. You will need to implement the shuffleDeck method in order for this class to be accepted.

The second class, Main, will use the Card class and your modified Deck class to create a shuffled Deck object and deal the two hands. The hands should be dealt in alternating order, starting with the first hand. Each hand should have five cards. As the cards are dealt into each hand they should be removed from the deck.

For example, each hand is shown for the following Deck.

Also, all of these cards should be removed from the deck.

After dealing the hand, Main should use the point value of each card to calculate the total point value of each hand. The hand with the highest point value wins. In the case of a draw, the second hand wins. In this game ace = 1, jack = 11, queen = 12, and king = 13. In the deck the card in the first position (index 0) is the top of the deck.

Lastly, Main will declare the winning hand. See the following sample run of Main for the exact format that will be expected by the code runner.

Sample Run of Main:

Hand 1 (total points 22)
Three of clubs (point value = 3)
Two of clubs (point value = 2)
Six of hearts (point value = 6)
Ten of hearts (point value = 10)
Ace of spades (point value = 1)

Hand 2 (total points 27)
Four of spades (point value = 4)
Ten of clubs (point value = 10)
Three of diamonds (point value = 3)
Eight of diamonds (point value = 8)
Two of hearts (point value = 2)

Hand 2 wins!

1. Submit your Deck class here.

NOTE: You MUST use the class name "Deck" for this submission.

2. Submit your Main class here.

NOTE: You MUST use the class name "Main" for this submission.

Hand 1: Hand 2: Seven of spades Queen of spades Ten of spades Eight of spades Three of spades King of hearts Queen of hearts Jack of clubs Four of clubs Eight of clubs

Explanation / Answer

1.    Card.Java

public class Card {

   public final static int SPADES = 0;
   public final static int HEARTS = 1;
   public final static int DIAMONDS = 2;
   public final static int CLUBS = 3;
   public final static int JOKER = 4;

   public final static int ACE = 1;    
   public final static int JACK = 11;  
   public final static int QUEEN = 12;
   public final static int KING = 13;

   private final int suit;  

   private final int value;


   public Card() {
      suit = JOKER;
      value = 1;
   }


   public Card(int theValue, int theSuit) {
      if (theSuit != SPADES && theSuit != HEARTS && theSuit != DIAMONDS &&
            theSuit != CLUBS && theSuit != JOKER)
         throw new IllegalArgumentException("Illegal playing card suit");
      if (theSuit != JOKER && (theValue < 1 || theValue > 13))
         throw new IllegalArgumentException("Illegal playing card value");
      value = theValue;
      suit = theSuit;
   }


   public int getSuit() {
      return suit;
   }

   public int getValue() {
      return value;
   }

}

Deck.java


public class Deck {

    private Card[] deck;
    private int cardsUsed;

    public Deck() {
        this(false);
    }


    public Deck(boolean includeJokers) {
        if (includeJokers)
            deck = new Card[54];
        else
            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++;
            }
        }
        if (includeJokers) {
            deck[52] = new Card(1,Card.JOKER);
            deck[53] = new Card(2,Card.JOKER);
        }
        cardsUsed = 0;
    }


    public void shuffleDeck() {
        for ( int i = deck.length-1; 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 deck.length - cardsUsed;
    }


    public Card dealCard() {
        if (cardsUsed == deck.length)
            throw new IllegalStateException("No cards are left in the deck.");
        cardsUsed++;
        return deck[cardsUsed - 1];
     
    }


}

2.

    Hand.java

import java.util.ArrayList;

public class Hand {

    private ArrayList<Card> hand;


    public Hand() {
        hand = new ArrayList<Card>();
    }


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


    public void addCard(Card c) {
        if (c == null)
            throw new NullPointerException("Can't add a null card to a hand.");
        hand.add(c);
    }


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


    public void removeCard(int position) {
        if (position < 0 || position >= hand.size())
            throw new IllegalArgumentException("Position does not exist in hand: "
                    + position);
        hand.remove(position);
    }


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

    public Card getCard(int position) {
        if (position < 0 || position >= hand.size())
            throw new IllegalArgumentException("Position does not exist in hand: "
                    + position);
        return hand.get(position);
    }

       public int getHandValue() {

        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;

    }

}

Main.java


public class Main {


   public static void main(String[] args) {
       
           Hand hand1=new Hand();
          
           Hand hand2=new Hand();

           Deck deck = new Deck();         
           deck.shuffleDeck();
         
           Card c= new Card();

           for(int i=0;i<5;i++)
           {
             c=deck.dealCard();
             hand1.addCard(c);
             c=deck.dealCard();
             hand2.addCard(c);
           }
       
         
           int count1=Hand1.getHandValue();
           int count2=Hand2.getHandValue();
          
           if(count1>count2)
            System.out.println("Hand1 won the game");
           else
            System.out.println("Hand2 won the game");       

   }
}