Click here to Skip to main content
12,511,383 members (124,873 online)
Click here to Skip to main content

Stats

176.1K views
3.2K downloads
114 bookmarked
Posted

More Texas Holdem Analysis in C#: Part 1

, 20 May 2008 GPL3
Using C# to do sophisticated analysis of Texas Holdem
Hand Evaluator
Applications
Benchmark
Benchmark.csproj.user
Properties
vssver2.scc
vssver2.scc
ZedGraph.dll
MultiOddsApp
Properties
vssver2.scc
vssver2.scc
MultiOddsGrid
Properties
vssver2.scc
vssver2.scc
MultiOppOdds
Properties
vssver2.scc
vssver2.scc
ZedGraph.dll
OddsGrid
Properties
vssver2.scc
vssver2.scc
OddsGridApp
Properties
vssver2.scc
vssver2.scc
bin
Debug
ZedGraph.dll
Release
ZedGraph.dll
HandEvaluator
bin
Debug
Release
HandEvaluator.dll
HandEvaluator.pdb
HandEvaluator.csproj.user
PocketQueryParser
Exam.lexer
Exam.parser
lg.exe
pg.exe
Runtime
vssver2.scc
vssver2.scc
Properties
vssver2.scc
vssver2.scc
Samples
PocketHandExamples
Properties
WinOddsExhaustive
Properties
WinOddsMonteCarlo
Properties
WinOddsMultipleOpponents
Properties
WinOddsMultipleOpponentsTable
Properties
WinOddsMultipleThreads
Properties
using System;
using HoldemHand;

// This example calculates the win odds for a player having "As Ks" against 
// 1-9 random players. The results is comma separated so that it can be imported
// into Excel using the .csv file type.
namespace WinOddsMultipleOpponentsTable
{
    class Program
    {
        static void Main(string[] args)
        {
            const double time = 5.0;
            ulong board = Hand.ParseHand("");
            ulong dead = Hand.ParseHand("");

            // Table Header
            Console.Write(",");
            for (int i = 1; i <= 9; i++)
            {
                Console.Write("{0},", i);
            }
            Console.WriteLine();

            // Iterates through one representative hand of each of the 169 possible
            // pocket hand types
            foreach (ulong pocket in PocketHands.Hands169())
            {
                // Show Pocker Hand
                Console.Write("\"{0}\",", Hand.MaskToString(pocket));

                // Calculate and Display the Approximate odds for 1-9 opponents
                for (int opponents = 1; opponents <= 9; opponents++)
                {
                    Console.Write("{0}%,", WinOddsMonteCarlo(pocket, board, dead, opponents, time) * 100.0);
                }
                Console.WriteLine();
            } 
        }

        // An example of how to calculate win odds for multiple players
        static double WinOddsMonteCarlo(ulong pocket, ulong board, ulong dead, int nopponents, double duration)
        {
            System.Diagnostics.Debug.Assert(nopponents > 0 && nopponents <= 9);
            System.Diagnostics.Debug.Assert(duration > 0.0);
            System.Diagnostics.Debug.Assert(Hand.BitCount(pocket) == 2);
            System.Diagnostics.Debug.Assert(Hand.BitCount(board) >= 0 && Hand.BitCount(board) <= 5);

            // Keep track of stats
            double win = 0.0, count = 0.0;

            // Keep track of time
            double start = Hand.CurrentTime;

            // Loop for specified time duration
            while ((Hand.CurrentTime-start) < duration)
            {
                // Player and board info
                ulong boardmask = Hand.RandomHand(board, dead | pocket, 5);
                uint playerHandVal = Hand.Evaluate(pocket | boardmask);

                // Ensure that dead, board, and pocket cards are not
                // available to opponent hands.
                ulong deadmask = dead | boardmask | pocket;

                // Comparison Results
                bool greaterthan = true;
                bool greaterthanequal = true;

                // Get random opponent hand values
                for (int i = 0; i < nopponents; i++)
                {
                    // Get Opponent hand info
                    ulong oppmask = Hand.RandomHand(deadmask, 2);
                    uint oppHandVal = Hand.Evaluate(oppmask | boardmask);

                    // Remove these opponent cards from future opponents
                    deadmask |= oppmask;

                    // Determine compare status
                    if (playerHandVal < oppHandVal)
                    {
                        greaterthan = greaterthanequal = false;
                        break;
                    }
                    else if (playerHandVal <= oppHandVal)
                    {
                        greaterthan = false;
                    }
                }

                // Calculate stats
                if (greaterthan)
                    win += 1.0;
                else if (greaterthanequal)
                    win += 0.5;

                count += 1.0;
            }

            // Return stats
            return (count == 0.0 ? 0.0 : win/count);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)

Share

About the Author

Keith Rule
Software Developer (Senior)
United States United States
I work at Tektronix in Beaverton OR. I've been programming for fun since 1975 (I started while in a Computer Explorer Scout group in Spokane WA). I've been programming in C since 1979 and I've been working professionally since 1983.

I really enjoy www.codeproject.com. It has saved me an incredible amount of time. I only hope my small contributions have given back some of what I've taken.

You may also be interested in...

Pro
Pro
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160929.1 | Last Updated 20 May 2008
Article Copyright 2007 by Keith Rule
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid