Skip to content

Poker Bot

A bot that plays online poker by reading the table from screenshots.

  • Python
  • TensorFlow
  • Keras
  • OpenAI GPT-4V
  • PyAutoGUI

A poker bot is a classroom exercise when the cards arrive as data. A real poker website gives you a screen instead, the same one a person sees, with no API and no feed of the cards. For two Cal Poly classes, three of us built a program that plays on a real site using nothing but screenshots. (Poker was number four of five in our pitch doc, after a movie recommender and a birthday gift advisor. The doc called it our best idea.)

Reading a screenshot takes a vision model, and every turn sent one full-screen image to GPT-4V. An hour of play cost about $12. So we gave the model one job, reading the table, and wrote everything else as plain code.

The reel above is the bot's point of view, dramatized. This is a real frame it read.

That's 247freepoker on the river. The boxes are what GPT-4V sent back for this frame: the pot, the board and a $4 raise from Hedy, one of the site's bots. Our bot clicked Call and won with a straight.

Why we skipped the dataset

The obvious plan was to learn hand strength from labeled examples, and we dropped it. The UCI poker hand dataset has a million of them, each five cards labeled with the hand they make.

Half of them are nothing, and most of the rest are one pair. The hands that decide big pots barely exist (there are 3 royal flushes in a million). And it's five cards. Hold'em has two private cards, five shared ones and four rounds of betting, and a five-card snapshot has no betting in it at all.

So we stopped trying to learn what a hand is worth. A straight beats three of a kind because that's the rule, and code can apply a rule. The learning went into the betting decision, where no rulebook exists.

If you can write the rule down, you don't need a dataset for it.

Reading the table

Each turn, the bot screenshots the whole screen and asks GPT-4V for the board state as JSON. That reply is the only thing the rest of the bot ever sees. Cards come back as strings like 10-H, so the strategy splits on the dash and never guesses.

The screen control came from self-operating-computer, an open-source project from HyperWrite that lets GPT-4V look at your screen and click things. (The version we started from played Super Mario 64, and the poker function still sits next to one called get_sm64_operation.)

3× speed, cut from the demo GIF in our repo.

It checks the turn, then faces Hedy's raise. From there, it's arithmetic. The pot is 278 and a call costs 4, so calling pays if the bot wins more than about 1.4% of the time. The strategy says call, and the bot clicks Call.1

That split made the bot testable without the website. The strategy takes four inputs (community cards, hole cards, pot and raises), so you can test it with made-up hands and never open a browser.

Deciding when to call

The strategy is the Knowledge-Based Systems half of the project, so it's rules on purpose. It weighs how good the hand is against what it costs to keep playing.

Hand strength starts with the best five-card hand the bot can make, from high card up to a straight flush. A small bonus for the two hole cards goes on top: suited beats connected, and an ace scores highest. Scaled down, that's a rough win probability.

Pot odds is the price, the call divided by the pot plus the call. After the flop, facing a bet, the bot calls when its win probability beats the pot odds. With no bet, it checks. So the whole post-flop strategy is one line through a plane.

Even the worst hand gets a win probability above zero, so after the flop the bot never folds to a small enough bet.2 The river bet in the recording was far under the line.

Before the flop, it plays looser, because two cards tell you little about the final hand. There's also a hard gate for junk. The next hand in the recording deals 8♥ 2♠, and the bot folds at once.

2× speed.

The result plays a tight pot-odds game, and every decision prints its inputs, so you can read exactly why it folded.

Clicking the right button

The bot first found buttons the way a person does, by reading their labels with OCR. But 247freepoker uses one button for both check and call, and swaps the label.

So the word the bot wanted wasn't always on the screen. With the browser window held in place, though, the buttons never move. Each one got a fixed point, written as a fraction of the screen so it works on any display with the same layout.

Before each click, the cursor circles the target for half a second, so a recording shows where it's about to click before it does.

Learning to bet

The Deep Learning half of the project is a Deep Q-Network. It makes the same betting decision as the fixed strategy, but learns it from chips won and lost instead of a chart.

For each state, it learns how many chips each action should end up with. Poker is a fair first DQN, with three actions and a reward that's just chips at the end.

It trains on a simulated game, because a thousand hands through GPT-4V would cost hours of screenshots. It's the same split as the live bot. Who wins a showdown has a right answer, so plain code decides it, and the network learns only the part that doesn't.

The network is small and built in Keras. It sees the cards, the pot, both stacks and the current bet, and outputs a value for fold, call and raise. After each hand, only the value for the move it played moves toward what that move turned out to be worth.3

The model's one job

The vision model is the only part of the bot that has to be smart, and the only part that costs money. So it only reads.

Everything with a right answer is plain code. The call at the top of this page was $4 into $278, and those rules call that bet every time.

  1. A call costs 4 to win 282, so the break-even is 4 / 282 = 1.4%. "Call" is a fixed point at (0.556, 0.86) of the screen. On a 1440 × 900 display, that's pixel (800, 774), and the log under the table says clicking at: 800 774. ↩
  2. Hand strength runs from 1 (high card) to 9 (straight flush), plus 0.1 to 0.9 for the hole cards, divided by nine. The lowest possible win probability is 1.1 / 9, or 0.122, which matches a bet of 13.9% of the pot. Before the flop the bot compares against half the pot odds, and anything under 1.45 folds. 8♥ 2♠ scores 1.40. ↩
  3. The input is 56 numbers, one per card plus the pot, both stacks and the bet, through two hidden layers of 64. The target is the reward if the hand ended, or the reward plus 0.95 times the best value of the next state. Each hand it trains on 32 moves sampled from its last 2,000, because moves in one hand are near-copies of each other. ↩