Guest Post: Poker Analysis With Haskell
As the first guest blogger of Isaac’s I feel like I should say something about myself:
I am an in-progress Mathematician, Physicist and Computer Scientist, my present degree being a Joint MSci in Maths and Physics with Computer Science made to fit in as much as my University allows me to (1/3 in 1st year and slowly decreasing). I am also a huge Functional Programming geek, and have proudly been for 2 years now. Now, this blogpost is about Poker and Haskell.
Last year, as a new personal project, I decided that analysing the probabilities of a live Poker game was an interesting puzzle from both a mathematical and a programming point of view.
Yes, I do realise that the companies who actually need this information simply query a large database with all cards combinations, but my version of the program is far more complex and interesting than simply generating said database, and perhaps even unique because no one chooses it in virtue of its logical complexity. Side note: I did generate the database anyway in very few lines of code after setting up some Algebraic Data Types early on through development just to test it out, but it was too resource-expensive to be practical.
ADTs (with Type Classes) are FP Data Types with kind of greater or equal power as but infinitely simpler than Objects and Classes for you OO people; here is the simplest example from the program:
data Suit = Spades | Clubs | Diamonds | Hearts deriving (Eq, Ord, Enum, Bounded, Show, Read)
This single line makes Suit an ADT with 4 Value Constructors, which are Equatable, Orderable, Enumerable, Bounded, and can be Shown as and Read from strings; this means that all the obvious functions you might want to apply to them exist and work right out of the box (think of it as beefed-up class inheritance).
Calculating Poker probabilities abstractly is as easy as generating that database (it is even on Wikipedia!), but reworking them for the information which is slowly revealed in a game round and without actually calculating that precious database data is not. At all.
The current version of the program contains a good Data Types infrastructure, a basic shell to track the game, a set of functions to recognise Hand types, combinatorial counting functions, unique Hand ranking functions and various other stuff to tie it all together.
Very basically, whenever the player asks for the probability of something, this is what happens (only the steps needed by the specific request are executed):
- Everything is checked for changes since the last request and the following recalculated if there are any;
- The Table Cards are analysed and their Hand ranked absolutely (for the purposes of the other players’ chances), then the Player Cards are added and the same procedure is repeated;
- Then the number of better Hands which can exist at that stage in the game are calculated for both sets, which means cleverly subtracting amounts from the total number of better Hands;
- Then the specific request is answered: it could be what the best Hand the player can get at that turn is, or simply (not in the least) what the probability of winning that round is.
Obviously, all of this is heavily parallelised thanks to Haskell’s excellent libraries built on its intrinsic parallelizability (it literally takes 3 words to parallelise pretty much anything).
Look away now if you do not want to see some code I used to test my counting functions or learn a bit about Haskell:
-- Test whether any of the count functions of the given HandTypes yields a -- HandType which is not its own. In particular, lookout for ones higher -- than it. Also, count the instances of each checkHandTypes, checkHandTypesPar :: [HandType] -> [Card] -> [Card] -> [(HandType, [HandType])] checkHandTypes hts ocs cs = filter (not . null . snd) $ map (bad . getChecks) hts where bad (ht,chts) = (ht, nub . sort $ filter (/= ht) chts) getChecks ht = (ht, checkHtc ht ocs cs) -- Parallel Version checkHandTypesPar hts ocs cs = checkHandTypes hts ocs cs `using` parList rdeepseq
The first non-comment line is a Type Signature, and it helps the Programmer follow the program flow and the Compiler check for errors; you will find that in Haskell if something compiles, the program usually works perfectly on the first try (which is an amazing feeling).
The last 3 words of the last line intelligently parallelise the whole first function.
Sadly, sometime after the first kind-of-working version I took a break and lost interest because of a hard choice on the next step of its development between a purely mathematical rework and a purely formal rework in order to address the divergence of probabilities from the real ones in some situations (I knew something was up when I received a probability greater than 100%!).
I might pick it up again in the future, as it was immense fun at every stage.
I made the repository public yesterday for no reason at all, so Isaac asked me to write about it without delving too deep into the depths of Haskell.
If you are new to FP everything will look either incomprehensible or incredibly clear at first sight, and after some time just plain weird in both cases. The stand-out factor will definitely be the conciseness of it all (many crucial functions are one-liners).
Have a look if you like: https://github.com/Dr-Lord/Poker-Analyser.
If people are interested in either Poker or FP I could expand on either and/or become a recurring guest. If you are looking for a funnier article than this on FP, The Register has a great one.
Thanks to Thomas for writing this post about his Poker Analyser!
Check out my other blog posts! If you found this post interesting, feel free to let me know either on Twitter (@Isaac_M_Jordan), or in the comments section below.
Enjoyed my post? Sign up to the newsletter to receive a small email when I post. No spam, I promise.
Smithe504 on April 11, 2016, 1:12 p.m.
I think this is a real great blog post.Much thanks again. bfakfkebeeddegef
Pharmb449 on April 12, 2016, 4:29 a.m.
Very nice site!
poker online on June 8, 2017, 2:15 a.m.
In most modern poker games, the first round of betting begins with one or more of the players making some form of a forced bet (the blind or ante).
judi dadu koprok online on May 23, 2019, 1:56 p.m.
Thankyou for shraing this site, im really like your information
siskawijaya on Nov. 7, 2019, 6:16 a.m.
Thanks For Your support,,Good article
<a href="https://divadomino.cc" >bandarq online</a>
<a href="https://divadomino.cc" >bandar domino99</a>
<a href="https://divadomino.cc" >qq online</a>
<a href="https://divadomino.cc" >agen judi terpercaya</a>
<a href="https://divadomino.cc" >poker online terpercaya</a>
<a href="https://divadomino.cc" >daftar domino</a>
<a href="http://22.214.171.124" >poker online</a>
<a href="http://126.96.36.199" >judi poker online</a>
<a href="http://188.8.131.52" >situs judi online</a>
<a href="https://robotidpkv.com/" >Daftar Akun Robot</a>
<a href="https://robotidpkv.com/" >Akun Robot PKV</a>
<a href="http://strikerjitu.com/" >Strikerjitu</a>
<a href="http://strikerjitu.com/" >Bola parlay</a>
<a href="http://sosmedqq.tech/" >sosmedqq</a>
<a href="http://sosmedqq.tech/" >sosmedqq terpercaya</a>
<a href="http://RobotAkunpkv.com/" >Akun Robot PKV</a>
<a href="http://nyonyabola.com/" >Bola parlay</a>
<a href="http://nyonyabola.com/" >Bola Terpercaya</a>
<a href="http://nyonyabola.com/" >Daftar Situs Poker</a>
<a href="http://tipsbandarq.com/" >Tips BandarQ Terbaru</a>
<a href="http://tipsbandarq.com/" >Situs Tips BandarQ</a>
<a href="https://situspokerterpercaya365.blogspot.com" >Situs Poker Terpercaya</a>
<a href="https://https://divadomino2020.blogspot.com/" >Divadomino2020</a>
<a href="https://situsjudionlineterbaru2018.blogspot.com/" >Situs Judi Online Terbaru 2018</a>
<a href="https://situsjudionlineterbaru2018.blogspot.com/" >Situs Poker Terpercaya</a>
<a href="https://channelberitabaru.blogspot.com" >Channel Berita Terbaru</a>
<a href="https://rajaidpro2019.blogspot.com/" >Raja ID PRO 2019</a>
<a href="https://divadomino2019.blogspot.com/" >Raja ID PRO 2019</a>
<a href="https://idmasterpro2019.blogspot.com" >ID Master PRO 2019</a>
<a href="https://idmaster2019.blogspot.com/" >ID Master 2019</a>
<a href="https://divadominoqq.blogspot.com/" >Divadominoqq</a>
<a href="https://situsdivadomino2019.blogspot.com/" >situsdivadomino2019</a>
<a href="https://https://situsdivadominoterbaru.blogspot.com/" >situs divadomino terbaru</a>
<a href="https://divadominopenipu.blogspot.com/" >divadominopenipu</a>
<a href="https://divadominoterpercaya.blogspot.com/" >divadomino terpercaya</a>
<a href="https://sakongdivadomino.blogspot.com/" >sakong divadomino</a>
Aslijudi on July 30, 2021, 4:51 p.m.
Thankyou for shraing this site, im really like your information