Wednesday, April 11, 2012

Lab 24: Cards


For this lab you will implement two classes, a Card and a Hand class.

The Card represt a card, which has a number and a Suit, there is also a Joker. The toString() method of this class will return the card's name in English, as shown below.

The Hand class uses a HashSet<Card> to hold a set of cards, which starts out empty. It has an add(Card c) which adds Card c to the hand. It has a methods makeFullDeck() which populates the hand with a full deck of cards (one Joker).

Finally, you will implement a public static HashSet<Hand> getAllHands(int numCards, Hand h) which returns the set of all the hands of numCards cards which can be created using the cards in hand h. You can do this the following recursive algorithm:
getAllHands(int numCards, Hand h)
  if numCards is 1
      then return the set of hands of just 1 card from h  
  hand = h.clone()
  result = empty set of hands
  foreach card in hand:
      remove card from hand
      allOtherHands =  getAllHands(numCards - 1, hand)
      add card to every hand in allOtherHands
      add allOtherHands to result
  return result

For example, the following main
public static void main(String[] args){
        Card twoClubs = new Card(2,Card.Suit.Clubs);
        System.out.println(twoClubs);
        
        Card oneSpades = new Card(1,Card.Suit.Spades);
        System.out.println(oneSpades);
        
        Card joker = new Card(0,Card.Suit.Hearts); //0 is the Joker 
        System.out.println(joker);
        
        Card kingDiamonds = new Card(13, Card.Suit.Diamonds); 
        System.out.println(kingDiamonds);
        
        Hand mine = new Hand();
        mine.add(twoClubs);
        mine.add(oneSpades);
        mine.add(joker); //0 is Joker
        mine.add(kingDiamonds);
        
        System.out.println("My hand is " + mine);

        //All the possible ways to pick 2 cards from mine
        System.out.println("All the possible ways to pick 2 cards from my hand are:");
        HashSet<Hand> pickTwo = getAllHands(2, mine);
        for (Hand hand : pickTwo) {
            System.out.println(hand);
        }
        System.out.println("Thus, there are " + pickTwo.size() + " possible ways.");
        System.out.println("");
        
        //All the possible ways to pick 3 cards from mine
        System.out.println("All the possible ways to pick 3 cards from my hand are:");
        HashSet<Hand> pickThree = getAllHands(3, mine);
        for (Hand hand : pickThree) {
            System.out.println(hand);
        }
        System.out.println("Thus, there are " + pickThree.size() + " possible ways.");
        System.out.println("");
        

        Hand deck = new Hand();
        deck.makeFullDeck(); //makes a full deck, with 1 Joker.
        System.out.println("The full deck is:");
        System.out.println(deck);
        
        System.out.println("All the possible ways to pick 2 cards from a full duck are:");
        HashSet<Hand> a = getAllHands(2, deck); //this will take a few seconds to run
        for (Hand hand : a) { //and a minute to print
            System.out.println(hand);
        }
        System.out.println("Thus, there are " + a.size() + " possible ways.");
        
        System.out.println("All the possible ways to pick 4 cards from a full duck are:");
        a = getAllHands(4, deck); //this will take a few seconds to run
        for (Hand hand : a) { //and a minute to print
            System.out.println(hand);
        }
        System.out.println("Thus, there are " + a.size() + " possible ways.");
    }

Should print out:
2 of Clubs
Ace of Spades
Joker
King of Diamonds
My hand is (Ace of Spades, King of Diamonds, 2 of Clubs, Joker)
All the possible ways to pick 2 cards from my hand are:
(Ace of Spades, 2 of Clubs)
(King of Diamonds, 2 of Clubs)
(Ace of Spades, King of Diamonds)
(King of Diamonds, Joker)
(Ace of Spades, Joker)
(2 of Clubs, Joker)
Thus, there are 6 possible ways.

All the possible ways to pick 3 cards from my hand are:
(Ace of Spades, King of Diamonds, Joker)
(Ace of Spades, King of Diamonds, 2 of Clubs)
(Ace of Spades, 2 of Clubs, Joker)
(King of Diamonds, 2 of Clubs, Joker)
Thus, there are 4 possible ways.

The full deck is:
(4 of Spades, 5 of Clubs, Queen of Spades, 8 of Spades, 8 of Clubs, Jack of Spades, Ace of Diamonds, Ace of Hearts, Queen of Clubs, 10 of Clubs, 3 of Spades, Queen of Diamonds, King of Spades, Ace of Spades, 6 of Diamonds, 9 of Clubs, King of Hearts, 7 of Clubs, 9 of Hearts, 4 of Hearts, Jack of Hearts, 3 of Diamonds, 4 of Diamonds, 10 of Hearts, Jack of Diamonds, 5 of Spades, Joker, 2 of Hearts, 9 of Diamonds, Ace of Clubs, 3 of Clubs, Jack of Clubs, King of Clubs, 5 of Diamonds, 10 of Diamonds, 7 of Diamonds, 8 of Hearts, King of Diamonds, 6 of Hearts, 9 of Spades, 7 of Spades, 6 of Clubs, 4 of Clubs, 3 of Hearts, 2 of Diamonds, 10 of Spades, 8 of Diamonds, 2 of Clubs, 6 of Spades, 5 of Hearts, Queen of Hearts, 2 of Spades, 7 of Hearts)
All the possible ways to pick 2 cards from a full duck are:
(Queen of Spades, Ace of Spades)
(Jack of Clubs, 5 of Hearts)
(2 of Diamonds, 5 of Clubs)
...and so on...
(King of Hearts, 3 of Diamonds)
(Jack of Clubs, 7 of Hearts)
(5 of Diamonds, Ace of Clubs)
Thus, there are 1378 possible ways.

All the possible ways to pick 4 cards from a full duck are:
(10 of Spades, 5 of Diamonds, Jack of Diamonds, Ace of Hearts)
(10 of Spades, 9 of Spades, Jack of Hearts, 7 of Diamonds)
(2 of Clubs, 7 of Hearts, Jack of Diamonds, 4 of Diamonds)
...and so on...
(6 of Diamonds, 9 of Diamonds, Ace of Hearts, 9 of Clubs)
(6 of Diamonds, 8 of Hearts, 7 of Clubs, King of Spades)
(King of Diamonds, 9 of Hearts, Jack of Hearts, 5 of Spades)
(Jack of Clubs, 10 of Hearts, 9 of Diamonds, 4 of Clubs)
(2 of Clubs, 4 of Spades, 4 of Clubs, 9 of Clubs)
Thus, there are 292825 possible ways.

Getting the recursive algorithm working might take you longer than the allotted lab time, so you can turn in this lab within 48 hours of the end time of your lab, in the dropbox.cse.sc.edu.

Hint: You will probably need to use an Iterator, because you need to Iterator.remove().

No comments: