Creating a Poker game in Unity: Part 2(Betting rounds)

Card games are very complex to design compared to high graphics 3D games because of the logics involved. In our last poker game tutorial, we understood the basics of poker game mechanics, the logic for creating a deck and dealing the cards to the players. In this article, we will see how to implement the betting rounds for your poker game in Unity.

Blinds and Antes

Before we can start with the logic, we must understand how betting works in a poker game. Most poker games played online, use blinds or antes to start the betting. I know, these terms can be confusing if you are not a regular poker player. We will try to explain them with the betting logic.

Blinds are forced bets posted by the players sitting to the left of the dealer button, which rotates clockwise after each hand. The player immediately to the left of the button posts the “small blind,” and the next player posts the “big blind.” These bets create the initial pot. Antes are a small bet that all players at the table must contribute before the hand starts.

To keep this tutorial simple, we will include antes and not blinds in our game. So, every player can post an ante to get the cards and perform their action in turns.

Game logic

Here is how we are going to implement the betting logic in our games.

  1. The player posts an ante to get the cards.
  2. Then the turn is passed on to the next player to post an ante.
  3. Once all players have posted the bets, cards are dealt to the players based on the logic we covered in the first part of the tutorial.
  4. Player decides which action to take based on the cards.
  5. Then the turn is passed on to the next player.

In this part of the tutorial, we will cover only the turn-based logic to let players enter the bet one by one.

Entering bet amount

We will use UI to display the player turn and to enter the bet amount. Then using an UI button, we will call a function to save the bet amount and pass the turn to the next player.

Let’s add a text input UI for the bet amount, a text to display the player number and a button to submit it.

Click on the + sign>UI>Input Field to add an input field. Similarly add a text field and a button. Arrange the UI elements depending on your requirement. Here is how it looks in my game.

Ui elements added to the scene.

We need a script to take in the bet amount and save them. We will use an array to save the player bet data. Here is what to do in the code.

  • Declare public variables for the UI elements: TMP_InputField for entering the bet amount, TextMeshProUGUI for displaying the current player, and Button for submitting the bet. Also, declare a public numberOfPlayers variable to set the number of players.
  • Declare private variables: betAmounts (an integer array to store bet amounts) and currentPlayer (to track the current player’s index).
  • In the Start method, initialize the betAmounts array, update the current player text, and add a listener to the submit button.
  • Create a SubmitBet method that checks if the input is a valid integer, stores the bet amount in the betAmounts array, and updates the current player. If all players have submitted their bets, the submit button is disabled.
  • Create an UpdateCurrentPlayerText method to update the displayed current player text.

Since we are using text mesh pro elements, we need to use the namespace TMPro. If you are using the regular Unity UI then you can remove the using TMPro namespace.

Here is the complete script

using UnityEngine;
using TMPro;
using UnityEngine.UI;

public class BetManager : MonoBehaviour
{
    public TMP_InputField betInputField;
    public TextMeshProUGUI currentPlayerText;
    public Button submitButton;
    public int numberOfPlayers = 4;

    private int[] betAmounts;
    private int currentPlayer = 0;

    void Start()
    {
        betAmounts = new int[numberOfPlayers];
        UpdateCurrentPlayerText();
        submitButton.onClick.AddListener(SubmitBet);
    }

    void SubmitBet()
    {
        int betAmount;
        if (int.TryParse(betInputField.text, out betAmount))
        {
            betAmounts[currentPlayer] = betAmount;
            currentPlayer++;
            if (currentPlayer == numberOfPlayers)
            {
                //change to next scene
            }
            else
            {
                UpdateCurrentPlayerText();
            }
        }
        else
        {
            Debug.Log("Invalid bet amount entered.");
        }
    }

    void UpdateCurrentPlayerText()
    {
        currentPlayerText.text = $"Current Player: {currentPlayer + 1}";
    }
}

To use this code, follow the steps below

  1. Create a new C# script called “BetManager” and attach it to an empty game object in your scene. Copy and paste the above code.
  2. In the Unity Editor, assign the TMP_InputFieldTextMeshProUGUI, and Button UI elements to the corresponding variables in the BetManager script component.
  3. Run the scene, and you should be able to enter bet amounts for each player using the TextMeshPro fields. The bet amounts will be stored in the betAmounts array, and the turn will pass to the next player after each valid bet submission.

What’s Next

We have successfully developed a comprehensive betting logic that forms an essential foundation for our card game script. With this logic in place, players can now make betting decisions in turns, adding an exciting element of strategy to the game.

Next, our focus will be on enhancing the gameplay experience by enabling players to take actions based on the cards they receive and ultimately determining the winner through hand rankings.

Try out the script we’ve provided and if you have any questions or feedbacks feel free to leave them in the comment box below.

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