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

Fast, Texas Holdem Hand Evaluation and Analysis

Rate me:
Please Sign up or sign in to vote.
4.89/5 (98 votes)
18 Apr 2006GPL313 min read 1.1M   18.7K   164  
A C# native, fast Texas Holdem Hand Evaluator.
// This control is covered by the LGPL Gnu license. See http://www.gnu.org/copyleft/lesser.html 
// for more information on this license.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using HoldemHand;

namespace OddsGrid
{
    /// <summary>
    /// 
    /// </summary>
    public partial class OddsGrid : UserControl
    {
        private string pocket = "";
        private string board = "";

        /// <summary>
        /// 
        /// </summary>
        public string Pocket
        {
            get { return pocket; }
            set { pocket = value; UpdateContents(); }
        }

        /// <summary>
        /// 
        /// </summary>
        public string Board
        {
            get { return board; }
            set { board = value; UpdateContents(); }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="v"></param>
        /// <returns></returns>
        private string FormatPercent(double v)
        {
            if (v != 0.0)
            {
                if (v * 100.0 >= 1.0)
                    return string.Format("{0:##0.0}%", v * 100.0);
                else
                    return "<1%";
            }
            return "n/a";
        }

        /// <summary>
        /// 
        /// </summary>
        private void UpdateContents()
        {
            if (!this.DesignMode)
            {
                int count = 0;
                double playerwins = 0.0;
                double opponentwins = 0.0;
                double[] player = new double[9];
                double[] opponent = new double[9];

                if (!Hand.ValidateHand(Pocket + " " + Board))
                {
                    ClearGrid();
                    return;
                }
                Hand.ParseHand(Pocket + " " + Board, ref count);

                // Don't allow these configurations because of calculation time.
                if (count == 0 || count == 1 || count == 3 || count == 4 || count > 7)
                {
                    ClearGrid();
                    return;
                }

                Hand.HandPlayerOpponentOdds(Pocket, Board, ref player, ref opponent);

                for (int i = 0; i < 9; i++)
                {
                    switch ((Hand.HandTypes)i)
                    {
                        case Hand.HandTypes.HighCard:
                            PlayerHighCard.Text = FormatPercent(player[i]);
                            OpponentHighCard.Text = FormatPercent(opponent[i]);
                            break;
                        case Hand.HandTypes.Pair:
                            PlayerPair.Text = FormatPercent(player[i]);
                            OpponentPair.Text = FormatPercent(opponent[i]);
                            break;
                        case Hand.HandTypes.TwoPair:
                            PlayerTwoPair.Text = FormatPercent(player[i]);
                            OpponentTwoPair.Text = FormatPercent(opponent[i]);
                            break;
                        case Hand.HandTypes.Trips:
                            Player3ofaKind.Text = FormatPercent(player[i]);
                            Opponent3ofaKind.Text = FormatPercent(opponent[i]);
                            break;
                        case Hand.HandTypes.Straight:
                            PlayerStraight.Text = FormatPercent(player[i]);
                            OpponentStraight.Text = FormatPercent(opponent[i]);
                            break;
                        case Hand.HandTypes.Flush:
                            PlayerFlush.Text = FormatPercent(player[i]);
                            OpponentFlush.Text = FormatPercent(opponent[i]);
                            break;
                        case Hand.HandTypes.FullHouse:
                            PlayerFullhouse.Text = FormatPercent(player[i]);
                            OpponentFullhouse.Text = FormatPercent(opponent[i]);
                            break;
                        case Hand.HandTypes.FourOfAKind:
                            Player4ofaKind.Text = FormatPercent(player[i]);
                            Opponent4ofaKind.Text = FormatPercent(opponent[i]);
                            break;
                        case Hand.HandTypes.StraightFlush:
                            PlayerStraightFlush.Text = FormatPercent(player[i]);
                            OpponentStraightFlush.Text = FormatPercent(opponent[i]);
                            break;
                    }
                    playerwins += player[i] * 100.0;
                    opponentwins += opponent[i] * 100.0;
                }

                PlayerWin.Text = string.Format("{0:##0.0}%", playerwins);
                OpponentWin.Text = string.Format("{0:##0.0}%", opponentwins);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        private void ClearGrid()
        {
            PlayerHighCard.Text = "";
            OpponentHighCard.Text = "";
            PlayerPair.Text = "";
            OpponentPair.Text = "";
            PlayerTwoPair.Text = "";
            OpponentTwoPair.Text = "";
            Player3ofaKind.Text = "";
            Opponent3ofaKind.Text = "";
            PlayerStraight.Text = "";
            OpponentStraight.Text = "";
            PlayerFlush.Text = "";
            OpponentFlush.Text = "";
            PlayerFullhouse.Text = "";
            OpponentFullhouse.Text = "";
            Player4ofaKind.Text = "";
            Opponent4ofaKind.Text = "";
            PlayerStraightFlush.Text = "";
            OpponentStraightFlush.Text = "";
            PlayerWin.Text = "";
            OpponentWin.Text = "";
        }

        /// <summary>
        /// 
        /// </summary>
        public OddsGrid()
        {
            InitializeComponent();
        }
    }
}

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)


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