Checking Poker Hands

I am trying to figure out an efficient way to check for the various poker hands a player can get while playing Poker.

I know what needs to be done to do it, checking for matching card values for pairs and the like, checking for the Values to be in order for straights, and seeing if the Suits are all the same for flushes. However, how I go about doing that efficiently is something I have tried to figure out and have been unsuccessful.

So far, the player gets his cards and the values of the cards (ranging 2-14) are put into an array and the same with the suits (1-Spades, 2- Diamonds, 3-Hearts, 4-Clubs)

I have started a script that will be used for this part of the game. Creating some arrays and passing the arrays that hold the card values and card suits to the script.

var valueCheck: Array = new Array();
var suitCheck : Array = new Array();

function Update () {
}

function pokerCheck()
{
	valueCheck = GetComponent(CardInfo).cValue;
	suitCheck =  GetComponent(CardInfo).cSuit;
}

Any help doing this would be appreciated.

Thanks

1 Like

I would write a function for each type of check:
function isOnePair()
function isTwoPairs()
function isThreeOfAKind()
function isStraight()
function isFlush()
function isFullHouse()
function isFourOfAKind()
function isStraightFlush()

Notice that isStraightFlush() is super simple:

function isStraightFlush() {
    return (isStraight()  isFlush());
}

Be careful with isFourOfAKind(). If it’s true, then 3 other functions are true too: isThreeOfAKind(), isTwoPairs(), and isOnePair().

Same goes for isThreeOfAKind(). If it’s true, then isOnePair() is also true.

And if isTwoPairs() is true, then isOnePair() is also true.

Just organize all those boolean-returning functions in your logic, and I bet it’s pretty simple.

Thanks Brett, that will definitely make things work easier. However, the problem I have is that I am still not very good with arrays. I don’t know how I should check the arrays for similar numbers or for a sequential order for a straight. That is my biggest hurtle at the moment.

Thanks for the help so far.

You would find it easier if you sort your array.

To give you an idea on how to continue from Brett’s suggestion, here’s a pseudocode for function isStraightFlush():

*use a while(true) loop to start checking
*first check if there is a 10 - hearts (you’ll use a for loop inside to check)
*if there is a 10 - hearts in array proceed to check for jack - hearts (this goes on till ace - hearts)
*if you find ace - hearts return true
*if anywhere in the loop you didn’t find the card you’re looking return false

implement all the functions brett told then use them in your update function. If you have any problems about how to write this into code, feel free to ask.

Thanks Speed, that helps me understand it quite a bit on how to go about it, however the how to code it is where I am really hung up on at the moment.

Like I said before, the going through Arrays and such for this type of functionality is still a bit over my head.

I would really appreciate the help with the code aspect for this.

Thanks.

As suggested earlier, the first thing you’re going to want to do is sort the array by card value. I use C# myself (not Unity js), so I can’t tell you exactly how to do this, but that’s what you’ll want to do first: start digging through the Unity js docs, and figure out how to sort an array.

Note that if you use two arrays (one for value and one for suit), you’ll need to keep them in sync somehow when you sort, which could be tricky. I would recommend instead using a single array that stores a user-defined type with fields for both value and suit. Then, sort this array by value. (How to do this is of course language-dependent; in C# or C++, for example, you’d use a library ‘sort’ function along with a predicate that causes the elements to be sorted according to the ‘value’ field.)

(Actually, I suppose you might not need to keep the suits sorted, since all you’re really concerned with as far as the suits go is whether all the cards share the same suit. I’d still recommend going with a user-defined type though.)

As for the specifics, I’m not a poker player so I’m just going off the hands as described here. You’ve already gotten several good answers regarding identifying hands, but I’ll go ahead and provide some ideas here (which will most likely be largely a repeat of what’s already been said) just so you have some more material to work with.

As noted previously, certain hands include other hands, so this will have to be accounted for in the game logic when checking for hands.

So now on to identifying individual hands (note that it is assumed that the array is sorted by card value):

One pair

For each of the first four cards, if the next card has the same value, you have a pair.

Two pair

Either the first and second cards have the same value and the third and fourth cards have the same value, or the second and third cards have the same value and the fourth and fifth cards have the same value, or the first and second cards have the same value and the fourth and fifth cards have the same value. Furthermore, the values of the two pair are different.

Three of a kind

For each of the first three cards, if the next two cards have the same value, you have three of a kind.

Four of a kind

For each of the first two cards, if the next three cards have the same value, you have four of a kind.

Full house

Either the first two cards have the same value and the last three cards have the same value, or the first three cards have the same value and the last two cards have the same value.

Straight

The difference in value between each consecutive pair of cards is 1.

Flush

All cards have the same suit.

Straight flush

Both a straight and a flush.

Royal flush

Is a straight flush, and the ‘top’ card is the high card.

(I can’t guarantee I got all of that right, but it should be close.)

Coding-wise, there are various ways you could approach it. You could just take a ‘brute-force’ approach and check the array entries manually as described above (e.g. for two pair, which requires checking for three different cases). Or, you could probably make things a little more elegant and break the code down into generic operations that you would then combine to check for the different hands.

Someone may very well just go ahead and post some example code showing you how to do it, but note that working with arrays in this fashion is just a basic programming problem and really doesn’t have anything specifically to do with Poker or with Unity (aside from the fact that Unity js is specific to Unity, that is). As such, you might benefit from doing some exercises or writing some simpler examples and just trying to gain some more familiarity with Unity js and with programming in general. Another suggestion would be to pick one of the functions described above, try to write it, and then post it here if you run into problems (IMO, you’ll benefit more from this approach than from having someone post the code for you).

Regardless, you’ll need to get the array sorted first, so that’s where I’d recommend starting :slight_smile:

Thanks for all of the help. I was able to get them working. I don’t know if all of them are working, going to have to do some more testing, but I do know that most of it is working.

This is what I wrote.

	//CHECK FOR PAIR
function onePair()
{
	if(valueCheck[0] == valueCheck[1] || valueCheck[1] == valueCheck[2] || 
		valueCheck[2] == valueCheck[3] || valueCheck[3] == valueCheck[4])
		{
			isPair =  true;
		}
}


	//CHECK FOR TWO PAIR
function twoPair()
{
		if(valueCheck[0] == valueCheck[1]  valueCheck[2] == valueCheck[3] ||
			valueCheck[0] == valueCheck[1]  valueCheck[3] == valueCheck[4] ||
			valueCheck[1] == valueCheck[2]  valueCheck[3] == valueCheck[4])
			{
				isPair = false;
				isTwoPair = true;
			}
}
	
	//CHECK FOR THREE OF A KIND
function threeOfaKind()
{
		if(valueCheck[0] == valueCheck[1]  valueCheck[0] == valueCheck[2] ||
			valueCheck[1] == valueCheck[2]  valueCheck[1] == valueCheck[3] ||	
			valueCheck[2] == valueCheck[3]  valueCheck[2] == valueCheck[4])
			{
				isPair = false;
				isThreeKind = true;
			}
}
	
	//CHECK FOR STRAIGHT
function straight()
{
	var cardOne = valueCheck[0];
	var cardTwo = valueCheck[1];
	var cardThree = valueCheck[2];
	var cardFour = valueCheck[3];
	var cardFive = valueCheck[4];

	if((cardOne + 1) == cardTwo  (cardTwo + 1) == cardThree 
		(cardThree + 1) == cardFour  (cardFour + 1) == cardFive)
		{
			isStraight = true;
		}
}
	
	//CHECK FOR FLUSH
function flush()
{
	if(suitCheck[0] == suitCheck[1]  suitCheck[0] == suitCheck[2] 
		suitCheck[0] == suitCheck[3]  suitCheck[0] == suitCheck[4])
		{
			isFlush = true;
		}
}
	
	//CHECK FOR FULL HOUSE
function fullHouse()
{
	if(valueCheck[0] == valueCheck[1]  valueCheck[2] == valueCheck[3]  valueCheck[2] == valueCheck[4] ||
		valueCheck[0] == valueCheck[1]  valueCheck[0] == valueCheck[2]  valueCheck[3] == valueCheck[4])
		{
			isPair = false;
			isThreeKind = false;
			isFullHouse = true;
		}
}
	
	//CHECK FOR FOUR OF A KIND
function fourOfaKind()
{
	if(valueCheck[0] == valueCheck[1]  valueCheck[0] == valueCheck[2]  valueCheck[0] == valueCheck[3] ||
		valueCheck[1] == valueCheck[2]  valueCheck[1] == valueCheck[3]  valueCheck[1] == valueCheck[4])
		{
			isPair = false;
			isThreeKind = false;
			isFourKind = true;
		}
}
	//CHECK FOR STRAIGHT FLUSH
function straightFlush()
{
	if(isStraight  isFlush)
	{
		isStraight = false;
		isFlush = false;
		isStraightFlush = true;
	}
}
	
	//CHECK FOR ROYAL FLUSH
function royalFlush()
{
	if(isStraightFlush  valueCheck[4] == 14)
	{
		isStraight = false;
		isFlush = false;
		isStraightFlush= false;
		isRoyalFlush = true;
	}
}

Thanks again for all of the help.

1 Like

That logic looks fine! One addition though: a full house qualifies as two pairs too, so in function fullHouse() add “isTwoPair = false;”.

Right, thanks. I thought there was something I missed in that regard.

Thanks.

Hello does anyone have this algorithm apply in a Unity 3d project example? I would really apreciate it.

You should just copy and paste the code into one of your classes. Any class will do.

There is missing operand between valueCheck[0] == valueCheck[1] valueCheck[2] == valueCheck[3]. Someone let you know which operation need to perform.

//CHECK FOR TWO PAIR
function twoPair()
{
if(valueCheck[0] == valueCheck[1] valueCheck[2] == valueCheck[3] ||
valueCheck[0] == valueCheck[1] valueCheck[3] == valueCheck[4] ||
valueCheck[1] == valueCheck[2] valueCheck[3] == valueCheck[4])
{
isPair = false;
isTwoPair = true;
}
}

Thanks SO much dude! i need this for my own game! Mind if i use it? Thank you once again!

Please don’t necro threads. That post was made 12 years ago with the last post that dev made being in 2011 so I very much doubt you’d get a response anyway.

Note, there’s a like button for showing appreciation which is always nice. :slight_smile: