Hi all, I am making a texas holdem poker game. In the CompareHandValues() function, I would like to check for who won the game, or if there is a tie. I am using an external namespace called HoldemHand, which allows me to use “Hand”. We can compare different Hands to see which has the higher rank, or a tie.
string board = "2d kh qh 3h qc";
// Create a hand with AKs plus board
Hand h1 = new Hand("ad kd", board);
// Create a hand with 23 unsuited plus board
Hand h2 = new Hand("2h 3d", board);
// Find stronger hand and print results
if (h1 > h2)
{
Console.WriteLine("{0} greater than \n\t{1}", h1.Description, h2.Description);
}
else
{
Console.WriteLine("{0} less than or equal \n\t{1}", h1.Description, h2.Description);
}
What is the best way I can do to return if there is a tie between players or there is a winner?
I am planning to do that in the CompareHandValues() function. Is it optimal to use a dictionary, or what other ways can I carry out this operation?
Some examples: ( Players)
If P1 & P2 have same hand ranks: P1 & P2 split pot
If P3 & P4 have same but higher rank than PI & P2 which also have same rank: P3 & P4 split pot
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using HoldemHand;
public class GameManager : MonoBehaviour
{
public DeckScript deckScript;
private CardScript[] allCards = new CardScript[7];
private CardScript[] boardCards = new CardScript[5];
private List<Hand> equalHands = new List<Hand>();
private string boardString = "";
private List<Hand> handList = new List<Hand>();
[SerializeField]
private List<PlayerScript> playerScripts = new List<PlayerScript>();
public void DealClicked()
{
ResetGame();
for (int i = 0; i < playerScripts.Count - 1; i++)
playerScripts[i].StartHand();// player gets cards
deckScript.DealCommunityCards();
}
public void Check()
{
for (int i = 0; i < deckScript.communityCards.Length; i++)
{
boardCards[i] = deckScript.communityCards[i];
boardString += boardCards[i].Formatting();
if (!(i == allCards.Length)) boardString += " ";
}
//Hand h1 = new Hand(player1Script.GenerateFormatting(), boardString);
//Hand h2 = new Hand(player2Script.GenerateFormatting(), boardString);
StoreHandValues();
CompareHandValues();
/*
for(int i = 0; i < boardCards.Length; i++)
{
boardString += boardCards[i].Formatting();
if (!(i == allCards.Length)) boardString += " ";
}
*/
//Debug.Log("h1: " + h1.HandTypeValue);
//Debug.Log("h2: " + h2.HandTypeValue);
//if (h1 > h2) Debug.Log("h1 > h2");
//else Debug.Log("h2 > h1");
}
private void StoreHandValues()
{
for (int i = 0; i < playerScripts.Count; i++)
handList.Add(new Hand(playerScripts[i].GenerateFormatting(), boardString));
}
private Hand CompareHandValues()
{
Hand largestHand = handList[0];
Debug.Log(largestHand.HandTypeValue);
Debug.Log(largestHand.ToString());
return largestHand;
}
private void ResetGame()
{
deckScript.Shuffle();
foreach (PlayerScript playerScript in playerScripts)
playerScript.ResetHand();
deckScript.ResetDeck();
}
}