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 write a "naive" algorithm but it requires a lot of code.
I saw also a few poker evaluation libraries, which use hashing and bitwise operations, but they look rather complex.
What is the "cleanest and simplest" algorithm for poker hand evaluation ?
Here's a naive approach to five-card hand comparison that I'm using to help initially populate a lookup table:
Instead of being as terse as possible, I prioritized type safety and clear, self-documenting code. If you're not familiar with the Guava types I'm using, you can browse their documentation.
And I'll include the code here (minus static imports for the enum constants at the bottom), although it's really too long to comfortably view in an answer.
Here is the algorithm translated to R, tested with a 6 card deck, corresponding to 42.504 combinations given by the result of:
combinations of poker hands. Did not tested with 13 card deck due to processing limitations (it would correspond to 2.598.960 combinations).
The algorithm represents the value of a hand by a string, composed by 2 parts:
So, for example, "32000NB" will be a Full House of three Aces and two Deuce.
The poker hand value string is convenient for comparative and ordering purposes.
Testing the function with the same hands of above example:
Which returns the same result:
Hope it helps.
If you just want to understand how it works here is simple algorithm:
It is from "ALGORITHMS AND ASSESSMENT IN COMPUTER POKER" by Darse Billings.