Texas Hold’em Poker in Java: Part 1 -Cards, Decks and Hands

Poker offers a very interesting coding challenge in terms of sorting, shuffling and comparing objects that we make. I will show how to create all the classes and code necessary to simulate poker, and in later blog posts will do some interesting analytics with the system we create.

Full project code can be found here https://github.com/MBlogs/Poker-in-Java.

I will only include important code snippets in this and subsequent posts.


Similar to when we coded important parts of Blackjack, we need a class to manage cards. A simple class that stores values and suits will do the trick:

public class Card{
	private int value;
	private int suit;
	public Card(int v, int s){
		value = v;
		suit = s;
        public int Value(){return value;}
        public int Suit(){return suit;}


I mentioned in an earlier blog post the features of a Fisher-Yates shuffle, which will help us make our deck of cards:

public class Deck {
    private Card[] deck;
    public Deck(){
        deck = shuffleDeck(populateDeck());
    private Card[] populateDeck(){
         Card[] d = new Card[4*13];
         for(int s = 0; s < 4; s++) {
             for(int v = 2; v < 13+2; v++) {                  d[s*13 + (v-2)] = new Card(v, s);              }          }          return d;     }       private Card[] shuffleDeck(Card[] d){         Random random = new Random();         for(int n = d.length-1; n > 0; n--){
            int i = random.nextInt(n+1);
            Card temp = d[i];
            d[i] = d[n];
            d[n] = temp;
        return d;
    public Card GetCard(int i){
        return deck[i];

The populate deck function fills up the deck with all valid cards. We choose to denote values 2 through to the Ace with numbers (values) 2 through to 14. The suits are arbitrary 0 – 3, although in the full code I include functions to pass back the suits as follows: 0 = spades, 1 = clubs, 2 = hearts, 3 = diamonds.

Shuffle deck implements a Fisher-Yates shuffle to reorder the deck. This is an efficient and fair shuffle and I talked about it in an earlier post.


This is where things get interesting. When first embarking on this project, I knew the main difficulty with coding poker is capturing everything needed to identify and compare two poker hands. Most people will be familiar with the following hand ranking:


A naive approach would be to think that one only needs to store the hand rank. However what if two players both have a straight? Well, the person with the higher straight wins. What about a full house against a full house? Well, the person with the higher triple wins. But what if both players have the same triple? Well, then the person with the highest pair to go with it wins. And high card can be even worse. If both players have the same 4 highest cards, a player can still win with their fifth card being higher than their opponents.

It’s clear that we actually need to know two things:

  1. The hand rank
  2. The intra-hand strength, i.e kickers associated with the hand

In poker terminology, a kicker means something a little different but for our purposes we’ll call kickers anything about a hand we can use to compare with another of the same rank.

For those unfamiliar with poker, here’s a run down of the kickers we’ll need to store for each hand rank. A poker hand consists of five cards, so we compare two hands of same rank as much as possible for those five cards. However, if two hands of same rank draw on each criteria mentioned, they are equal in strength and will result in a tie or “chopped” pot.

  • High card: 1. Highest card. 2. Second highest card. 3. Third highest card. 4. Fourth highest card. 5. Fifth highest card
  • Pair: 1. The pair value. 2. Highest other card. 3. Second highest other card 4. Third highest other card
  • Two pair: 1. Higher value pair 2. Lower value pair 3. Highest other card.
  • Three of a Kind: 1. Card value for which the player has three. 2. Highest other card. 3. Second highest other card.
  • Straight: Highest value card in the straight
  • Flush: 1. Highest value card of the flush suit. 2. Second highest value card of the flush suit. 3. Third highest value card of the flush suit. 4. Fourth highest value card of the flush suit. 5. Fifth highest value card of the flush suit.
  • Full House: 1. Card value for which the player has three. 2. Card value for which the player has two.
  • Four of a Kind: 1. Card value for which the player has four. 2. Highest other card
  • Straight Flush: 1. Highest value in the straight flush

So we need to store at most 5 kickers associated with a hand to know we can fully compare one hand against another.

Our final complication is that in Texas Hold’em poker, players are given a total of 7 cards to then “make” the best 5 card hand possible. We don’t need to think about this yet, but when we make the formula to identify hands, it will need to take this into account and find the best out of all 5 card combinations of the 7 available.

public class Hand {   
    private int rank;
    private int[] kickers = new int[] {0,0,0,0,0};;
    public int Rank(){ return rank; }
    public int Kicker(int i){return kickers[i];}
    public void SetRank(int r){rank = r;}
    public void SetKickers(int[] k){
        kickers=new int[]{0,0,0,0,0};
        for(int i=0;i< k.length; i++){
            kickers[i] = k[i];

Now we’re ready…

We have the infrastructure in place to identify, store and compare poker hands, which we’ll do in the next installment.

One thought on “Texas Hold’em Poker in Java: Part 1 -Cards, Decks and Hands

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s