The simplest algorithm for poker hand evaluation

后端 未结 12 1041
独厮守ぢ
独厮守ぢ 2020-12-04 12:39

I am thinking about poker hand (5 cards) evaluation in Java. Now I am looking for simplicity and clarity rather than performance and efficiency. I probably can

12条回答
  •  情书的邮戳
    2020-12-04 13:03

    I have written a poker hand evaluator in both C++ and Javascript. Basically the program would convert a randomly picked set of cards to a 3d array of 1s and 0s. By converting the cards into this format I was then able to write functions that would test for each type of hand starting from the highest.

    So in recap, my program would generate random cards, convert them into a 3D array of hearts, diamonds, spades and clubs, where 1 represented one of the cards I had. I would then test the 3D array to see if I had a Royal Flush, Then Straight Flush, Then 4 of a Kind until a match was detected. Once a match was detected say after testing for a flush, then my program wouldn't have to test for straight, 3 of a kind, etc as a flush beats a straight.

    Below is outputted data from my program:

    My random cards:

    Table Cards
    { Value: '9', Suit: 'H' }
    { Value: 'A', Suit: 'H' }
    { Value: '9', Suit: 'D' }
    { Value: '7', Suit: 'S' }
    { Value: '6', Suit: 'S' }
    

    3D array representing my cards:

     A  2  3  4  5  6  7  8  9  10 J  Q  K  A
    
    Spades
    [ 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0 ]
    Diamonds
    [ 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 ]
    Clubs
    [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]
    Hearts
    [ 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ]
    

    Using the values above I can tell that I have a pair 9s with an A, 7, 6 kicker.

    You can see the array includes Aces twice. This is because you want to test for a straight flush starting from A. So (A,2,3,4,5).

    If you wanted to test for 7 cards instead of 5 you could also use this system. You can include the users 2 cards with the 5 on the table and run it through my system. You can also do the same for other players at the table and compare the results.

    I hope this helps a little.

提交回复
热议问题