Jump to content

Check out our Community Blogs

Register and join over 40,000 other developers!

Recent Status Updates

View All Updates

- - - - -

A Case Study: Minimal Poker Game

java case study project

  • Please log in to reply
No replies to this topic

#1 mdebnath


    CC Newcomer

  • Member
  • PipPip
  • 17 posts
  • Programming Language:C, Java, C++
  • Learning:C, Java, C++

Posted 17 February 2013 - 03:11 AM

Lets try a full fledged Java application to simulate a very simple card game. The application uses random number generation and an array of reference type elements to develop a class that simulates card shuffling, dealing and playing. This class can then be used to implement application that play specific card games, lets say a minimal poker.


Problem description

A card game simulator java application to deal say, five poker hand which on run simulates the play that determine whether a hand contains faces such as - a pair, two of a kind, three of a kind, four of a kind, a flush( all five cards of a same suit).


How do we approach


We first develop class Card that represents a playing card that has a face (such as, “Ace”, “2”, ”3”,...”Jack”, “Queen”, “King”) and a suit (such as, “Spades”, ”Diamonds”, “Hearts”, “Clubs”). Then we develop a Deck class which creates a deck of 52 playing cards in which each element is a Card object. Next, we develop Player class. Each player is dealt five cards upon shuffle from the deck of 52 cards. From the Game class cards are shuffled, players are initialized and ready to roll. GameApp is the Java application class to begin the simulation.


Class Card

Class Card contains two String instance variables – face and suit, that are used to store references to the face name and suit name for a specific card. The constructor for the class receives two String that is uses to initialize face and suit. Method toString creates a String consisting of the face of the card, the String “of” and the suit of the card. Apart from the constructor initialization, there are distinct setter method (setFace and setSuit) for faces and suit respectively to initialize each of them. Similarly, there are getter method (getFace and getSuit) to get the value of the attribute, face and suit.

//Card class represents a playing card
package org.game.cards;
public class Card {
    private String face;
    private String suit;
    //two-argument constructor initializes Cards face and suit
    public Card(String face, String suit) {
        this.face = face;
        this.suit = suit;
    //getter method to return the face value
    public String getFace() {
        return face;
    //setter method to initialize the face
    public void setFace(String face) {
        this.face = face;
    //getter method to return the suit value
    public String getSuit() {
        return suit;
    //setter method to initialize the suit
    public void setSuit(String suit) {
        this.suit = suit;
    //return String representation of Card object
    public String toString() {
        return face + " of " + suit;
}//end of class

Class Deck


The class Deck declares an instance variable array name deck of Card objects. Like primitive type array declaration, the declaration of an array of objects includes the type of the elements in the array, followed by the name of the array variable and square brackets. Class Deck also declares an integer constant TOTAL_CARDS indicating the number of cards in the deck (which is 52). The class's constructor instantiates the deck array to be of size TOTAL_CARDS. When first created, the elements of the deck array are null by default, so the constructor uses a for statement to fill the deck[] with Cards. Each Card is instantiated and initialized with two String – faces and the suits. The calculation i%13
always results a value from 0 to 12, and the calculation i/13 always results in a value from 0 to 3.

The method shuffle shuffles the cards in the deck. For each card, a number between 0 and 51 is picked randomly to select another Card. Next, the current Card object and the randomly selected Card object are swapped in the array. This gives the effect of shuffling of cards.

//Card class represents a deck of cards
package org.game.cards;
import java.util.Random;
public class Deck {
    private final String faces[] = {"Ace","2","3","4","5","6","7","8","9","10","Jack","Queen","King"};
    private final String suits[]={"Hearts","Diamonds","Clubs","Spades"};
    private Card deck[];
    private final int TOTAL_CARDS=52;
    private Random randNum;    
    //no-argument constructor fills the deck of cards
    public Deck(){        
        deck = new Card[TOTAL_CARDS];
        randNum = new Random();
        for(int i=0;i<deck.length;i++){
            deck[i] = new Card(faces[i%13],suits[i/13]);
    //shuffles the deck
    public void shuffle(){
            int j = randNum.nextInt(TOTAL_CARDS);
            Card c = deck[i];
            deck[i] = deck[j];
            deck[j] = c;

    //returns the individual card in the deck
    public Card getCard(int index){
        return deck[index];
}//end of class

Class Player


Class Player declares an instance variable array name deck of Card objects. Class Player also declares an integer constant MAX_CARD indicating the number of cards in each hand (which is 5). The methods getCardAtIndex and setCardAtIndex is used to get the Card from the player's hand and set the dealt Card into the hand of the player respectively. CountPair simple counts how many pair of faces are there in a Player's hand. The method isFlush checks if all five cards are of same suit or not.


//class to represent a player
package org.game.cards;
public class Player {
    public final static int MAX_CARD = 5;
    private Card cards[];
    //constructor initializes 5 cards in each hand
    public Player() {
        cards = new Card[MAX_CARD];
    //returns all the cards in hand
    public Card[] getCards() {
        return cards;
    //get the cards at a particular position
    public Card getCardAtIndex(int index) {
        if (index >= 0 && index < MAX_CARD)
            return cards[index];
            return null;
   //sets the card at particular position
    public void setCardAtIndex(Card c, int index) {
        if(index >= 0 && index < MAX_CARD)
            cards[index] = c;
    //counts number of matched pair
    public int countPair() {
        int count = 0;
        for (int i = 0; i < cards.length; i++) {
            for (int j = i + 1; j < cards.length; j++) {
                if (cards[i].getFace().equals(cards[j].getFace())){
        return count;

    //checks if it is a flush or not i.e all five cards of same suit
    public boolean isFlush() {
        int count = 0;
        for (int i = 0; i < cards.length; i++) {
            for (int j = i + 1; j < cards.length; j++) {
                if (cards[i].getSuit().equals(cards[j].getSuit())) {
        if(count == 5)
            return true;
            return false;

Class Game


Class Game declares two instance variables. One an array named Players another indicating Deck of cards. The constructor of the Game creates the Deck object and initializes four player and subsequently shuffles the deck. Now, details of the game is laid out.


//class represents the minimal poker game
package org.game.cards;
public class Game {
    private Player[] players;
    private Deck deck;
    //constructor initializes the deck and cards
    public Game() {
        deck = new Deck();
        players = new Player[4];
        players[0] = new Player();
        players[1] = new Player();
        players[2] = new Player();
        players[3] = new Player();
    //deals the card to each player
    public void dealCards() {
        int count = 0;
        for (int i = 0; i < players[0].getCards().length; i++) {
            for (int j = 0; j < players.length; j++) {
                players[j].setCardAtIndex(deck.getCard(count++), i);
    //simulates the game and shows the result
    public void showCards() {
        for (int i = 0; i < players.length; i++) {
            System.out.print("Player " + (i + 1) + ": ");
            for (int j = 0; j < players[0].getCards().length; j++) {
                System.out.print("{" + players[i].getCardAtIndex(j).toString()+"} ");
            if(players[i].countPair()> 0)
                System.out.print("\nPAIR(S):" + players[i].countPair()+ "! ");
}//end of class

Class GameApp


Class GameApp is the Java application which contains the main function. The simulator application runs from here.

//class represents java application
package org.game.cards;
public class GameApp {
    public static void main(String[] args) {        
        Game game = new Game();
    }//end of main
}//end of class

Modify the program by adding the following methods.

  1. IsStraight() (to find all five cards of consecutive face values)   

  2. isFullHouse() (to find two cards of one face value and three cards of another face value)

Previous: Introduction to classes and objects | Next: | Back to Table of content

Edited by mdebnath, 19 February 2013 - 07:26 PM.

  • 0

Also tagged with one or more of these keywords: java case study, project

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download