Creating a Poker game in Unity: Part 1(Basics)

Poker is a popular card game that has been enjoyed by players worldwide for centuries. It combines elements of skill, strategy, and luck, making it an engaging and competitive experience.

We have divided the whole process into a series of articles, due to the complexity of the logic. In this article, we will cover the basic rules and mechanics of poker, creating a deck of cards, shuffling the deck, dealing cards to players. In the subsequent articles, we will see how to implement betting rounds, determine the winner and how to create UI for it.

Poker is played both as a fun game and a serious one with real money involved. If you are developing a real money online poker, then please check the rules and regulations in your country.

Poker is a betting game where players wager on the strength of their hands. The objective is to either have the best hand at the showdown or convince other players to fold, thereby winning the pot. To develop a poker game, you must first familiarize yourself with the basic rules and mechanics:

Poker Hand Rankings:

  1. Royal Flush: The highest hand in poker, consisting of Ace, King, Queen, Jack, and Ten of the same suits.
  2. Straight Flush: Five consecutive cards of the same suit.
  3. Four of a Kind: Four cards of the same rank.
  4. Full House: A combination of Three of a Kind and One Pair.
  5. Flush: Five cards of the same suit, not in sequence.
  6. Straight: Five consecutive cards of any suit.
  7. Three of a Kind: Three cards of the same rank.
  8. Two Pair: Two sets of two cards of the same rank.
  9. One Pair: Two cards of the same rank.
  10. High Card: The highest card in the hand when no other poker hand is formed.
Visual representation of poker hand ranking

Player Actions

In poker, player actions are fundamental to the gameplay and directly influence the outcome of each hand. Understanding these actions is crucial for developing a good poker game. Here are the four primary player actions in poker: bet, call, raise, and fold.

Bet: To place a bet means to wager chips or money into the pot. The first player to act in a betting round typically has the option to check (meaning they don’t bet anything and pass the action to the next player) or bet. If a player chooses to bet, they set the amount of chips or money they wish to wager. Subsequent players must match this bet or raise to stay in the hand.

Call: When a player “calls,” they match the current bet that has been placed by a previous player. Essentially, a call allows the player to stay in the hand without increasing the bet. For example, if a player before you, has bet 10 chips, and you choose to call, then you also put 10 chips into the pot.

Raise: If a player wants to increase the current bet, they can choose to “raise.” This action requires the player to match the previous bet and then add additional chips to the wager. Other players must match this new, higher bet if they want to stay in the hand. For example, if a player bets 10 chips, and you decide to raise to 30 chips, the other players must match the 30 chips to continue playing.

Fold: To “fold” means to discard your hand and forfeit any chips you have contributed to the pot. When a player believes their hand is weak and has little chance of winning, they can choose to fold to avoid further losses. Players who fold no longer participate in the current hand and wait for the next one to begin.

Now that we know the rules, it time to make a poker game.

Creating a Deck of Cards

In Unity, we can represent a deck of cards using a list. Each card object has properties like suit (Clubs, Diamonds, Hearts, Spades) and rank (Two, Three, Four, etc., up to Ace). Let’s start by creating a class for the Card and a method to create a deck of 52 unique cards.

Let’s create a script called “DeckManager” and add two classes to it. One is DeckManager class, that derives from monobehaviour and the other is the “Card” class which contains the properties of the cards. We will use the DeckManager class to generate the list of card objects. Here is how to do it.

using UnityEngine;
using System.Collections.Generic;

public class Card
{
    public string suit;
    public string rank;
}

public class DeckManager : MonoBehaviour
{
    private List<Card> deck = new List<Card>();

    public void CreateDeck()
    {
        string[] suits = { "Clubs", "Diamonds", "Hearts", "Spades" };
        string[] ranks = { "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace" };

        foreach (string suit in suits)
        {
            foreach (string rank in ranks)
            {
                Card newCard = new Card();
                newCard.suit = suit;
                newCard.rank = rank;
                deck.Add(newCard);
            }
        }
    }
}

Shuffling the Deck

Once the deck is created, the next step is to shuffle the deck. Shuffling the deck means randomizing the order of the cards. The Fisher-Yates shuffle algorithm is a popular method to shuffle a list. We’ll create a method called “ShuffleDeck” that takes the deck of cards and shuffles it using the Fisher-Yates algorithm.

public void ShuffleDeck()
    {
        int n = deck.Count;
        while (n > 1)
        {
            n--;
            int k = Random.Range(0, n + 1);
            Card temp = deck[k];
            deck[k] = deck[n];
            deck[n] = temp;
        }
    }

Dealing Cards to Players

Dealing cards means distributing cards to players. In most poker games, each player receives five cards. We’ll create a method called “DealCards” that takes the deck and the number of players as input and returns a list of hands for each player. Each hand will be represented as a list of cards.

We will create a method named “DealCards” that returns list of cards and takes an integer parameter for number of players.

public List<List<Card>> DealCards(int numberOfPlayers)
    {
        List<List<Card>> hands = new List<List<Card>>();
        for (int i = 0; i < numberOfPlayers; i++)
        {
            List<Card> hand = new List<Card>();
            for (int j = 0; j < 5; j++) // Dealing five cards to each player
            {
                hand.Add(deck[0]);
                deck.RemoveAt(0);
            }
            hands.Add(hand);
        }
        return hands;
    }

The above code adds the first 5 cards from the deck list to the hand list and removes them from the desk list. It does it for all the players, there by distributing the cards.

Now we have successfully created a deck, shuffled it and distributed the cards to the players. In our next article, we will see how to implement betting rounds and determine the winner. If you have any questions till now, you can leave them in the comment box below.

1 thought on “Creating a Poker game in Unity: Part 1(Basics)”

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from VionixStudio

Subscribe now to keep reading and get access to the full archive.

Continue reading