Saturday, April 25, 2009

Programming Poker AI

Peter has done great updates (one, two, three) about how the user interface for our iPhone poker game has evolved.

Since it's looking so good for 2 players with chips, we knew that the one big request we would get is to have an AI so you could play one player against your iPhone.  I really didn't think I could do a good one--not even knowing where to start.

This post really helped get me started, with some methods to do the hand strength and some basic play.  I'd thought there would be some crazy mathematical formulas, but it was simpler than that--simulate a bunch of hands and see the percent it would win made sense.  Brute force I could do :)

I'm sure I could have found some existing poker AI to just plug something in.  But for any feature that's a core thing in a program I am making, I really don't feel I've solved the problem if I use some library to do it.  It's a Poker App--I wouldn't feel right using somebody elses AI.

I'd already done my own thing to detect which is a winning hand for the 2 player game--and while pretty  inefficient compared to the ones I've read about--it works.  And is fast enough it can do all the AI's calculations in a second or two.  Good enough for our 2 player poker game (since only has to do it for one player instead of a full table of players!)  It just took running that hand comparison with the AIs hand against a bunch of random hands.  It can do 100 or so in a second.  Which by my tests, gets it within a few % of the right hand strength.  Any errors where it's off will just make it more unpredictable. 

Interestingly, since the cards haven't changed, we tried having later actions in the turn play quicker...and it was too quick and didn't seem right.  So if it has to decide again (it bet, you raised, so it has to decide what to do), it does more calculations to improve its hand strength estimate--and to take enough time to really seem to be thinking about what it will do.

Starting with the basic strength percent, it also does many checks to play-down or play-up various hand types.  A few examples:
  • If it has a pair but there's at least one card showing on the board that is higher than my pair subtracts a bit from the strength.
  • Big flush draws when I don't have a flush are scary.
  • There's a bunch more...
Another thing we've worked on is so it knows some more advanced poker strategies.  Things I've probably done in a game, but Peter has done and knows the reason and names :)

Sometimes, it will slow-play really good hands.  But also knows when to stop slow playing them too.  (AA pre-flop is one it might slow play, but if the board comes 678 suited, it likely will stop slow playing its cards.)

We'll leave other strategies it does as a surprize for when it does them.

It's turning out to be a very competent player!  I'd worked for several days doing many strategy improvements, and the first game, it beat Peter (a good player who's won a nice bit of money in tournaments.) 

1 comment:

jonwilson said...

Thanks a lot for sharing this, Michael. I'm working on a poker game for my programming thesis and I hope you don't mind me using your AI tips.

learn poker