Simulating a deck of cards

  • In the real world, a deck of cards looks like this:

  • In this lecture, we will represent a deck of cards using objects

  • We will also study how to implement the following instance methods to:

      • Shuffle a deck of cards
      • Deal a card from a deck of cards

Defining a the Card class to represent a card

  • A card has the following properties:

     A suit (e.g.: Spades, Hearts, Diamond, Clubs)
     A rank (e.g.: Ace, 2, 3, ..., 9, 10, Jack, Queen, King)

  • A card has the following actions:

      • None

  • Therefore, the skeleton code for Card class that represents a card is:

    public class Card
    {
        private String suit;
        private String rank;
    
        Card(...) // Constructor to create a Card object
        {
            ...
        }
    }

Defining a the Card class to represent a card

  • We want the ability to create a Card with a certain suit and rank:

    public class Card
    {
        private String suit;
        private String rank;
    
        public Card(String cardSuit, String cardRank)
        {
           // Initialize the Card with:
           //     suit = cardSuit  and rank = cardRank
        }
    
    }

    Let's write the constructor...

Defining a the Card class to represent a card

  • The Card class that represents a card is:

    public class Card
    {
        private String suit;
        private String rank;
    
        public Card(String cardSuit, String cardRank)
        {
            suit = cardSuit;
            rank = cardRank;
        }
    
    }

    Let's create some Card objects and print them out...

How to use the Card class to make card objects

Sample program that uses the Card class:

public class myProg
{
    public static void main(String[] args)
    {
        Card c1 = new Card("Spades", "Ace");
        Card c2 = new Card("Hearts", "10");
        
        System.out.println( c1 );
        System.out.println( c2 );
    } 
}

DEMO: demo/03-classes/12-card/


You will see output like this:   Card@c72318e

I like to show you how to make the System.out.println( ) method output look nicer first...

The toString( ) method to return a String representation of objects

  • The System.out.println(object) call will always print the output of object.toString()

  • To control how an object will be printed out, define a String toString( ) that return a String with the object's information

  • Example:   print a Card object as "Ace of Spades":

    public class Card
    {
        private String suit;
        private String rank;
    
        public Card(String cardSuit, String cardRank)
        {
            // Left out for brevity
        }
    
        // Define the toString() method to control object print out
        public String toString() // Used to print an object
        {
            return rank + " of " + suit;
        }
    }

How to use the Card class to make card objects

Run the same program that uses the Card class:

public class myProg
{
    public static void main(String[] args)
    {
        Card c1 = new Card("Spades", "Ace");
        Card c2 = new Card("Hearts", "10");
        
        System.out.println( c1 );
        System.out.println( c2 );
    } 
}

DEMO: demo/03-classes/13-card

The output is now:   Ace of Spades !

Representing a deck of card

  • A computer program cannot replace a real deck of cards:

                

  • But, a computer program can imitate the things you can do with a deck of cards

  • We will imitate these "things that you can do" with a deck of cards:

      • Shuffle a deck of cards

      • Deal a card from a deck of cards

We must first define a class that represents a deck of cards...

Defining a the DeckOfCards class to represent a deck of cards

  • A deck of cards has the following properties:

      52 playing cards consisting of:
    
          Ace of Spades,   2 of Spades,   ...., King of Spades
          Ace of Hearts,   2 of Hearts,   ...., King of Hearts
          Ace of Diamonds, 2 of Diamonds, ...., King of Diamonds
          Ace of Clubs,    2 of Clubs,    ...., King of Clubs

  • A deck of cards can perform the following actions:

      shuffle( ): re-arranges the cards randomly
                  After shuffle( ), all cards are available
    	      for dealing                              
                                       
      deal( ):    deals out the top of the card in the deck
                  A card that is dealt cannot be dealt again

Scheletal class of the DeckOfCard class

  • From the information in the previous slide, the for DeckOfCards class must contain the following members:

    public class DeckOfCards
    {
        private Card[] deck;  // Array hold 52 cards (to be created)
    
        DeckOfCards()  // Initialize a deck of 52 cards
        {
           ....
        }
    
        public void shuffle() // Shuffle this deck of cards
        {
           perform a shuffle on the deck of cards   
        }
        
        public Card deal() // Deal the next card from this deck
        {
           returns the next card in deck
        }
    }  

Defining a the DeckOfCards class to represent a deck of cards

  • Representing the 52 cards in a deck:

      52 different cards:
    
                   
     
      Use: array of Card objects:
    
                   

Defining a the DeckOfCards class to represent a deck of cards

  • Representing the card shuffle action of a deck of cards:

      shuffle( ):
    
                   
     
      Use: random shuffle algorithm:
    
                   

Defining a the DeckOfCards class to represent a deck of cards

  • Representing the "deal a card" action of a deck of cards:

      deal( ): deal the next card from the deck
    
                   
     
      Use: variable dealPosition to mark the top position in deck:
    
                   

Defining a the DeckOfCards class to represent a deck of cards

Based on the properties and actions, the skeleton code for the DeckOfCards class is as follows:

public class DeckOfCards
{
    private Card[] deck;  // Hold 52 cards (to be created)
    private int    dealPosition;  // Location of the top card in deck

    DeckOfCards()  // Constructor to make a deck of cards
    {
       ....
    }

    public void shuffle() // Shuffle this deck of cards
    {
       perform a shuffle on the deck of cards   
    }
    
    public Card deal() // Deal the next card from this deck
    {
       returns the dealt card
    }
}  

The constructor of the DeckOfCards class

Constructor:   making a deck of cards with 52 specific cards:   (naive approach)

public class DeckOfCards
{
    private Card[] deck;
    private int    dealPosition;

    DeckOfCards()  // Constructor to make a deck of cards
    {
        deck = new Card[52];    // Make a holder for 52 cards

        deck[0] = new Card( "Spades", "Ace");
        deck[1] = new Card( "Spades", "2");
        ...
        and so on 
	(52 assignment statements !!!)  
        (How can we code this easily ??)
    }

    ...
}  

The constructor of the DeckOfCards class

Constructor:   making a deck of cards with 52 specific cards:

public class DeckOfCards
{
    private Card[] deck;
    private int    dealPosition;

    DeckOfCards()  // Constructor to make a deck of cards
    {
        deck = new Card[52];    // Make a holder for 52 cards
        // Define 2 arrays with all the suits and ranks
        String[] suit = {"Spades", "Hearts", "Diamonds", "Clubs"};
        String[] rank = {"Ace", "2", "3", "4", "5", "6", "7", "8",
                         "9", "10", "Jack", "Queen", "King"};


 
       
        
         
        
       
    }
}  

The constructor of the DeckOfCards class

Constructor:   making a deck of cards with 52 specific cards:

public class DeckOfCards
{
    private Card[] deck;
    private int    dealPosition;

    DeckOfCards()  // Constructor to make a deck of cards
    {
        deck = new Card[52];    // Make a holder for 52 cards

        String[] suit = {"Spades", "Hearts", "Diamonds", "Clubs"};
        String[] rank = {"Ace", "2", "3", "4", "5", "6", "7", "8",
                         "9", "10", "Jack", "Queen", "King"};
        int k = 0;   // Index into deck[ ]
        // Use a nested loop to make all 52 cards and assign to deck[ ]
        for ( int i = 0; i < suit.length; i++ )
            for (int j = 0; j < rank.length; j++ )
            {
                deck[k] = new Card( suit[i], rank[j] );
                k++;
            }
    }
}  

Prelude to shuffle()    swap value in 2 variables

Before we write the shuffle() method, we need to discuss how to swap 2 variables:

public class MyProg
{
   public static void main(String[] args)
   {
       int a = 1, b = 2;

       

       // Code to swap values in a and b


       // End result: a = 2, b = 1
   }
}
  

Prelude to shuffle()    swap value in 2 variables

Analogy to illustrate the swap programming technique:

 

 

Prelude to shuffle()    swap value in 2 variables

The algorithm used to exchange the values in the variables a and b:

public class MyProg
{
   public static void main(String[] args)
   {
       int a = 1, b = 2;

       int help;       

       // Code to swap values in a and b
       help = a;
       a = b;
       b = help;
   }
}
  

The shuffle() method of the DeckOfCards class

The shuffle() method will re-arrange the cards randomly and allow all cards to be dealt:

public class DeckOfCards
{
    private Card[] deck;
    private int    dealPosition;

    DeckOfCards() ...  // Done 

    public void shuffle()  // Shuffle this deck of cards
    { 
        for (int i = 0; i < deck.length; i++) 
        { // Generate an index randomly
            int j = (int)(Math.random() * deck.length);

            Card temp = deck[i];   // Swap
            deck[i] = deck[j];
            deck[j] = temp;
        }
        
        dealPosition = 0;
    }
} 

The shuffle() method of the DeckOfCards class

Generate random numbers j and swap card[i] and card[j]:

public class DeckOfCards
{
    private Card[] deck;
    private int    dealPosition;

    DeckOfCards() ...  // Done 

    public void shuffle()  // Shuffle this deck of cards
    {
        for (int i = 0; i < deck.length; i++) 
        {  // Generate an index j randomly
            int j = (int)(Math.random() * deck.length);

            Card temp = deck[i];   // Swap card[i] and card[j]
            deck[i] = deck[j];
            deck[j] = temp;
        }
         
        dealPosition = 0;
    }
} 

The shuffle() method of the DeckOfCards class

Reset a dealPosition variable that marks the index of the next card that will be dealt:

public class DeckOfCards
{
    private Card[] deck;
    private int    dealPosition; // Marks the position of next card

    DeckOfCards() ...  // Done 

    public void shuffle()  // Shuffle this deck of cards
    {
        for (int i = 0; i < deck.length; i++) 
        {  // Generate an index j randomly
            int j = (int)(Math.random() * deck.length);

            Card temp = deck[i];   // Swap card[i] and card[j]
            deck[i] = deck[j];
            deck[j] = temp;
        }
       
        dealPosition = 0; // Reset the next deal card position
    }
} 

The deal() method of the DeckOfCards class

The deal() method will deal out (= returns) the next card in the deck of cards

public class DeckOfCards
{
    private Card[] deck;
    private int    dealPosition; // Marks the position of next card

    DeckOfCards() ...  // Done 

    public void shuffle() ...  // Done 

    public Card deal()  // deals (= returns) the next card in this deck
    {
        Card nextCard;

        
        
           
            
        
       
           

        return nextCard;
    }
} 

The deal() method of the DeckOfCards class

If the deck has more cards, save the next card in nextCard and advance dealPosition:

public class DeckOfCards
{
    private Card[] deck;
    private int    dealPosition; // Marks the position of next card

    DeckOfCards() ...  // Done 

    public void shuffle() ...  // Done 

    public Card deal()  // deals (= returns) the next card in this deck
    {
        Card nextCard;

        if ( dealPosition < deck.length )
        {
            nextCard = deck[dealPosition];
            dealPosition++;
        }
      
           

        return nextCard;
    }
} 

The deal() method of the DeckOfCards class

Otherwise: return null or we can throw an exception:

public class DeckOfCards
{
    private Card[] deck;
    private int    dealPosition; // Marks the position of next card

    DeckOfCards() ...  // Done 

    public void shuffle() ...  // Done 

    public Card deal()  // deals (= returns) the next card in this deck
    {
        Card nextCard;

        if ( dealPosition < deck.length )
        {
            nextCard = deck[dealPosition];
            dealPosition++;
        }
        else
            nextCard = null;    // Alternately: throw exception

        return nextCard;
    }
} 

The deal() method of the DeckOfCards class

Return the saved next card:

public class DeckOfCards
{
    private Card[] deck;
    private int    dealPosition; // Marks the position of next card

    DeckOfCards() ...  // Done 

    public void shuffle() ...  // Done 

    public Card deal()  // deals (= returns) the next card in this deck
    {
        Card nextCard;

        if ( dealPosition < deck.length )
        {
            nextCard = deck[dealPosition];
            dealPosition++;
        }
        else
            nextCard = null;    // Alternately: throw exception

        return nextCard;
    }
} 

How to use the DeckOfCards class

This Java program shows how to create one deck of cards and deal out some cards:

public class myProg
{
    public static void main(String[] args)
    {
        DeckOfCards d = new DeckOfCards();   // Create 1 deck of cards
        
        for ( int i = 0; i < 53; i++ )
            System.out.println( d.deal() );  // Deal out 53 cards
        System.out.println();
            
        d.shuffle();                         // Shuffle the deck d
        for ( int i = 0; i < 5; i++ )
            System.out.println( d.deal() );  // Deal out 5 cards after shuffle
    } 
} 

DEMO: demo/03-classes/14-card

We can use the DeckOfCards class to create any number of decks of cards
The DeckOfCards class will help you write Java program that play card games !!!