Question

I can't really think of a good way to word this question, nor a good title, and maybe the answer is so ridiculously simple that I am missing it. I am working on a poker AI, and I want to calculate the number of hands that exist which are better than mine. I understand how to that, but what I can't figure out is the best way to iterate over a group of cards.

So I am at the flop, I know what my two cards are, and there are 3 cards on the board. So, there are 47 unknown cards and I want to iterate over all possible combinations of those 47 cards assuming that two are passed out, so you can't have two cards of the same rank and suit, and you if you have previously calculated a set you don't want to do it over again, because I will being wasting time, and this will be called many times. If you don't understand want I am asking please tell me and I will clarify more. So, I can set something up like this, if that element equals one, it means it is not in my hand and not on the board, 4 for each suit, and 13 for each rank. setOfCards[4][13] .

If I do a simple set of for loops like this: (pseudocode)

//remove cards I know are in play from setOfCards by setting values to zero
for(int i = 0; i < 4; i++)
    for(int j = 0; j < 13; j++)
        for(int k = 0; k < 4; k++)
            for(int l = 0; l < 4; l++)
                //skip if values equal zero
                card1 = setOfCards[i][j]
                card2 = setOfCards[k][l]
                //now compare card1, card2 and set of board cards

So, this is actually going to repeat many values, for example: card1 = AceOfHearts, card2 = KingOfHearts is the same as card1 = KingOfHearts, card2 = AceOfHearts. It will also alter my calculations. How should I go about avoiding this? Also, is there a name for this technique? Thank you.

Was it helpful?

Solution

It'd be easier to just have a single array of 52 cards, then you can say

for (int i=0; i<52; i++)
    for (int j=i+1; j<52; j++)

Now j will always be higher than i, so you get no repeats. You could do a similar thing for the 2-d array, but it's a bit trickier:

for (int k=i; ...) 
    for (int l=j+1; ...)

in the inner two loops ought to do it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top