Evaluate a Poker Hand C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace FiveCardArray_CSharp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            string[] CardValues = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace" };
            string[] CardSuits = { "Hearts", "Spades", "Clubs", "Diamonds" };
            foreach (string value in CardValues)
            {
                ddlValue1.Items.Add(value);
                ddlValue2.Items.Add(value);
                ddlValue3.Items.Add(value);
                ddlValue4.Items.Add(value);
                ddlValue5.Items.Add(value);
            }
            foreach (string value in CardSuits)
            {
                ddlSuit1.Items.Add(value);
                ddlSuit2.Items.Add(value);
                ddlSuit3.Items.Add(value);
                ddlSuit4.Items.Add(value);
                ddlSuit5.Items.Add(value);
            }
        }
        private static int GetSuit(string DropDownSuit)
        {
            int retVal = 4;
            switch (DropDownSuit)
            {
                case "Clubs":
                    retVal = 0;
                    break;
                case "Diamonds":
                    retVal = 1;
                    break;
                case "Hearts":
                    retVal = 2;
                    break;
                case "Spades":
                    retVal = 3;
                    break;
            }
            return retVal;
        }
        private static int GetFace(string DropDownFace)
        {
            int retVal = 13;
            switch (DropDownFace)
            {
                case "Ace":
                    retVal = 0;
                    break;
                case "2":
                    retVal = 1;
                    break;
                case "3":
                    retVal = 2;
                    break;
                case "4":
                    retVal = 3;
                    break;
                case "5":
                    retVal = 4;
                    break;
                case "6":
                    retVal = 5;
                    break;
                case "7":
                    retVal = 6;
                    break;
                case "8":
                    retVal = 7;
                    break;
                case "9":
                    retVal = 8;
                    break;
                case "10":
                    retVal = 9;
                    break;
                case "Jack":
                    retVal = 10;
                    break;
                case "Queen":
                    retVal = 11;
                    break;
                case "King":
                    retVal = 12;
                    break;
            }
            return retVal;
        }
        private void btnGetHand_Click(object sender, EventArgs e)
        {
            if (ddlSuit1.Text == "" | ddlSuit2.Text == "" | ddlSuit3.Text == "" | ddlSuit4.Text == "" | ddlSuit5.Text == "" |
              ddlValue1.Text == "" | ddlValue2.Text == "" | ddlValue3.Text == "" | ddlValue4.Text == "" | ddlValue5.Text == "")
            {
                MessageBox.Show("All boxes need to have a value.");
            }
            else
            {
                lblHandDisplay.Text = CreateHand();
            }
        }
        private string CreateHand()
        {
            string retHand = "";
            int[,] hand = new int[3, 12];
            //1st Card
            hand[GetSuit(ddlSuit1.Text), GetFace(ddlValue1.Text)] = 1;
            //2nd Card
            hand[GetSuit(ddlSuit2.Text), GetFace(ddlValue2.Text)] = 1;
            //3rd Card
            hand[GetSuit(ddlSuit3.Text), GetFace(ddlValue3.Text)] = 1;
            //4th Card
            hand[GetSuit(ddlSuit4.Text), GetFace(ddlValue4.Text)] = 1;
            //5th Card
            hand[GetSuit(ddlSuit5.Text), GetFace(ddlValue5.Text)] = 1;
            if (IsFlush(hand) && IsStraight(hand))
            {
                retHand = "Straight Flush";
            }
            else if (IsFourOfAKind(hand))
            {
                retHand = "4 of a Kind";
            }
            else if (IsThreeOfAKind(hand) && IsPair(hand))
            {
                retHand = "Full House";
            }
            else if (IsFlush(hand))
            {
                retHand = "Flush";
            }
            else if (IsStraight(hand))
            {
                retHand = "Straight";
            }
            else if (IsThreeOfAKind(hand))
            {
                retHand = "3 of a Kind";
            }
            else if (IsTwoPair(hand))
            {
                retHand = "2 Pairs";
            }
            else if (IsPair(hand))
            {
                retHand = "a Pair";
            }
            else
            {
                retHand = "Nothing";
            }
            return retHand;
        }
        private bool IsFlush(int[,] hand)
        {
            bool retVal = false;
            int fCount = 0, x = 0, y = 0;
            do
            {
                if (hand[x, y] == 1)
                {
                    fCount++;
                }
                y++;
                if (fCount == 5)
                {
                    retVal = true;
                    break;
                }
                else if (fCount != 5 && y == 13 && x < 4)
                {
                    y = 0;
                    x++;
                    fCount = 0;
                }
                else if (fCount != 5 && y == 13 && x == 4)
                {
                    break;
                }
            }
            while (x < 4 && y < 13);
            return retVal;
        }
        private bool IsFourOfAKind(int[,] hand)
        {
            bool retVal = false;
            int fKCount = 0, x = 0, y = 0;
            do
            {
                do
                {
                    if (hand[x, y] == 1)
                    {
                        fKCount++;
                    }
                    x++;
                }
                while (fKCount < 5 && x < 4);
                if (fKCount == 4)
                {
                    retVal = true;
                    break;
                }
                else if (fKCount > 4)
                {
                    break;
                }
                else
                {
                    y++;
                    x = 0;
                    fKCount = 0;
                }
            }
            while (x < 4 && y < 13);
            return retVal;
        }
        private bool IsStraight(int[,] hand)
        {
            bool retVal = false;
            int sCount = 0, total = 0, x = 0, y = 0;
            do
            {
                total = 0;
                x = 0;
                do
                {
                    total = total + hand[x, y];
                    x++;
                }
                while (x < 4);
                if (total > 1)
                {
                    break;
                }
                else if (total == 1)
                {
                    sCount++;
                    if (sCount == 5)
                    {
                        retVal = true;
                        break;
                    }
                }
                else if (total == 0)
                {
                    sCount = 0;
                }
                y++;
            }
            while (y < 13);
            if (sCount == 5)
            {
                return retVal;
            }
            else
            {
                int Aces, Kings, Queens, Jacks, Tens;
                Aces = hand[0, 0] + hand[1, 0] + hand[2, 0] + hand[3, 0];
                Kings = hand[0, 12] + hand[1, 12] + hand[2, 12] + hand[3, 12];
                Queens = hand[0, 11] + hand[1, 11] + hand[2, 11] + hand[3, 11];
                Jacks = hand[0, 10] + hand[1, 10] + hand[2, 10] + hand[3, 10];
                Tens = hand[0, 9] + hand[1, 9] + hand[2, 9] + hand[3, 9];
                if (Aces + Kings + Queens + Jacks + Tens == 5)
                {
                    retVal = true;
                }
                return retVal;
            }
        }
        private bool IsThreeOfAKind(int[,] hand)
        {
            bool retVal = false;
            int x = 0, y = 0, tKCount = 0;
            do
            {
                do
                {
                    if (hand[x, y] == 1)
                    {
                        tKCount++;
                    }
                    x++;
                }
                while (tKCount < 4 && x < 4);
                if (tKCount == 3)
                {
                    retVal = true;
                    break;
                }
                else if (tKCount > 3)
                {
                    break;
                }
                else
                {
                    y++;
                    x = 0;
                    tKCount = 0;
                }
            }
            while (x < 4 && y < 13);
            return retVal;
        }
        private bool IsPair(int[,] hand)
        {
            bool retVal = false;
            int x = 0;
            int y = 0;
            int pCount = 0;
            do
            {
                do
                {
                    if (hand[x, y] == 1)
                    {
                        pCount = pCount + 1;
                    }
                    x = x + 1;
                }
                while (pCount < 3 && x < 4);
                if (pCount == 2)
                {
                    retVal = true;
                    break;
                }
                else if (pCount > 2 && y > 12)
                {
                    break;
                }
                else if (pCount <= 3)
                {
                    y++;
                    x = 0;
                    pCount = 0;
                }
            }
            while (x < 4 && y < 13);
            return retVal;
        }
        private bool IsTwoPair(int[,] hand)
        {
            bool retVal = false;
            int x = 0, y = 0, pCount = 0, p2Count = 0;
            do
            {
                do
                {
                    if (hand[x, y] == 1)
                    {
                        pCount++;
                    }
                    x++;
                }
                while (pCount < 3 && x < 4);
                if (pCount == 2)
                {
                    break;
                }
                else if (pCount > 2)
                {
                    break;
                }
                else
                {
                    y++;
                    x = 0;
                    pCount = 0;
                }
            }
            while (x < 4 && y < 13);
            y++;
            x = 0;
            if (pCount == 2)
            {
                do
                {
                    do
                    {
                        if (hand[x, y] == 1)
                        {
                            p2Count++;
                        }
                        x++;
                    }
                    while (p2Count < 3 && x < 4);
                    if (pCount == 2 && p2Count == 2)
                    {
                        retVal = true;
                        break;
                    }
                    else if (p2Count > 2)
                    {
                        break;
                    }
                    else
                    {
                        y++;
                        x = 0;
                        p2Count = 0;
                    }
                }
                while (x < 4 && y < 13);
                return retVal;
            }
            else
            {
                return retVal;
            }
        }
    }
}

One Comment

  1. Ping from Evaluate a Poker Hand from a 2D Array in C# and VB.NET » PSxPoker.com:

    […] C#  Source code to evaluate a 5 card poker hand […]

please comment to add input to the next build


Hit Counter provided by shuttle service from lax