.
.


Card Game - Dealing Cards, Searching for 3-of-a-kind

  Download Source Code

/**************************************************
A card game can be SIMULATED by a computer program.  
We must decide how to REPRESENT a single card, 
as well as how to STORE a whole deck of cards.
This program stores each card as an int number, like this:
  card = rank * 10 + suit , e.g. 114 = Jack of Spades, 31 = 3 of Clubs
where rank is between 2 and 14, and suit is 1,2,3 or 4.  
The deck is an ARRAY of ints.  The shuffle method mixes up
the cards. The takeCard method returns the first card (deck[0]) 
and then moves each card up one position.
The program shuffles a new deck and then "deals" a hand of 5 cards.
It checks whether the hand contains "3 of a kind" - that is,
3 cards with the same rank.  It repeats 1000 times and counts
the number of times that 3 of a kind occurs.  The result of about
20-254 occurences in 1000 hands gives an idea of the probability
of dealing a hand with 3 of a kind.
***************************************************/

public class CardGame
{
   int[] deck = new int[52];
   int[] hand = new int[5];

   int last = 0;
     
   public CardGame()
   {
     int count=0;
     for(int x=0; x < 1000; x=x+1)      // dealing 1000 hands
     {
        makeCards();                    // create 52 cards
        shuffle();                      // shuffle the deck
        hand = take5();                 // deal a 5-card hand

        if(threeKind(hand)==true)       // check for 3-of-a-kind
        {  
           count++;                     // count successful hands
           System.out.print(x + " : " );           // print successful hand    
           printHand(hand);   
        }
     }
     System.out.println(count);
   }   
   
   void makeCards()                     // make all 52 cards
   {
      int pos = 0;
      for(int suit = 1; suit <= 4; suit = suit + 1)     // all 4 suits
      {
         for(int rank = 2; rank <= 14; rank = rank + 1) // all ranks
         {
             deck[pos] = 10*rank + suit;
             pos++;
         }       
      } 
      last = 51;
   }
   
   void shuffle()                
   // shuffle the deck by selecting 2 positions at random
   // and then switching these 2 card codes, repeat 1000 times
   {
      for(int x = 0; x < 1000; x++)
      {
         int a = (int)(Math.random()*last);
         int b = (int)(Math.random()*last);
         int temp = deck[a];
         deck[a] = deck[b];
         deck[b] = temp;
      }
   }
   
   String cardName(int c)        // produce a 2-character code for a card
   {
      String[] suits = { "?", "c", "d", "h", "s" };
      String[] ranks = { "*", "*", "2", "3", "4", "5", "6", "7",
"8", "9", "T", "J", "Q", "K", "A"}; int suit = getSuit(c); int rank = getRank(c); return ranks[rank] + suits[suit]; } int getRank(int c) // extract rand from int c { return c / 10; } int getSuit(int c) // extract the suit from int c { return c % 10; } int takeCard() // returns the card from deck[0] // then moves each card up one position // and subtracts 1 from LAST { int card = deck[0]; for(int c = 0; c < last; c = c+1) { deck[c] = deck[c+1]; } deck[last] = 0; last = last - 1; return card; } int[] take5() // take the first 5 cards from deck { int[] result = new int[5]; for(int x = 0; x < 5; x = x+1) { result[x] = takeCard(); } return result; } void printHand(int[] hand) // print the 5 cards from hand[] { for(int c = 0; c < 5; c=c+1) { System.out.print(cardName(hand[c])+" "); } System.out.println("\n------------------------"); } boolean threeKind(int[] hand) // check for 3-of-a-kind in hand[] { int a = getRank(hand[0]); int b = getRank(hand[1]); int c = getRank(hand[2]); int d = getRank(hand[3]); int e = getRank(hand[4]); if( a==b && a==c || a==b && a==d || a==b && a==e || a==c && a==d || a==c && a==e || a==d && a==e || b==c && b==d || b==c && b==e || b==d && b==e || c==d && c==e ) { return true; } else { return false; } } public static void main(String[] args) { new CardGame(); } }

Simulation

A simulation is a program (or computer system) that attempts to behave realistically.  The purpose of a simulation
is to create a system where we can do experiments and check the results.  This is hopefully faster or easier or safer
that do the experiments in reality.  You have probably used simulations - a video game is usually a simulation
of some realistic environment.  The biggest difference between games and simulations is that simulations have
a serious, scientific purpose, whereas the main purpose of a game is to have fun.  Simulations must be as
realistic as possible.

Simulations must do two major things:

Most data is represented as a measurement, so representing data is accurately is usually simple enough. 
Representing behaviors is generally a lot more difficult.  Behaviors might involve natural laws. 
Even worse, behaviors might represent human preferences and decisions, and these are very difficult to simulate.

Cards

This program does a relatively simple simulation - it simulates a card game with a standard deck of 52 cards.

The first question is how to represent the data.  In this case, how should we represent the cards? 
Here are some possibilities:

The integer solution is attractive because it is simple, but it makes it difficult to determine the suit or the rank.
For example, when looking for a pair, which numbers match with which?

The author decided to do it this way:

So  the Ace of Spades is  144  =  14*10 + 4.

Having cards stored as simple int values makes it easy to manipulate them, for things like shuffling and dealing.
We can still display text by converting the number to the corresponding text - done by the cardName method.

Behaviors

The behaviors for cards are things like moving around in the array when we shuffle, or moving from
the deck into a hand when we deal.

Shuffling is done by choosing two random cards and then exchanging their positions.  This is not the
way that "real" shuffling actually works.  But we hope it is equivalent and arranges the cards in a random order.

The program creates a new deck, shuffles it, and then deals the top 5 cards into a "hand".  It checks whether there
are 3-of-a-kind, that is 3 cards with the same rank.  Then it does this all again with a new deck, shuffling and taking
the top 5 cards.  It repeats this 1000 times and counts how often it gets 3-of-a-kind. This behavior is not realistic -
 no real human being would do this.  But it is hopefully equivalent to what happens in reality, producing a realistic
estimate of the probability of dealing a hand with 3-of-a-kind.

Programming Practice