Poker probabilities and variance simulation
- 1 Introduction
- 2 Method
- 3 User interface
- 4 Model
- 5 Results
- 6 Conclusion
- 7 Code
- 8 Resources
The goal of my research, called „Poker probabilities and variance simulation“, is to make an own simulator tool in a traditional programming language, which will be able to simulate winning probabilities of starting poker cards. The main part of the research stands in graphical outputs of the simulator. The program will be able to produce graphs, which will be showing the results variance during Monte Carlo simulations of compared starting cards. This research describes the technology used for producing the tool and provides the readers with graphical results. I decided for the poker topic because it’s my favourite card game. And as a software developer, I see making an own simulator as a challange. The code itself is hosted as a .zip file, link provided.
• Project name: Poker probabilities and variance simulation
• Class: 4IT496 Simulation of Systems (WS 2014/2015)
• Author: Marian Zikmund
• Model type: Monte Carlo
In this part, the technogolies, which were used for making the simulator, are described. The simulator is accessible as a webpage (Single page application).
Programming workflow was as follows:
Setup the whole project
By this, several small procedures are meant. It is necessary and practical for later faster development to initiate a Git repository (for version control) and set the development environment correctly. I chose NodeJS environment as the most suitable one for this task. When starting a new project using NodeJS, it is very useful to use a tool called Gulp. It makes all tasks, which need to be made periodically, automatic.
Make the user interface
The UI is simple, using the classical Twitter Bootstrap CSS framework,which makes it look clean and simple. Therefore, no graphical tools (like Adobe Photoshop) were needed.
Glossary of the described terms:
HTML, CSS – the simulator works as a classical webpage, which can be opened in a web browser, so these technologies are necessary
Git – version control
Gulp – tool for automatization of repeatedly maked tasks
Twitter Bootstrap – frontend CSS framework for easier coding without caring too much about the design
When a new sumulation is being configured, the user interface is maximally simple, al lit asks for are the Hero’s preflop cards, Villain’s preflop cards and number of rounds to simulate (Monte Carlo method).
Results of the simulation
After the simulation is started (clicking on the „Count the probability“ button), whole box smoothly travels to the top (to make more space), and the result percentages and graphics appear.
This is the actual structure of the simulator and the description of its logic:
The main classes: App – Simulate – AllCards - GenerateCard
At the beginning, the user provides the application with the starting hands for two players (these are the hands, for which strength we are looking for). These are the starting parameters of the simulation. After this, an XMLHttpRequest is being made on the server, with exactly these parameters, plus the number of rounds for the Monte Carlo simulation. These parameters get to the main class – App. Here, the method simulate(parameters) from the class Simulate is invoked.
This class has the main method simulate(parameters), which invokes method play(parameters) with the same parameters. Method play is the bootstraping part. It calls the methods makeBoard(startingCards) and getBoard() on the class AllCards.
The two main methods here are makeBoard(startingCards) and getBoard(). As static variables, an array of all possible cards in the game is made. When the function makeBoard(startingCards) is invoked, it makes a new array of randomly generated cards. The parameter startingCards are the cards, already being held by the two players (the compared cards), therefore these cards cannot be generated anymore. This function also „generates“ the five cards on the board (as in the Texas Hold’em variant of poker game). It does so by calling method addCard(cardsAlready,allCards) from the class GenerateCard.
Method getBoard() simply returns the array with the randomly generated cards on the board.
The main method of this class is AddCard(cardsAlready, allCards). The first parameter is supposed to be an array with cards, which cannot be generated anymore (are either in the player’s hands or generated in the preceding generating for the board). Parameter allCards stands for all the possible cards in the game, as being defined earlier.
This is the hearth of the server side of this simulator. After calling the script from the frontend part via AJAX, the method http.createServer() is invoked. At the beginning of this method, the basic parameters of the simulation are read: heroCards, villainCards and rounds for Monte Carlo simulation. Knowing these, the actual simulation may begin. This method contains a loop,which is run as many times as it is set in the rounds property. Each iteration, the board is generated (as described in the AllCards class). For each of these iterations, the result has to be counted. By the result, I mean – who wins this current round (iteration). This number is held in the property herowon. In the end, we get the percentage by dividing the property herowon by the number of rounds.
For comparison of the cards strengths, a special module called „poker-evaluator“ is used. It was made a school project by a student from the USA. It is just a small class, with a main method evaluate(hands), which takes all of the cards and calculates the best possible combination and returns a value of its strength. This is made for both players and the one with higher value wins.
Table of randomly chosen cards and rounds with the results probabilities:
Graphs showing randomly chosen cards and how the variance is reduced with the number of simulated rounds (using Monte Carlo method, results of player 1 – hero - shown ):
As9s vs. Ts8s, 10 rounds, 80%
Ts9s vs. 7h4d, 30 rounds, 63,333333%
JsJd vs. Ah2d, 100 rounds, 76%
QsAd vs. Kh5d, 500 rounds, 65.8 %
The simulator provides the requested results with an automatically generated image line graph for variance demonstration. The simulator tool itself can be downloaded directly from the provided GitHub directory (beware of the necessity of having NodeJS preinstalled).
It is interesting to see the numbers of iterations, which are necessary for the Monte Carlo simulation to provide us with at least a quite precise result.
You can view the prepared code, with necessary plugins and so on here: https://dl.dropboxusercontent.com/u/63085939/poker.zip (it is only necessary to have NodeJS environment preinstalled).