lee croydon

lee croydon

  • NA
  • 48
  • 11.6k

evaluating a straight draw in a poker hand

Nov 8 2014 6:53 AM
Hi,
 
I am working on a poker game and have become a little bogged down with testing to see if the players hand plus the table cards evaluate to a straight.
 
the code i have so far recognises  4 of kind, full house, three of a kind, two pairs, single pair and high card, however, when it comes to the straight it either produces a straight when only 4 cards are consecutive or produces a high card reading. 
 
the code i have so far is below, any help on this would be very much appreciated
 
Thanks in advance Lee
 
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LeeCroydon.PokerGame
{
    public enum Suit { Clubs, Diamonds, Hearts, Spades }

    public enum CardNumber { Ace, King, Queen, Jack, Ten, Nine, Eight, Seven, Six, Five, Four, Three, Two }


    public class Card
    {
        private static Random rand = new Random();

        public Suit suit;
        public CardNumber number;
        public bool isUsed = false;

        public Card()
        {
            suit = (Suit)rand.Next(0, 4);
            number = (CardNumber)rand.Next(0, 13);
        }

        public void showCard()
        {

            Console.WriteLine("{0} of {1}", this.number, this.suit);
        }

        public void Deck(List<Card> c)
        {
            do
            {
                Card card = new Card();
                bool exists = c.Exists(x => x.suit == card.suit && x.number == card.number);
                if (exists == false)
                {
                    c.Add(card);
                }
            }
            while (c.Count < 52);
        }
        class CardValueComparer : IEqualityComparer<Card>
        {
            public bool Equals(Card c1, Card c2)
            {
                return c1.number == c2.number;
            }
            public int GetHashCode(Card c)
            {
                return c.number.GetHashCode();
            }
        }
        class Game
        {
            static string[] name1 = { "Ace", "King", "Queen", "Jack", "Ten", "Nine", "Eight", "Seven", "Six", "Five", "Four", "Three", "Two" };

            static string[] name2 = { "Aces", "Kings", "Queens", "Jacks", "Tens", "Nines", "Eights", "Sevens", "Sixs", "Fives", "Fours", "Threes", "Twos" };

            static string[] suitsNames = { "Clubs", "Diamonds", "Hearts", "Spades" };

            static void Main(string[] args)
            {
                Card card = new Card();
                List<Card> deckOfCards = new List<Card>();
                card.Deck(deckOfCards);

                List<Card> allCards = new List<Card>();
                var playersHand = deckOfCards.Take(2);

                foreach (Card c in playersHand)
                {
                    c.showCard();
                    allCards.Add(c);
                }
                deckOfCards.RemoveRange(0, 2);

                var burnCard = deckOfCards.Take(1);

                foreach (Card c in burnCard)
                {
                    deckOfCards.Remove(c);
                    Console.WriteLine("Card burnt");
                }

                var flop = deckOfCards.Take(3);


                Console.WriteLine("Flop:");
                foreach (Card c in flop)
                {
                    c.showCard();
                    allCards.Add(c);
                }
                deckOfCards.RemoveRange(0, 3);

                burnCard = deckOfCards.Take(1);

                foreach (Card c in burnCard)
                {
                    c.isUsed = true;
                    Console.WriteLine("Card burnt");
                }

                var turn = deckOfCards.Take(1);

                Console.WriteLine("Turn:");
                foreach (Card c in turn)
                {
                    c.showCard();
                    deckOfCards.Remove(c);
                    allCards.Add(c);
                }

                burnCard = deckOfCards.Take(1);
                card.isUsed = deckOfCards.Contains(card);

                foreach (Card c in burnCard)
                {
                    deckOfCards.Remove(c);
                    Console.WriteLine("Card burnt");
                }

                var river = deckOfCards.Take(1);

                Console.WriteLine("River:");
                foreach (Card c in river)
                {
                    c.showCard();
                    deckOfCards.Remove(c);
                    allCards.Add(c);
                }

                // sort by multiples

                Card[] hand = new Card[7];
                hand = allCards.ToArray();

                var groupings = from aCard in hand
                                group aCard by aCard.number into g
                                orderby g.Count() descending, g.Key
                                select g;


                int handType = -1;
                string myHand = null;

                //check for card multiples

                int count1 = groupings.First().Count();
                int count2 = groupings.ElementAt(1).Count();
                int value1 = (int)groupings.First().Key;
                int value2 = (int)groupings.ElementAt(1).Key;

                //hand = (from aCard in hand orderby aCard.number select card).ToArray();

                Card[] distinctCards = hand.Distinct(new CardValueComparer()).ToArray();

                bool straight = false;

                if (count1 == 4)
                {
                    handType = 1;
                    myHand = "Four " + name2[value1];
                }
                else if (count1 == 3 && count2 >= 2)
                {
                    handType = 2;
                    myHand = "Full House- " + name2[value1] + " and " + name2[value2];
                }
                else if (count1 == 3)
                {
                    handType = 5;
                    myHand = "Three " + name2[value1];
                }
                else if (count1 == 2 && count2 == 2)
                {
                    handType = 6;
                    myHand = "Two Pairs- " + name2[value1] + " and " + name2[value2];
                }
                else if (count1 == 2)
                {
                    handType = 7;
                    myHand = "Pair of " + name2[value1];
                }
                else 
                {
                    handType = 8;
                    myHand = name1[value1] + " High";
                }
                if (distinctCards.Length >= 5)
                {
                    for (int i = 0; i < distinctCards.Length - 4; i++)
                    {
                        if (distinctCards[i].number == distinctCards[i + 4].number - 4)
                        {
                            if (handType > 4)
                            {
                                myHand = hand[(int)distinctCards[i].number] + " high striaght";
                                handType = 4;
                            }
                            straight = true;
                            break;
                        }
                    }
                }
               Console.WriteLine(myHand);
          Console.ReadKey();

Answers (2)