Algorithm to give a value to a 5 card Poker hand

后端 未结 3 483
-上瘾入骨i
-上瘾入骨i 2021-01-05 01:43

I am developing a poker game as college project and our current assignment is to write an algorithm to score a hand of 5 cards, so that the scores of two hands can be compar

3条回答
  •  不知归路
    2021-01-05 02:45

    As you have found, if you add together the values of the cards in the way you have proposed then you can get ambiguities.

    100000 + 4^1 + 6^2 + 11^1 + 13^1 = 100064
    100000 + 3^1 + 4^1 + 7^2 + 8^1 = 100064
    

    However, addition is not quite the right tool here. You are already using ^ which means you're partway there. Use multiplication instead and you can avoid ambiguities. Consider:

    100000 + (4^1 * 6^2 * 11^1 * 13^1)
    100000 + (3^1 * 4^1 * 7^2 * 8^1)
    

    This is nearly correct, but there are still ambiguities (for example 2^4 = 4^2). So, reassign new (prime!) values to each card:

    Ace => 2
    3 => 3
    4 => 5
    5 => 7
    6 => 11
    ...
    

    Then, you can multiply the special prime values of each card together to produce a unique value for every possible hand. Add in your value for type of hand (pair, full house, flush, etc) and use that. You may need to increase the magnitude of your hand type values so they stay out of the way of the card value composite.

提交回复
热议问题