Click here to Skip to main content
15,896,154 members
Articles / Programming Languages / C#

More Texas Holdem Analysis in C#: Part 2

Rate me:
Please Sign up or sign in to vote.
4.93/5 (32 votes)
20 May 2008LGPL327 min read 144.8K   2.9K   76  
Using C# to do sophisticated analysis of Texas Holdem
using System;
using System.Collections.Generic;
using HoldemHand;

namespace WinOddsExhaustive
{
    class Program
    {
        static void Main(string[] args)
        {
            // This code calculates the probablity of As Ks winning against 
            // another random hand.            
            ulong pocketmask = Hand.ParseHand("As Ks");     // Hole hand            
            ulong board = Hand.ParseHand("");               // No board cards yet  
            long wins = 0, ties = 0, count = 0;             // Iterate through all possible opponent hole cards         
            double start = Hand.CurrentTime;

            // Iterate through all possible opponent hands
            foreach (ulong oppmask in Hand.Hands(0UL, board | pocketmask, 2))
            {
                // Iterate through all possible board cards                
                foreach (ulong boardmask in Hand.Hands(board, pocketmask | oppmask, 5))
                {
                    // Evaluate the player and opponent hands and tally the results    
                    uint pocketHandVal = Hand.Evaluate(pocketmask | boardmask, 7);
                    uint oppHandVal = Hand.Evaluate(oppmask | boardmask, 7);
                    if (pocketHandVal > oppHandVal)
                    {
                        wins++;
                    }
                    else if (pocketHandVal == oppHandVal)
                    {
                        ties++;
                    }
                    count++;
                }
            }
            // Prints: Win 67.0446323092352%            
            Console.WriteLine("Win {0}%, Elapsed Time {1}",
                (((double)wins) + ((double)ties) / 2.0) / ((double)count) * 100.0, 
                Hand.CurrentTime-start);

            
        }
    }
}



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 Lesser General Public License (LGPLv3)


Written By
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.

Comments and Discussions