How I Built a Working Online Poker Bot, Part 2: Interlude
Tuesday, May 20, 2008   

Introduction

Last week, I posted about how I built a working, real-money online poker bot. This week, we'll discuss how to get started building your own bot, address some specific bot-building techniques, talk about the woeful state of online poker user privacy, respond to a few reader questions and comments, and close out with some recommended reading.

This series is both a detailed manual on how to build and make money with an online poker bot, and a story of how two guys from the Dallas poker underground managed to realize such a bot in practice.

We'll be discussing everything necessary to build what's quickly becoming the de facto standard in online poker botting: a sophisticated parallel processing rig incorporating collusion AI, massive hand history databases, and real-time information flow to dominate the opposition. This is where the world is headed:

NOTE: While I believe building and running an online poker bot to be neither cheating nor unethical, building a ring of such bots is a different story. As a long time poker-player, I absolutely don't condone cheating people out of their money by colluding in online poker or any other game. We're going to discuss things like bot collusion not so we can (all of us) go out and build colluding bot rings, but because these are the techniques people are using right now, today and tomorrow, to gain an unfair edge in online poker.

In order to build, or learn how to defeat, such a contraption, we're going to have to cover a lot of ground:

  • Artificial Intelligence. Neural nets, genetic algorithms, rules engines, decision trees.
  • Poker Strategy. Not the stuff you see on TV. Expert EV-driven poker strategy as it exists today, at the tables, in books, and on the forums.
  • Input Simulation. How to generate an appropriately timed and positioned stream of mouse moves, clicks, and keyboard input.
  • Operating Systems. Code injection, API hooking, kernel objects, multi-threading, DLLs. In general: forcing Windows to do what you want.
  • Reverse Engineering. How to reverse engineer poker client applications and the network data streams they rely on.
  • Probability and Statistics. Bayes Theorem, probability distributions, confidence intervals, and other goodies.

If you're new to poker, and particularly if you're new to programming, this will be a little bit of a trial by fire. Building a bot is basically the programmer equivalent of joining a Fight Club.

A guy who came to Fight Club for the first time, his ass was a wad of cookie dough. After a few weeks, he was carved out of wood.

Sort of. Read on.

Why are so many people interested in building bots?

Last weekend I got a phone call from a friend (a guy I'll have more to say about later), informing me that How I Built a Working Poker Bot, Part 1 had hit the front page of Digg, reddit, del.icio.us, and other sites, and that Coding the Wheel was getting enough traffic to occasionally throttle the server. 

Since then, readers have posted upwards of a thousand comments to the story, here and around the net, and I've gotten a couple hundred emails ranging from technical diatribes to job offers to good old-fashioned hate mail.

The response has been unexpected. Thanks to all who took the time to read the first article, and to those who've joined the discussion so far. In hindsight it seems obvious, but many, many people have a vested interest in building, or discouraging the building of, software tools which emulate human behavior. It goes way beyond online poker, then again, in online poker the profit motive is compelling:

The Poker DB

A successful online poker bot is worth hundreds of thousands of dollars even if all your bot does is break even. Sensationalistic? Maybe, but it's grounded in the very dull, non-sensational mathematics of rakeback and player promotions, which I've outlined below, and which we'll revisit in future posts.

Botting Rule #232: Wherever mouse clicks or other human-computer interactions are worth money, hordes of people will try to write software to simulate those interactions.

Why online poker clients are basically spyware

I want to preface this by saying: the online poker sites aren't evil. They're trying to protect their business, and that's perfectly normal. But I think they've strayed from the beaten path when it comes to respecting the privacy of their users.

Many online poker players have no idea that major poker sites silently install a browser plugin which has access to every page you visit in your browser, and potentially every piece of information on that page (the text of private emails; credit card numbers; user login information; etc):

Many online poker players have no idea that poker clients can and will snoop around on your system, potentially viewing sensitive data:

  • By examining your list of running processes
  • By reading body and titlebar text from every window you have open
  • By taking occasional screenshots
  • By snooping around on your file system, and in the system registry
  • By doing who knows what else, since there's zero oversight.

This is the definition of spyware. These "safeguards" constitute a basic invasion of your digital privacy and you should be angry about it. It doesn't matter whether the sites actually collect, pay attention to, store, or transmit that information. It doesn't matter if they do it with the best of intentions. It doesn't matter if they tell you they're going to do it in the EULA. They quite simply have no business doing it at all, and if they're going to do it, they should be doing a better job.

In other words, online poker is really the worst of both worlds: extreme invasion of privacy unheard of outside the realm of spyware, and zilch to show for it - zero effective safeguards against bots or other supposedly malicious software.

And yes: it's true that many sites will recognize well-known botting software such as WinHoldem. But they currently do very little in the way of preventing home-grown bots.

Botting Rule #472: The biggest advantage of a poker bot is its obscurity.

Using Spy++ or Winspector to get basic information from the poker client

Spy++ and Winspector are useful window analysis and debugging tools which allow you to view window properties and messages for any window in the system, including poker client windows:

If you take a close look at the above screen shot, you'll see that you'll see that Spy++ has extracted the handle (HWND), caption, class, style, location, and other properties of a Poker Stars table window. We can also drill down and find its child windows, parent window (if any), window class (PokerStarsTableFrameClass, in case you're curious) and other assorted information.

We can also use Spy++ to snoop on the exact messages received by a given window: 

Spy++ is included in all versions of Visual Studio, as part of an MSDN subscription. Winspector has a larger feature set than Spy++ and is is available as a free download from http://www.windows-spy.com/.

Later, we'll be using this tool to help us figure out what the poker client is doing, beneath the hood. It's also an extremely useful all-purpose window debugging tool, and an important part of a bot developer's (or any Windows developer's) toolkit.

Why a bot doesn't have to win, in order to win

I've stated repeatedly that a poker bot only needs to break even in order to generate a profit. Here's how it works, for those who aren't familiar with rakeback or other online poker promotions:

  • IF you play X hands of online poker within a certain amount of time (such as a calendar year).
  • THEN the poker site gives you Y amount of money, or "player points" which are worth money.

That's a simplified, but otherwise accurate, description. And listen. Y can be very large, even "life-changingly" large. Let's look at some actual figures.

Consider that it's fairly standard for a (human) player to play 8 or even 12 tables simultaneously. 

Furthermore, in online poker you're generally getting between 50 and 70 hands per table per hour. Let's call it 55, just to be conservative.

So that's 10 tables @ 55 hands per table per hour, for 10 hours a day, 6 days a week, 50 weeks a year.

If you do the math, you'll find it comes out to around 1.65 million hands per year. If your bot is playing anything other than the microlimits, this is easily enough to qualify you for (just as an example) the Poker Stars Supernova Elite promotion, which when properly liquidated is worth around seventy or eighty thousand dollars the year you achieve it, and two or three times that amount the following year (because of the multiplier effect of FPP bonuses).

Similarly, other sites offer rakeback promotions (~30% rakeback is fairly standard) along with assorted perks. If your bot is playing break-even poker, then your rakeback - quite substantial, as you move up from the microlimits - is pure profit. Ironically, your profit would shrink to zero if the site stopped collecting rake! (Update: a few readers have questioned this statement, claiming that a break-even bot with rake equates to a winning bot without rake. This is true, but the statement was tongue in cheek. It's ironic that a break-even bot ultimately ends up making money because of, rather than in spite of, the rake.) The thing that more than any other makes games tough to beat - the rake - makes it possible for your bot to turn a healthy profit. And that, as a long-time online poker player, makes me smile.

Botting Rule #1274: Many online poker sites would love to allow bots, if only their users would let them.
Botting Rule #47: Online poker players fear bots all out of proportion to the average bot's ability to win in competitive poker.

How to simulate human input (an overview)

In next week's post we'll cover this topic in detail, but for now: in order for your bot to be able to perform actions such as betting, raising, or folding, it needs to know how to talk to the poker client. There are at least three basic mechanisms you can use:

  • (Most difficult) Reverse-engineer the network protocol and communicate directly with the poker server.
  • (Somewhat difficult) Hook into the poker client just beneath the user-interface. In this scenario, you don't simulate mouse clicks; you figure out what internal function(s) handle those mouse clicks, and call those functions directly.
  • (Easy) Simulate user input - mouse clicks, keyboard input, whatever - at the operating system or even the driver level.

The first two techniques are mostly black magic. This third technique - user input simulation - can be further broken down into:

And regardless of which method you use, you'll want to make it realistic:

  • By incorporating subtle timing randomizations.
  • By creating realistic mouse-movement trails.
  • By occasionally clicking and interacting with unrelated windows.

You'll find that the SendInput method offered the best tradeoff between power and ease of use, but this is a complex topic. I mean: the interactions between user input events and the various Windows subsystems are complex, and will require tweaking to get right. But the code to generate those interactions is fairly simple.

Why poker bots will soon be accepted as some of the strongest players in the world

In the first-ever Man vs. Machine Poker Championship, a University of Alberta research team lead by Dr. Jonathan Schaeffer (the guy behind Chinook, the program that effectively solved the game of checkers)...

...piitted the Polaris poker bot against famous poker professionals, Phil "the Unabomber" Laak and Ali Eslami.

Laak/Eslami won the match with 2 wins, 1 draw, and 1 loss. That's not a hugely convincing margin, and in fact, as the University of Alberta website points out:

The match was a success in many ways. Polaris proved that it was able to compete with some of the best poker players in the world. In fact, the 2-1-1 record of the humans is a little misleading. The actual difference in monetary outcome was just $395 which is a very small amount. The format of the match did a great deal to reduce the large variance in the game of poker, but it does not remove it all. The $395 sum could be as few as one or two hands where Polaris decided to fold when the human who got the same cards decided to continue. For this reason, a future match should prove particularly interesting, as the bot continues to develop in strength.

As for Phil Laak, he had this to say about Polaris, and about poker bots in general:

We're already at the point where artificial intelligence crushes players that are unsophisticated, beats handily intermediate players, and loses small or wins small against savvy opponents... For Round 1, I'd say the bots have it.

Polaris's performance is reminiscent of the 1996 match in which world chess champion Gary Kasparov fended off the Deep Blue supercomputer, for the last time.

 

The IBM team responsible for Deep Blue made a few tweaks, and one year later, in 1997, Deep Blue came back and won a historic match, 3.5 to 2.5 (that's two wins, one loss, and three draws). Kasparov's allegations of unfair software-coaching practices were mostly ignored, and IBM never granted him a rematch. That caused some people to question the authenticity of Deep Blue's victory, for almost a decade.

Until 2006, when another chess program, Deep Fritz, handily beat world champion Vladimir Kramnik. The crucial difference between Deep Blue and Deep Fritz:

  • Deep Blue ran on specially designed supercomputer-grade hardware
  • Deep Fritz ran on a workstation PC with two Intel Core 2 Duo CPUs

For now I just want to make the point that intelligent, commercial-quality poker bots are a reality in the low and middle limits, and have been for a few years.

Why DLL injection is so powerful

Several readers have stated that techniques like DLL injection are either a) overkill or b) likely to get you in trouble with the online poker sites. So I'd like to address this issue in a little more detail.

First, here's a video which demonstrates a simple, harmless use of DLL injection in practice: overwriting the Poker Stars cashier balance with a balance dreamt up by the user.

  • Inject the DLL
  • Subclass the Cashier window
  • Detect when the Cashier window is invoked
  • Override WM_PAINT and display a "fake" balance to the user

First, I recommended it earlier, and I'm going to recommend it again: Jeffrey Richter's excellent book on advanced Windows API development: Windows via C/C++. Buy it. Borrow it. Steal it if you must. Most of the information you'll find inside is available somewhere on the Internet, but you'll spend hours upon hours tracking it down. And nobody - nobody - speaks on this subject with Richter's authority.

Here are some thing to keep in mind when deciding whether or not DLL injection is for you:

  • DLL injection isn't a hack. It's a formal capability offered by the Windows API, without which hundreds of legitimate applications (ranging from computer-based training apps to instant messaging applications) would stop working.
  • There are at least 6 ways to inject a DLL (or binary) code into another process on Windows operating systems: 1) via the Registry 2) using Windows hooks 3) as a Debugger 4) with Remote Threads 5) by creating a Trojan DLL 6) CreateProcess/WriteProcessMemory.
  • DLL injection isn't particularly difficult to implement, and it's not a poorly-documented, error-prone procedure. It more or less just works.
  • A given poker client can't simply declare that all injected DLLs are evil. The operating system, as well as legitimate third-party applications, can both cause DLLs to be mapped into a process's address space. If Poker Stars, World of Warcraft, or whatever other application were to simply shut down whenever this happened, they'd be unusable.
  • While it's possible to detect when DLLs are injected into the process's address space, it's not possible to determine whether a given DLL is innocent or malicious. Sure, you can look for the DLL's name, but the name can be changed, with every invocation if need be. You can create some sort of checksum to try to identify malicious DLLs at the binary level. Again, easy to get around by creating a self-modifying .EXE.

That said, is it possible to create a bot without DLL injection? Absolutely. But if you can't get your code to run in the poker client's process, your powers are somewhat limited. For example, you won't be able to subclass poker client application windows. You won't be able to query various process data structures. You won't be able to intercept Windows messages intended for the poker client. And so forth.

Why screen-scraping is a bad idea

A lot of people have argued that some of the screen-scraping techniques I mentioned - such as pixel-testing for hole cards - are difficult to code, error-prone, and tedious to maintain. I couldn't agree more. Screen scraping is a last resort. The bot's Input Component needs to be structured such that it falls back on screen-scraping techniques only when all other options are exhausted.

That said, a mature bot capable of playing at multiple sites will probably require some sort of screen-scraping capability. The trick is to create a generic screen-scraping mechanism driven by XML or some other easily-edited format. That way, when some minor aspect of the poker client UI changes, all you have to do is update the "schema" for that particular online poker venue, without rebuilding the poker bot executables.

This will be the subject of (you guessed it) a future article.

Recommended Reading

This is by no means a complete list of recommended programming and/or poker books. However, all the books on this are a) relevant to building a poker bot and b)  contain information which isn't readily available on the Internet, at least not without a good deal of sleuthing. I own a dog-eared, coffee-splattered copy of every book on this list, and have read most of them multiple times. Note: the programming books are coached from a C++ and Windows perspective but the techniques can be ported to other environments/languages.

Programming books:

Poker books:

  • The Theory of Poker. David Sklansky. Quite possibly the most influential book in poker, written by the father of modern poker theory, David Sklansky. The value of this book is that it explains poker theory in a way that's universally applicable to all poker games. Required reading for all poker players and poker bot programmers.
  • The Mathematics of Poker. Bill Chen and Jerrod Ankenman. Difficult but extremely rewarding exposition of poker mathematics. In order to program a fluent poker AI you'll want to understand a lot, though not necessarily all, of this material.
  • Professional No-Limit Hold 'em: Volume I. Matt Flynn, Sunny Mehta, and Ed Miller. In my opinion this is the best book on no-limit Hold'em ever written, and the notion of planning hands around commitment and SPR squares very well with the poker bot AI we'll get into later.
  • No Limit Hold'em: Theory and Practice. David Sklansky and Ed Miller. Another excellent no-limit Hold'em book.
  • Gambling Theory and Other Topics. Mason Malmuth. A somewhat dry, rarely-read masterpiece, with some non-trivial mathematics. Not everything here is directly applicable to playing, or building software for, online poker, but it's all useful.
  • Getting Started in Hold 'em. Ed Miller. Introductory text. There are many good introductions to poker and to Texas Hold'em; this is one of the better ones, and introduces a short-stacked strategy that will be useful in deriving a competitive bot AI for real-money games, down the road.

Conclusion

We took a break from the source code in this installment as I don't want to alienate too quickly the readers who've subscribed in the past week. In the next installment, we're going to have to plunge right back in, beginning with an in-depth introduction to the architecture of the bot's AI component, expressed in C++, C#, or the language of your choice, and concluding with specific code you can use to simulate a realistic stream of human input. We'll also perform explorative surgery on various online poker clients, exposing their inner resources and code structures to the light of day. Last but not least, we'll answer the question of: how do I snoop on the poker client's log or hand history file in real time?

As always, thanks for reading, and stay tuned for the next installment in the series. Until then...


Posted by James Devlin   115 comment(s)

James - excellent post, I personally like it when you include a bunch of source code, but realize that might not be to every taste..but it's good to take a break too. I AM looking forward to the AI stuff. Keep up the good work.

jcv on 5/20/2008 4:39 PM (629 days ago)

Interesting vid.. do you have the code for it? Can you post the code or let us download the app. I've got a friend I'd love to use that on, lol

FL Poker Nut on 5/20/2008 4:41 PM (629 days ago)

i'm not satisfied til i get the c++ code for the bot a.i. better yet, C# Smile

Damien on 5/20/2008 4:47 PM (629 days ago)

I've long wondered whether I was being taken for a ride by poker bots while playing online.

This is such a cool series. Smile

Chris on 5/20/2008 5:05 PM (629 days ago)

very nice thanks for #2 good read like the first.

Anonymous on 5/20/2008 5:52 PM (629 days ago)

Ok, you got a major load on your servers. Really loved your first post but thisis going down the drain...

bst on 5/20/2008 5:57 PM (629 days ago)

Seems like you went from an informative article with good content (#1) to no real content and a sales pitch for a few books on amazon to earn you some money (#2).

Hmm on 5/20/2008 6:27 PM (629 days ago)

"Ironically, your profit would shrink to zero if the site stopped collecting rake! The thing that more than any other makes games tough to beat - the rake - makes it possible for your bot to turn a healthy profit. And that, as a long-time online poker player, makes me smile."

That's not true at all. In fact, the logic there is terrible. If your bot is breaking even at a raked table, and you remove the rake, it would become a winning player. Far more so, in fact, than it makes just taking 35% of the rake back. It would then be getting 100% rake back.

Matt Maroon on 5/20/2008 6:31 PM (629 days ago)

@Matt, Anonymous: The sentence as originally written indicated that (a break-even bot with rake becomes a winning bot without rake). I took it out because it's not necessarily true. Absent rake, player strategies shift - who or what guarantees that you're previously break-even bot now wins?? Maybe it plays too too tightly (given that there's no rake) and therefore loses.

But other than that, you're right of course. Thanks for the note.

James Devlin on 5/20/2008 6:45 PM (629 days ago)

You need to rethink your paragraph on rakeback as Matt pointed out.

Anonymous on 5/20/2008 7:08 PM (629 days ago)

You're going to make a lot of poker sites very, very unhappy.

Xirium on 5/20/2008 7:09 PM (629 days ago)

Like everyone else, I'm loving the articles. Keep 'em coming.

I'm guessing the final article will also mention how an online poker bot is capable of spotting a rival bot and how it uses that information to its advantage Smile

dave on 5/20/2008 7:12 PM (629 days ago)

Xirium, it may indirectly make lots of poker sites very unhappy when hundreds or even thousands of home-grown poker bots start sitting at their tables. Some of which will be weak and have obvious "bot-like tells" and therefore create a great many unhappy human players who will undoubtedly complain and take their business elsewhere!

Ethan on 5/20/2008 8:04 PM (628 days ago)

Hi,

I would like to hire you to run a winning poker bot. How much would you charge ?

Thanks in advance

Loic Dachary on 5/20/2008 8:32 PM (628 days ago)

The poker sites are installing software that spies on you?

What kind of a complete and total retard are you?

I thought after this first post you were misguided, now I'm positive you're just retarded.

Eric on 5/20/2008 9:28 PM (628 days ago)

@Eric: And which poker site do you work for pray tell??

nyack nyack

shillz... lulz.. gotta love em

Hey James whats the IP lookup on that??

LJ on 5/20/2008 9:42 PM (628 days ago)

You might want to black out the names in the image, rather than blurring them -- as they stand now, some of them, at least, are retrievable with a bit of effort.

Anonymous on 5/20/2008 9:57 PM (628 days ago)

Expect the trolls to come out in force on this one James.. GOOD ARTICLE, fwiw, and about time somebody pointed this @#$$ out. The same thing with regard to privacy has been happening in WoW for too long..

www.kensavage.com/.../

Rich Debois on 5/20/2008 9:58 PM (628 days ago)

Loic- I've got a poker bot I'll sell you for a mere $50 transfer on PStars Wink

CodifyTheChad on 5/20/2008 10:30 PM (628 days ago)

@Rich. Here the post where it broke.

"4.5 million copies of EULA-compliant spyware".. World of Warcraft spyware.

http://www.rootkit.com/blog.php?newsid=358

Keith Stillwell on 5/20/2008 10:35 PM (628 days ago)

This is a great series. I would love to see more details on reading the text from the GUIs and changing the UI as shown in the video.

I have been looking at a .NET application that uses custom UI widgets instead of the standard controls etc. I tried using many API spying programs to get the text that is displayed on the custom controls, but the lpString parameter of ExtTextOut appeared to be garbled (unprintable characters). I need to do further testing and research.

Keep up the great work.

qwerty on 5/20/2008 10:41 PM (628 days ago)

@Ethan - actually they wont take their business elsewhere. What will happen is a bunch of poorly coded bots will spring up and actually lose money to humans and better coded bots.

Adam on 5/20/2008 10:49 PM (628 days ago)

h4x

b0xii on 5/20/2008 11:05 PM (628 days ago)

Adam, it would be interesting if players come to welcome bots because the only bots they really notice are the ones that are playing terribly and are beatable. What is probably inevitable though, is that a highly successful winning bot will get caught and possibly exposed to the public--then there would be chaos.

Ethan on 5/20/2008 11:32 PM (628 days ago)

Why the poker sites don't really care about a few bots: bots just mean more hands are played, and since the poker sites get their moolah from the rake, they are happy more hands are played.

Why that isn't sustainable: I am sure most people don't like playing against bots. So basically, bots make the other players unhappy. Unhappy players leave. Poker sites then cry. So basically, if bots become widespread, they will inevitably wreck the game of online poker.

That said, I save have at it. It is cool stuff, and those poker sites that are worth their salt will figure out how to foil your bots. But you may soon end up seeing captchas sprinkled throughout the game to foil bots. Which is kinda sucky for everyone.

dumbfounder on 5/21/2008 12:31 AM (628 days ago)

@James:

Strategy would shift toward playing a few % more hands. That would make the bot play more postflop, which is certainly it's weak point. So you won't get the 3.3x gain you would mathematically expect otherwise when going from 30% rakeback to 100%.

Pouring $100 or more per hour into the tables would also probably cause players to raise limits (the losing players now lose much less, and the winning players win more). So maybe you would get that 3x plus return. Maybe more.

Either way, adding that amount to the table is very significant, and I don't think it would be possible for someone to have an EV afterward that isn't significantly higher, even with rakeback and even if they don't adjust.

Matt Maroon on 5/21/2008 12:37 AM (628 days ago)

Very promission! Keep up the good work.

HH Parser on 5/21/2008 2:02 AM (628 days ago)

i agree with matt that the bot is likely to win with no rake if it breaks even with rake, but who cares?? like the sites will ever get rid of rake. non-issue

andy in seattle on 5/21/2008 2:04 AM (628 days ago)

Poker bots are over-rated, but it's cool work you've done.

A human player using a decision support system showing expected value outcome statistics can crush Polaris quite easily, data are here:

forums.poker-academy.com/viewtopic.php

The advantage of poker bots is that they are better at the math. When humans are equated on this (i.e., outcome statistics for playing choices), humans, with their wily intelligences, can make better decisions than bots.

holden on 5/21/2008 2:09 AM (628 days ago)

"assist with inter-bot collusion" ? !

Hold on there Hollywood. I'm not into that!

No Way Jose on 5/21/2008 2:12 AM (628 days ago)

Jose: I agree 100%. Building a bot is neither unethical nor cheating by any sensible definition of the word. Collusion, however, is.

Just for the record, I don't condone or promote cheating people out of their money in this way (via collusion of any kind). But not everybody has my scruples, so we'll discuss the techniques that people are actually using, not just the ones we'd want them to use.

James Devlin on 5/21/2008 2:50 AM (628 days ago)

what do you think is the biggest advantage with code injection over proxying the SSL traffic and reading/injecting packets into the datastream directly? is it worh the extra work just to be able to use pre-defined datastructures?

Anonymous on 5/21/2008 2:55 AM (628 days ago)

Mr. Devlin, Thank you for these articles. Your explanations helped me better understand how coding with Windows API work. It has certainly sparked my interest and I am very grateful for that.

Anonymous on 5/21/2008 3:24 AM (628 days ago)

@anonymous

seems like the packet-injection approach is more work than code injection.. then again I'm a mere application developer with not much knowledge of network-related things. is this something you web guys do frequently? hack into SSL data streams?

dorian on 5/21/2008 5:11 AM (628 days ago)

Hello from Germany!!!
Keep up the good work!!

@The Shield on 5/21/2008 9:54 AM (628 days ago)

Thank you for the effort you are putting into this James!

I started programming in 2004 (primarily got interested in it due to aimbots and such in fps games) and have always been interested in the very things you are talking about now. I have not had the time myself to research these topics, nor have I had the opportunity at work to work on a project that would encompass this type of development.

I am really looking forward to your explanations on ai and simulating user input, and if you are taking suggestions for language examples ("expressed in C++, C#, or the language of your choice"), I'd love to see some in c#.

Tony on 5/21/2008 11:35 AM (628 days ago)

"Building a bot is neither unethical nor cheating by any sensible definition of the word"

Here's a sensible definition: It is not allowed by the poker sites. The players do not want to play against bots. Those players play on sites which do not allow bots. Definition = Unethical.

Jack on 5/21/2008 1:22 PM (628 days ago)

I'm most looking forward to some sort of code on how you are grabbing the card values - be it via screenscrape or any of the other methods you mentioned.

I think that would be a lot more useful as any monkey can create a mouse click and auto-fold until they get top 10 hands and then hammer it preflop and on all streets. To be honest my lack of knowing an efficient way to gather the hole cards is the only thing that's been holding me back from building a poker bot over the past few years.

Andrew Johnson on 5/21/2008 3:02 PM (628 days ago)

Can the next article go into more about the algorithm of the bot? How the bot plays? How the bot breaks even? I know how to figure out how to make the bot interact with the poker client (even though I would like to learn more about the DLL injection), I want to know how to make the bot play.

Anonymous on 5/21/2008 3:18 PM (628 days ago)

A very nice start to what we hope will be an interesting series.

Will you be describing a preflop "shove bot" based on hole cards and position, or a more sophisticated bot that reads the postflop board, and considers betting action, stack sizes, and player stats before it makes a decision?

Either one would be interesting, but the later one more so.

I don't like the collusion stuff either.

Parserbilities on 5/21/2008 3:50 PM (628 days ago)

Hi,
I think many of your readers will be interested in the artficial intelligence portion of the series. I was wondering how in-depth you will be discussing in those topics?

Anonymous on 5/21/2008 5:15 PM (628 days ago)

How does your bot determine position and number of players? Screen scrape? Previous HH? Action order?

Location Location Location on 5/21/2008 5:54 PM (628 days ago)

Many have been asking for code to do the types of things mentioned in this article and the previous. There is an opensource poker bot engine/platform available here: http://code.google.com/p/openholdembot/

It very much uses the screen-scraping approach, but with a flexible "table map" external definition file that allows one to define how the screen scraper works with any given casino site, and which does not require binary recompilation. It also uses the SendInput API approach for mouse/keyboard event injection.

Being open source, full source code is available (of course). DLL injection for game state identification and alternative input stream hijacking is on the development agenda, and is just a matter of time. Feel free to stop by and help out.

Anonymous on 5/22/2008 1:27 PM (627 days ago)

Awesome articles! Thanks for doing this.

Chris on 5/22/2008 2:32 PM (627 days ago)

I'm more interested in the techniques you are using, more than I am in actually creating a poker bot to play poker for me. The application of the ideas are definitely beyond poker clients, and having experimented (lightly) with dll injection in the past, it is fun to see real world examples.

As to whether or not this is "bad", or will make poker sites mad... I think most developers that are capable of doing this just needed to hear "download Detours" or "use hooks". But chances are, they knew that already. Those that aren't may find the series interesting from a Win programming point of view, but probably shouldn't go invest a ton of money in their bot bankroll any time soon.

In fact, the number of people that are waiting around to copy and paste code are the comic relief portion of this article. Most will give up because it is too hard, and those that persevere will ultimately lose money trying to run a spectacularly awful bot. If you are an online poker player, you *want this*.

Personally, I'd love to sit at a table with someone's copy/paste bot. That is, unless their name is Chris Ferguson or Andy Bloch...

Bullseye on 5/24/2008 10:00 AM (625 days ago)

I am creating a c# poker interface library for commercial reasons.
It will work with all sites, and is not affected by changing poker client graphics.

It allows a full object based system to track whats going on at the table, and has a seperate library for parsing Hand Histories into statistics.

Currently only working under Party-poker - drop me an email at unreal1987@hotmail.com if anyone is interested in beta-testing.

Paddy on 5/25/2008 7:08 AM (624 days ago)

Wow, this is really great work you've got here and kudos on making it available! Can't wait to read the next few posts in your series.

shawn on 5/25/2008 2:18 PM (624 days ago)

yeah, great article by the way - gives people a head-start who wouldnt normally know where to start.

In terms of AI (I have studies a few different techniques) what would you say would be the most effective? It seems the *serious programmers* are all fairly confident with their neural networks.

Paddy on 5/25/2008 6:21 PM (624 days ago)

Absolutely intriguing. I'm a poker player and a student of Computer Science, and this is probably the most interesting article I have found online in a long time. Keep up the great work

Sean on 5/25/2008 8:46 PM (623 days ago)

James, could you please specify what exact methods you hook in order to get the necessary information? Both DrawText and ExtTextOut do not give any information when detoured (only get called when resizing or minimizing the screen). This holds for both pokerstars and everes poker.

Frank on 5/26/2008 7:59 AM (623 days ago)


Hook both the "W" and "A" versions of those functions. DrawTextW, DrawTextExW, TextOutW, ExtTextOutW, etc. Note that DrawTextW calls DrawTextExW which calls TextOutW and so forth. Here's a PokerStars API trace.

PokerStars API logging

James Devlin on 5/26/2008 9:13 AM (623 days ago)

thanks james, I really appreciate this!!

Frank on 5/26/2008 10:44 AM (623 days ago)

An incredibly insightful article on poker bot methodology. Cheers.

Jazzy on 5/27/2008 4:58 PM (622 days ago)

I like.

borat on 5/28/2008 6:03 AM (621 days ago)

I'm looking at a .NET application, and when I hook DrawTextA/W, DrawTextExA/W, TextOutA/W, ExtTextOutA/W, the string is garbled. Most of the time it is not ASCII. It appears the application is using DirectX. I'm stumped.

qwerty on 5/29/2008 6:07 AM (620 days ago)

Color will win at this hands down. LOL and its all 2d painting, no 3d. I agree with the previous comments your just trying to sell books, everything you posted is known by thousands of people, perhaps post more private info or sources.

Runescape Color Bot on 5/29/2008 8:55 AM (620 days ago)

runscape he said that in the first article, basically saying that this information is wide spread on the internet. There is just no where that has it all in the same spot. he put it all together in 1 section for people to learn from it. so what if he makes some money on selling some books, he is only doing what he said he would in the first place.

bring all the info to 1 spot instead of it being all over the place and harder to find.

james on 5/29/2008 9:20 AM (620 days ago)

Hmm... I like the article, and there's a link to a book that might be useful to the subject matter. Oh the dilemma... if I click on the link to the book, the author (whose article I like, remember?) might actually get a little bit of an affiliate kick back.

What to do?? After all, I paid good money for this post... oh wait.

Bullseye on 5/29/2008 5:02 PM (620 days ago)

Question: You call this a "break-even" bot with rake - but how is it breaking even if there is a 5% rake - that means that it must be making a 5% profit in order to stay in business.. correct?

Bryce on 6/3/2008 3:15 PM (615 days ago)

first off, great article!
im also a bot builder and have built several bots myself (although theyre mostly used for my univ. AI research rather than making money) and i find them generally inefficient unless you use learning based AI and opp. modeling as proposed in the univ. alberta papers, or you exploit the fishes at the lo limit tables. but they do work! a simple EV calculating bot can make up to about $10 an hour at the $.10/$0.20 fixed holdem tables.

anyways, id like to add that poker sites DO have bot prevention measures and ive been caught several times despite having randomized click coords/speed (and i mean uniformly distributed randomization, not centered), hidden processes, etc etc. all the things a comp sci nerd can drum up, and i seriously do believe they go over play history and activities manually if your winnings get large so you cant let your guard down even if your bots seem sufficiently stealthy.

heres one i got from pokerstars:

--------------------------snip snip-----------------------------
Hello XXX,

We at PokerStars have noticed that recently you have been running a program
called a Poker Bot.

Perhaps you were unaware that this was against the rules, or were just
curious about the program, but use of this software under any circumstances
is against our terms of service. Those terms of service are located here:

http://www.pokerstars.com/poker/room/tos/

Among them are these terms:

5.5. AUTOMATIC PLAYERS (BOTS). The use of artificial
intelligence including, without limitation, "robots" is
strictly forbidden in connection with the Service. All
actions taken in relation to the Service by a User must
be executed personally by players through the user
interface accessible by use of the Software.

5.6. You agree that PokerStars may take steps to detect and
prevent the use of prohibited EPA Programs. These steps
may include, but are not limited to, examination of
software programs running concurrently with the PokerStars
Software on the User's computer.

5.8. FRAUDULENT BEHAVIOR. In the event that PokerStars deems
that a User has engaged .... in any of the activities set
forth above or any other game manipulation ... PokerStars
shall be entitled to take such action as it sees fit,
including immediately blocking access to the Service,
terminating such User's account with PokerStars, seizing
all monies held in the User's PokerStars account...

PokerStars takes its obligation to the integrity of its games seriously, and
cannot permit such programs as yours to operate.

We realize that you probably were unaware of the forbidden nature of this
program, and therefore we will not take any of the above stern reactions due
to this detection. Instead, all we ask is that you reply to this Email
stating that you're now aware of the rule against bots, and that you won't
use such programs on PokerStars again in the future.

Until we receive such an acknowledgement, we have temporarily suspended your
PokerStars account. The account closure isn't intended as punishment nor
anything other than to get your attention and to have your acknowledgement
of the rules on record. Once you've acknowledged the above rules and agreed
to refrain from the use of "bots", we will be pleased to restore your
account to good standing.

Please don't protest that you didn't use a bot in your response or
otherwise object to this mild warning. Any reply that doesn't include
something like "I understand that bots are against the rules and I won't use
them on PokerStars" will only delay the re-opening of your account.

Thank you for your cooperation in this matter.

Best Regards,

YYY
PokerStars Game Security
--------------------------snip snip-----------------------------

bwahaha. anyways, great article, cant wait for the next!

Hannibal on 6/4/2008 4:33 AM (614 days ago)

when can we expect the 3rd installment? so far all of have interesting reads and i for one would love to be able to see the next

Anonymous on 6/4/2008 4:17 PM (614 days ago)

it should be good

Doyle on 6/5/2008 2:13 AM (613 days ago)

I dodge bullets baby!

Phil H. on 6/5/2008 11:27 AM (613 days ago)

this is scary stuff, baby

Scotty on 6/5/2008 3:14 PM (613 days ago)

But can the bot beat the 10/20 at the Bellagio?

Phil L. on 6/6/2008 4:31 PM (612 days ago)

if you leave your bot at home, you cannot win, baby

Scotty on 6/7/2008 1:15 PM (611 days ago)

So, in the end, it's just a war between bot builders and the poker site's bot detection software?

Thomas on 6/9/2008 5:55 AM (609 days ago)

That's an awesome set up he's got in the first picture! I want 4 monitors!

I think that in time, if poker bots become more and more common, big sites like PokerStars and PartyPoker will use algorithms to spot the botters from the notters and bust them! (Hopefully). I posted an exert of this article here: www.pokerisrigged.com/showthread.php Hope that's ok with you!

Poker is Rigged | Forums on 6/9/2008 6:44 AM (609 days ago)

Great work James. When do you expect the next articles to come out? Looking forward to them

CJ on 6/9/2008 6:49 AM (609 days ago)

Question.... How did you use detours to hook only the DrawText, TextOut, etc... api's for only the pokerstars process and more importantly for certain poker tables as to not get the information confused between tables?

Anonymous on 6/9/2008 1:55 PM (609 days ago)

This is probably the most important series of articles I've ever read. Thank you very much for this. (and more, I hope)

Anonymous on 6/9/2008 9:37 PM (608 days ago)

When I Hook API functions like DrawTextW, TextOutW, I can find out the Text, DC and Coordinates. But when 2 or more tables are opened, how to determinate in what window that happens. Most programs draw text and images on temporary Bitmap and then bitblt it to the window.

Kaban on 6/10/2008 1:29 AM (608 days ago)

The second part was even more interesting! We used similar techniques for our poker bot, www.icmbot.com, if you want to check it out.

Free demo and download, full version is almost ready to be released.

I enjoyed the article, thanks!

Michael on 6/10/2008 2:39 AM (608 days ago)

PokerStar uses special (bitmap) fonts for user names and stack sizes. So only functions, that draw text in status window, can be hooked (See fig. James Devlin on 5/26/2008 9:13:00 AM).

Kaban on 6/16/2008 6:22 AM (602 days ago)

very interesting!!


Thanks
LG
http://www.dealtoplay.com

Lucas on 6/28/2008 2:18 PM (590 days ago)

Your instructions are lame and your motivation for writing this is so obvious.
Most good players I know would love to play against bots.

Its obvious that you're posting controversial information on one of the most controversial and heated topics of online poker, which is extremely popular.

You seek to earn money from your repeated references to amazon.com books with your tagged links, you've got your google ads, and ofcourse you're creating a highly ranked site which you can obviously make a lot of money with.

So you're a hoodinie m#$(*^rf#$^%er. I kind of respect you, but hahahaha its just funny. Well done on your SEO and your plan.

Lope on 7/2/2008 8:48 PM (585 days ago)

Thanks for the many kind comments. If you've emailed me, hang on! I'm still playing catch up.

@Kaban: There's more than one way to skin this particular cat. Stay tuned...

@Lope: I've said from the first post of this series, any good player should want to play against the average bot. Period. I myself would love to play against nothing but bots, all the time, for real money. Bring on the bots.

James Devlin on 7/4/2008 1:34 AM (584 days ago)

the diagram in the early part of this installment goes beyond mere collusion,
it's a roll-your-own superuser.Scary.
I was a hardware debug tech in Silicon Valley for 15 years. This stuff looks way
too doable [esp. for someone like myself, who has enuf knowledge of C to cause serious injury Smile]
Currently, there is an input-based offline simulator for Razz [we use this on
2+2 to analyze hand histories]. You punch the numbers in [your holecards, boardcards], it kicks out an estimated win percentage. Whole process takes
20-30 secs., too slow for a bot.
However-
If the holecard/boardcard data were dumped off to, let's say, a co-processor,
perhaps a TI or Analog Devices DSP chip mounted on a PCI extender card...
still using all the bot API processes, but with lightning fast simulation
for razz/stud hands instead of a holdem lookup table...
disregarding expenses [the hardware prototype could be functional for 500-1500$, including sw/reference books],
is this a feasible approach?

$pike

P.S. may the the poker gods forgive me for speaking of such blasphemous things! I should not want to be forever cursed in my daily excursions to the triple draw games....

$pike420211 on 7/5/2008 1:19 PM (583 days ago)

If any of you players or bot writers need hand histories for your stats (PT, HM etc) databases you should head to HandHQ.com.

handhq on 7/9/2008 9:40 AM (579 days ago)

is it true that when using the a poker bot, you have to get the other players to download it so you can use it against them? just asking.. ive been doing alittle looking around and reasing alot of forums looking for a good bot. i play poker all of the time....... and well.... it takes too long to make any good/real money. what are some suggestions for finding a good bot?

tom on 8/29/2008 10:36 PM (527 days ago)

Hi, Tom.

"is it true that when using the a poker bot, you have to get the other players to download it so you can use it against them?" Not true. There are some bots that work that way, but I don't consider them bots but spyware.

"just asking.. ive been doing alittle looking around and reasing alot of forums looking for a good bot. i play poker all of the time....... and well.... it takes too long to make any good/real money. what are some suggestions for finding a good bot?" No good bots out there. When a bot is good, you don't give it to everybody: you keep it secret and make money stealthy.

You should make your own bot, following this series you can if you are a programmer. I have started less than a month ago and I already have a pre-flop bot.

Anonymous on 9/2/2008 11:22 AM (524 days ago)

Nice Article

Poker Bot on 10/13/2008 4:33 AM (483 days ago)

Very interesting stuff.

I used a fairly basic poker bot on FT that I got for free somewhere. Low limits obviously, and it seemed to break even over about twenty hours play. I only ran it to see how it would work, and it didn't make enough on rakeback at those limits to interest me much. I can see the potential here though.

free poker on 10/25/2008 10:34 AM (471 days ago)

Aside from the main focus of the article on building a poker bot I think you gave some very good extra information on the parallel between a lot of poker clients and spyware, well done for that.

Poker Tournaments Online on 11/5/2008 10:17 AM (460 days ago)

Yes, that info regarding the use of spyware by the poker clients was a bit of an eye opener. In the course of looking at various bots, and calculators, that are commercially available I was surprised at how developed this area of online poker has become. It would seem now that a significant percentage of the players at any table are using some form of software to help their game. Which leaves those of us that aren't using any (yet) at a bit of a disadvantage I think. Is this the future of online poker then?

Free Texas Hold'em on 12/21/2008 10:04 AM (414 days ago)


Just curious...has anyone recently used this against Poker Stars?
The API hooks into the chat window no longer work...

JohnDoe on 12/29/2008 7:03 PM (406 days ago)

Man I sure wish I knew what the heck you're talking about.

StyleBackground on 12/30/2008 5:35 PM (405 days ago)

I have the same problem, the API hook for PokerStars is not working anymore.

Joop Bob on 12/31/2008 9:57 AM (404 days ago)


Joop,

Sorry to hear that you are having the same issues.

I noticed that the Stars client no longer uses the custom ActiveX control (i.e. Afx:400000:20).
It appears to have been replaced with the control: PokerStarsChatClass.

For Stars, I used to be able to API hook on the user32.dll API (DrawTextW), but this no longer works. However, my API hook on the gdi32.dll API (ExtTextOutW) still works for Full Tilt, so I might have to switch over to that site.

Have tried the following hooks on Stars and "none" of them return information from the latest Stars chat window...assuming I have them coded correctly:

User32.dll
----------
DrawText
DrawTextA
DrawTextW
DrawTextEx
DrawTextExA
DrawTextExW

GDI32.dll
---------
TextOut
TextOutA
TextOutW
ExtTextOut
ExtTextOutA
ExtTextOutW
PolyTextOut
PolyTextOutA
PolyTextOutW

If anyone else has any insight or suggestions, they are greatly appreciated!


JohnDoe on 12/31/2008 10:34 AM (404 days ago)


More information in regards to not being able to capture the text from the Stars Chat window.
Dug up a related link issued on December 12, 2008...about the same time I noticed that my API hooks stopped working for Poker Stars.

www.pokercalculatorreport.com/.../...industry.html

JohnDoe on 1/1/2009 11:31 AM (403 days ago)

Hey,

Im trying to make a bot for CDPoker but having the problem trying to obtain the chat window.

its a "internet explorer_server" class window.

Joe Cooper on 2/10/2009 8:58 AM (363 days ago)

Nice job!

We did something similar here:
http://www.pokerbot-smart.com/

Smart Poker Bot on 3/1/2009 12:02 PM (344 days ago)

You should write a book on this. Ever think about the viral marketing effect this could have ? Your posts already have lots of quality information, so you could amalgamate them and create a one of a kind book.

California Poker on 4/19/2009 3:01 PM (295 days ago)

Hey guys, can anyone here tellme how to create a bot for Zynga's Texas Hold'em Poker?

I lost like billions of dollars like that. There are bots available but i cannot afford one, I am just a student. I am not aware of much of computer language but i can get help from my Brother who is a computer designer but he is not sure what i want.

Please reply at broken_89@live.com

Zak on 5/13/2009 9:55 PM (270 days ago)

Hey guys, can anyone here tellme how to create a bot for Zynga's Texas Hold'em Poker?

Please reply at spilja_vip@live.com

Hariss on 5/28/2009 12:19 PM (256 days ago)

Hack free for pokerstars????link

Piter on 6/18/2009 10:33 AM (235 days ago)

The great thing about poker is that a lot of it isn't based on raw data, like a players bluffing habits or when he plays aggressive as opposed to defensive. Poker has a very human aspect.Sadly blackjack, and checkers have been completely figured out. Chess is very close. I wonder if it'll ever happen with poker?

video blackjack on 6/19/2009 3:59 AM (234 days ago)

Since online poker players are playing against each other to win each other's money, pray tell how the site is affected? At the most, maybe in a free roll tournament, if you are lucky. If its a buy-in, you are still taking other player's money, not to mention paying to get in anyway. Tournament fees are already collected by the site separately. So this is not something that makes one smarter by taking a higher moral ground saying bots are taking on the poker sites, just other innocent players? If you refer to Ring Tables, thats again taking other player's money. Unless I missed the very premise of this article?

Anonymous on 6/19/2009 5:04 AM (234 days ago)

great post! I have long been a fan of the CPRG's work. i guess the goal is to write the 'middle bit' - or processing bit - in a way that it can deal with scraped input from any number of sources/poker clients, and likewise output something that can be translated by any number of interfaces.

clickH on 6/25/2009 11:16 PM (227 days ago)

@ erik I wouldnt be suprised at all if they installed spyware to detect known cheats. If you think this isnt possible look at spyware like punkbuster ( Call of duty + a series of other games) Warden in World of warcraft is another example of spyware to stop cheating. As described it can take screenshots look at your running processes and has the capacity to look at basically anything on your PC.

This isnt fiction its fact.

scott on 7/8/2009 3:00 PM (215 days ago)

http://www.floribus.com/howto.html

CatShoe on 8/7/2009 1:43 PM (185 days ago)

Common guys !No one on this earth will give you a ,,free poker bot" or information how to do it...Even here - the ,,how to make " guide is so difficult that i have the feeling i need to give all my life to make a bot xD Programming?C++ ,mathematic theorems, algorithms bla bla bla , i need to be a professor, genius studding and studding and studding...
So,thats just life guys-nothing is free or worthless...

ANTONY on 8/13/2009 8:59 AM (179 days ago)

Well, not a whole life. I have needed less than a year to make a bot, and I started by reading this blog. It's not a winner bot yet, and I need to improve its stealth, but it's a start. And, in the meanwhile, it already helps me showing information and advice while I play.

Anonymous on 8/14/2009 5:39 AM (178 days ago)

Wow great in detail stuff here! Great technical information!

quit smoking on 9/14/2009 1:35 AM (147 days ago)

Hi everyone,

I would like to congratulate James Devlin because your web site is really amazing. From the technical point of view, it is 7 stars, not 5. It is outstanding.

Right now, I would like to find someone to cooperate with me on a poker software project whose goal is to improve SNG table selection (Pokerstars). As James Devlin states, table selection is very, very important and I need to improve that area. What I am looking for is someone that is able to develop a software that retrieves the content of any listbox on Pokerstars. I have already developed all the software needed to evaluate if we should or should not register on each specific tournament taking into account some statistic criteria ....

If you are interested, please post your email on a comment and I will contact you afterward.

Poker of Aces on 11/22/2009 9:48 PM (77 days ago)

Hey Poker of Aces,
I work on another Poker project. I focus on cash games. But maybe we could exchange some knowledge and how to investigate new Poker rooms.

Feel free to write me an email: Klabautermann176@web.de

Klabautermann on 11/25/2009 7:56 PM (74 days ago)

James, there is some seriously detailed information in this second Poker bot post. I really like the idea of winning through not winning and can see how the Rakeback channel is a great way to do this.

I would interested to know how many players at a Poker table are bots like you have coded? Could there ever be a bot stalemate?

James on 12/11/2009 9:48 AM (59 days ago)

I will follow the next articles.

Unibet on 12/14/2009 1:11 AM (56 days ago)

Are there still many pokerbots active ?

odds comparison script on 12/25/2009 3:45 PM (45 days ago)

I have already developed all the software needed to evaluate if we should or should not register on each specific tournament taking into account some statistic criteria.
Assignments
GCSE Coursework
Dissertation Help
Thesis Writing
Essay Help

Anonymous on 1/6/2010 3:09 AM (33 days ago)

There was a gunman ,all things about him are cold,including his gun,eyes and heart.
ugg boots online
Finally,he died of coldness.
uggs online
However,with ugg,everything will totally change,not tragedy like this any more!

ugg boots online on 1/24/2010 9:42 PM (14 days ago)

Alot of people in the poker world don't know you can get full tilt rakeback for online poker rooms like full tilt.

rakeback on 1/31/2010 8:11 PM (7 days ago)

Most poker players need full tilt rakeback for bonus.

joeloss on 1/31/2010 8:36 PM (7 days ago)

The grass is greener on the other side of the fence.God helps those who help uggs outlet themselves.A little knowledge is dangerous.A good uggs sale medicine tastes bitter.

uggs online sale on 2/2/2010 9:02 PM (5 days ago)

Thanks for sharing such prolific techniques. Recently have found a prolific poker site that offers a good amount of rakeback with certain bonus. Just check it!

Poker on 2/8/2010 3:25 PM (3 hours ago)

Comment on this post:

Thanks for your interest in Coding the Wheel. All fields are optional.