Click here to Skip to main content
11,504,184 members (73,701 online)
Click here to Skip to main content
Add your own
alternative version

More Texas Holdem Analysis in C#: Part 1

, 20 May 2008 GPL3 156.8K 2.7K 114
Using C# to do sophisticated analysis of Texas Holdem
hand_evaluator.zip
Hand Evaluator
Applications
Benchmark
Benchmark.csproj.user
Properties
Settings.settings
vssver2.scc
vssver2.scc
ZedGraph.dll
MultiOddsApp
Properties
Settings.settings
vssver2.scc
vssver2.scc
MultiOddsGrid
Properties
vssver2.scc
vssver2.scc
MultiOppOdds
Properties
Settings.settings
vssver2.scc
vssver2.scc
ZedGraph.dll
OddsGrid
Properties
vssver2.scc
vssver2.scc
OddsGridApp
Properties
Settings.settings
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
namespace WinOddsMultipleOpponents
{
    class Program
    {
        // Expected output (approximate values)
        // 1: win 67.08%
        // 2: win 50.82%
        // 3: win 41.53%
        // 4: win 35.46%
        // 5: win 31.13%
        // 6: win 27.82%
        // 7: win 24.99%
        // 8: win 22.77%
        // 9: win 20.77%
        static void Main(string[] args)
        {
            const double time = 5.0;
            ulong pocket = Hand.ParseHand("As Ks");
            ulong board = Hand.ParseHand("");
            ulong dead = Hand.ParseHand("");

            for (int opponents = 1; opponents <= 9; opponents++)
            {
                Console.WriteLine("{0}: win {1:0.00}%", 
                    opponents, WinOddsMonteCarlo(pocket, board, dead, opponents, time) * 100.0);
            }
        }

        // 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.

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