Hacker News new | past | comments | ask | show | jobs | submit login
Show HN: A simple Poker API to calculate the winning hand/s (pokerapi.dev)
85 points by GFuller on July 5, 2020 | hide | past | favorite | 44 comments



I have a sudden urge to extend this to calculating probabilities and to buy a pair of smart glasses.


This has been a standard tool in remote poker for years. That tooling has even included hand histories for your opponent. One of the last great new players to blow up the poker scene before "Black Friday" almost went bust in a match where his opponent analyzed his hand history. You wouldn't gain an edge with these tools, you would be way behind if you didn't use them.

Running the calculations in your head isn't hard. You can get it with practice and it's an obvious step for being a good poker player.

There's a huge difference in quality of players in virtual gaming vs "B&M" (bricks and mortar, or playing face to face.) The world mourned the poker scene after Black Friday because the market for horrible players with money to blow disappeared. This left the hard core grinders with nobody to fish but each other. I suppose this would be much like potential competition in a world of WFH vs requiring your presence in one of the most expensive cities in the world.

There's still lots of horrible players in B&M games. You can do okay with just a gut feel for the calculations in these games. A lot of players like to gamble. It depends on the level of the game. As the blinds climb, the game gets harder.


For anyone wondering, Black Friday seems to refer to this: https://en.wikipedia.org/wiki/United_States_v._Scheinberg


I played online during the golden years (2003-2010), made a lot of money but also witnessed the progression of the game from shooting fish in a barrel to becoming significantly more demanding, even at low stakes.

I started playing one table at a time, then two and ended up at 4 (6max NL holdem). One thing that always bothered me, especially when the HUDs + pokertracker became necessities, was that no poker site allowed you to switch usernames in order to nullify playing history that your opponents had accumulated.

Eventually, this ruined the game for me as it completely changed the dynamics and took away a lot of possibilities and excitement. I also saw limit holdem be completely destroyed by algorithms and bots, long before it became so obvious that most stopped playing it altogether. I don't play these days but I'm wondering as to what extent shorthanded NL is being algorithmically exploited.


Basically you do that when you play poker. Knowing hand odds, pot sizes, number of players, are all numbers you use to do on the fly calculations and use to aid risk/reward assessment while on the table


I once had to write a version that worked for multiple games. Holdem, Omaha, Omaha 8 or better, Ace to Five, Deuce to Seven, and Badugi. I did it using inheritance, having high and low hand classes. The low ball games just reverse ordinary logic, but Omaha 8 needed both and the 8 or better restrictions. The Badugi class rewrote everything entirely.

When I finally finished I was asked to add Stud, Badeucy and Badacey. That’s when I quit.


Would you do this in a different way now?


I wouldn’t do it in Objective C again, that’s for sure.

But no, I think the class structure worked well for this. Just too many esoteric rules in these games. And it’s getting worse from what I hear of the high stakes games. Archey was just getting popular when I quit and now there is like a half dozen new games they play. It’s a constant arms race to see who can master a new set of rules to skin the fish with.


As crazy as it sounds, this is why I'd try to keep it simple and just keep each implementation separate. Sure I'd save some keystrokes having a base abstraction and whatnot, but I feel like Poker is one of those "i'll make an interface" traps that is much more complex than it originally seems


The abstraction is pretty simple, and close to that. IIRC I had base Hand object that defines a comparison routine that takes a Hand object.

The Hand object has a method that returns Card objects by index with a high to low order so subclasses can do their comparisons in either 4 or 5 card games.

The you need like a best hand method, so games like Holdem that make best 5 card hands out of 7 cards work. Then a bunch of utility functions.

It’s also useful to have your Card objects have their comparison functions.


What challenges were from Obj-C, specifically?


It’s a pointer based language, ie its C. Swift makes it far easier to write correct code and not have to chase down wild pointer problems.


Fair enough. My Obj-C experience was all years before Swift existed... mostly pre-ARC, even!


What are you using under the hood?

I’ve been using poker-eval for a few projects but it seems its now abandonware, with few updates out there.

I’m tempted to port it to Go and/or Rust to keep it alive, but I’m interested in what else people are using out there for poker hand evaluation.


If you don't need everything that's going on in poker-eval, you should be able to roll your own two dozen lines of Python.

A poker hand evaluator is problem #54 at Project Euler. [0] Peter Norvig has a solution there (which you can view after you submit your own correct solution) that taught me a lot about Python-style and how it differs from C++ (which was my mindset at the time).

[0] https://projecteuler.net/problem=54


That's very nice, much shorter than mine. array[[i][/i]r] had me scratching my head for a few minutes, when I realized those were stray italic tags. My only complaint is he doesn't check for the wheel, but to be fair most solutions don't seem to.


The wheel is excluded by the problem definition (or rather, not specifically included). The ace only counts as a high card here.


> The wheel is excluded by the problem definition (or rather, not specifically included).

Excluded was correct, if only indirectly: a straight is defined as five cards of consecutive value and aces are explicitly the highest (and not also the lowest) values, so the wheel is not a valid straight by the definition given.


Fair enough. I think the problem statement does not have enough information by itself (especially concerning how rank is used to break ties for two pairs), and so it invites people to fill in the blanks using their knowledge of the game.


It's not apparent to me which part of evaluating poker hands would need to be updated over time... Just rebuilding for new framework versions?


Evaluating quality source code:

1. Does it have a badge that says "build passing"?

2. Does it have a bunch of stars on GitHub?

3. Does it have a commit in the past week?

If so, wrap that in an npm package and publish it.

Otherwise, it's AbAnDoNwArE.


This is actually a side-project of a side-project, so I wanted to have a go at building the algo myself for a bit of fun/learning. I’m using Ruby under the hood...


Here's an implementation in Clojure that scores hands and selects a winner. It handles low vs high ace in straights.

https://gist.github.com/mchampine/dca965226148dfd19d5e4d7f3d...

Most of it was done for a weekly Clojure programming challenge run by Eric Normand in his PurelyFunctional.tv newsletter. You can see all the entries at https://gist.github.com/ericnormand/fbc9b6386a66c1ece590930d...


For Python I'd recommend: https://github.com/worldveil/deuces


Im using a two plus two hand evaluator i found in Java. Curious as well couldn’t find good open source ones


The Twoplustwo evaluator is open source ("GPL, use as you like" specifically)

Third party repo: https://github.com/tangentforks/TwoPlusTwoHandEvaluator

Original statement of license: https://forumserver.twoplustwo.com/showpost.php?p=1482536&po...



Reminds me of the 2+2 poker hand evaluator - they basically built an insanely fast giant lookup table that fit in ~100megs of ram IIRC.

The main idea was to do monte-carlo simulations and give you your odds of winning at any point in the game by dealing random cards for the unknowns.

Interesting read on that and other evaluators as well:

http://web.archive.org/web/20130116102452/http://www.codingt...


With a fast evaluator you generally don't need to do Monte Carlo approximation - you can just brute force all combinations.


One of my side projects was to make a game where you need to calculate your odds correctly as fast as possible but main goal was to learn some TypeScript so I lost interest after I was satisfied.

Anyway, I got at least one "game" done, you need to correctly count your outs. It is not perfect but covers most cases well.

https://github.com/GustekDev/poker-odds-game


I have an algorithm in C# that converts hands to numerical values from 0.highcard, 1.pair, 2.twopair, 3.trips ... to 9 (royal flush). From there it can compare the hands and determine the winner(s).


Calculating odds would be more interesting


That’s relatively straight forward by calculating the entire game tree for pretty much any street after the flop (52-7)*(52-8) combos.

The flop requires slightly more power to execute, but many tools do this in a small amount of time so I guess it’s feasible.


Did a Node library for that once...ranges, odds, hand evaluation, supports the poker hand syntaxes (e.g., QhQs, 99+, AQs+). https://github.com/ngokevin/pokery


Here's something you can use to calculate odds vs a range of hands. https://chriszieba.com/2017/11/26/holdem


I was also expecting to see probabilities here. Does anyone know of any other similar software that does do probabilities?


I agree. Not sure how this helps improve poker play.


It doesn't. Nothing does. Good poker players should have no problem doing all this in their heads. The tooling which does help is hand analysis and replay. I haven't kept up, but there used to be tools while playing online poker which would track your play and the play of your opponents. Then I believe there were databases where you could upload that information so that you could study the play of regular opponents. Most helpful was looking at your own play though.


I doubt it’s supposed to, it’s to calculate who wins when writing your own poker implementation. I am not the author, but calculating the winner is non trivial.


Is it not? The win criteria is well specified so just iterate downwards from the highest priority win until you find a match?


Sure, but that gets complicated when comparing two matching hands (flush vs flush, 2 pair vs 2pair). It’s more than a 50 line problem to solve.


Okay fair it’s more than 50 lines, I guess my definition of non-trivial is something you can’t quickly think of a design for. I’ll try it later and see if there’s some gotchas I can’t think of :)


I had the same initial reaction as the other person. Should be easy right? I think you’re right though, there are probably many more steps to getting this right than it seems on the surface.

I kind of want to take your 50 line challenge however :) I always loved playing poker, might be a fun exercise.


You want to buy deckofcardsapi.com?




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: