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 