Exemple #1
0
        public PokerHand(Card[] cards)
        {
            this.Cards = new Card[5];

            if (cards.Length != 5) // Poker hand must contain 5 cards
            {
                throw new Exception("Invalid pokerhand.");
            }

            for (int i = 0; i < 5; i++) // No two same cards are allowed in a single poker hand
            {
                for (int j = 0; j < 5; j++)
                {
                    if (i == j)
                    {
                        continue;
                    }
                    if (cards[i].Equals(cards[j]))
                    {
                        throw new Exception("Invalid pokerhand.");
                    }
                }
            }

            cards.CopyTo(this.Cards, 0);
            PokerCombination = PokerHelp.GetCombination(this, out coreValue);
            setValueCode();
        }
Exemple #2
0
        static List <List <Card> > GetStraightGroups(PokerCombination combination)
        {
            List <List <Card> > result = new List <List <Card> >();
            List <Card>         group  = new List <Card>();
            Card lastCard = new Card();
            int  count    = 1;
            var  dist     = combination.SortedCards.Distinct(new CardValueEqualityComparer()).ToList();

            foreach (Card c in dist)
            {
                if (count == 1)
                {
                    lastCard = c;
                }
                else
                {
                    if (c.Value == lastCard.Value - 1)
                    {
                        if (group.Count == 0)
                        {
                            group.Add(lastCard);
                        }
                        group.Add(c);
                        lastCard = c;
                    }
                    else
                    {
                        if (group.Count > 4)
                        {
                            result.Add(group);
                        }
                        group    = new List <Card>();
                        lastCard = c;
                    }

                    if (count == dist.Count)
                    {
                        if (c.Value == 2 && combination.SortedCards[0].Value == 14)
                        {
                            group.Add(combination.SortedCards[0]);
                        }
                        if (group.Count > 4)
                        {
                            result.Add(group);
                        }
                    }
                }
                count++;
            }
            return(result);
        }
Exemple #3
0
        public static bool GetPokerCombinations(Table table)
        {
            // Ako ima više od 2 handa na stolu i community cards barem flop.
            if (table.Hands.Count > 1 && table.Community.Cards.Count > 2)
            {
                table.PokerCombinations.Clear();
                foreach (Hand h in table.Hands)
                {
                    try
                    {
                        var pc = CreatePokerCombination(h, table.Community);
                        table.PokerCombinations.Add(pc);
                    }
                    catch (Exception e)
                    {
                        Debug.Write("Kreacija poker kombinacija nije uspjela! " + e.Message);
                    }
                }
            }
            table.PokerCombinations.Sort();
            table.PokerCombinations.Reverse();


            table.WinningHands.Clear();
            PokerCombination bestPC     = null;
            bool             firstCheck = true;

            foreach (PokerCombination pc in table.PokerCombinations)
            {
                if (firstCheck)
                {
                    firstCheck = false;
                    bestPC     = pc;
                    table.WinningHands.Add(pc.Hand);
                }
                else
                {
                    if (pc.CompareTo(bestPC) == 0)
                    {
                        table.WinningHands.Add(pc.Hand);
                    }
                    else
                    {
                        break;
                    }
                }
            }
            return(table.PokerCombinations.Count > 0);
        }
Exemple #4
0
        static List <List <Card> > GetFlushGroups(PokerCombination combination)
        {
            List <List <Card> > result        = new List <List <Card> >();
            List <Card>         groupClubs    = new List <Card>();
            List <Card>         groupDiamonds = new List <Card>();
            List <Card>         groupHearts   = new List <Card>();
            List <Card>         groupSpades   = new List <Card>();

            foreach (Card c in combination.SortedCards)
            {
                switch (c.Suit)
                {
                case Suit.Clubs:
                    groupClubs.Add(c);
                    break;

                case Suit.Diamonds:
                    groupDiamonds.Add(c);
                    break;

                case Suit.Hearts:
                    groupHearts.Add(c);
                    break;

                case Suit.Spades:
                    groupSpades.Add(c);
                    break;
                }
            }

            if (groupClubs.Count > 4)
            {
                result.Add(groupClubs);
            }
            if (groupDiamonds.Count > 4)
            {
                result.Add(groupDiamonds);
            }
            if (groupHearts.Count > 4)
            {
                result.Add(groupHearts);
            }
            if (groupSpades.Count > 4)
            {
                result.Add(groupSpades);
            }

            return(result);
        }
Exemple #5
0
        static List <List <Card> > GetSameValueGroups(PokerCombination combination)
        {
            List <List <Card> > result = new List <List <Card> >();
            var dist = combination.SortedCards.Distinct(new CardValueEqualityComparer());

            try
            {
                foreach (Card c in dist)
                {
                    List <Card> group = combination.SortedCards.Where(card => card.Value == c.Value).ToList();
                    if (group.Count > 1)
                    {
                        result.Add(group);
                    }
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine("Sortiranje po vrijednosti nije uspjelo! " + e.Message);
            }
            return(result);
        }
        static void Main(string[] args)
        {
            Stopwatch sw = new Stopwatch();
            PokerHand HandPlayer1, HandPlayer2;

            Card[]           current1   = new Card[5];
            Card[]           current2   = new Card[5];
            int              Player1Won = 0;
            int              Player2Won = 0;
            PokerCombination highest    = PokerCombination.High_Card;

            sw.Start();

            string[] pokerHands = File.ReadAllLines(@"C:\Users\Ben\Documents\Ben\Programmeren\C#\ProjectEuler\PE54 - Poker hands\PE54 - Poker hands\poker.txt");

            foreach (string Hands in pokerHands)
            {
                string[] cards = Hands.Split(new char[] { ' ' });
                for (int i = 0; i < cards.Length; i++)
                {
                    if (i < 5)
                    {
                        current1[i] = Poker.StringToCard(cards[i]);
                    }
                    else
                    {
                        current2[i - 5] = Poker.StringToCard(cards[i]);
                    }
                }

                HandPlayer1 = new PokerHand(current1);
                HandPlayer2 = new PokerHand(current2);

                //Console.Write("Player 1: " + HandPlayer1.PokerCombination + "\t||   Player 2: " + HandPlayer2.PokerCombination);

                if (HandPlayer1.PokerCombination > highest)
                {
                    highest = HandPlayer1.PokerCombination;
                }
                if (HandPlayer2.PokerCombination > highest)
                {
                    highest = HandPlayer2.PokerCombination;
                }

                if (HandPlayer1.ValueCode.CompareTo(HandPlayer2.ValueCode) > 0)
                {
                    //Console.WriteLine("   |---> Player 1 wins!");
                    Player1Won++;
                }
                else
                {
                    //Console.WriteLine("   |---> Player 2 wins!");
                    Player2Won++;
                }
            }

            sw.Stop();

            Console.WriteLine("Player 1 wins " + Player1Won + " times!");
            Console.WriteLine("The best combination was: " + highest);
            Console.WriteLine("Elapsed time: " + sw.ElapsedMilliseconds + " ms.");
        }
Exemple #7
0
        public static PokerCombination CreatePokerCombination(Hand hand, CommunityCards community)
        {
            PokerCombination result = new PokerCombination(hand, community);

            var straightGroups  = GetStraightGroups(result);
            var flushGroups     = GetFlushGroups(result);
            var sameValueGroups = GetSameValueGroups(result);

            // Check for Straight Flush
            if (straightGroups.Count > 0 && flushGroups.Count > 0)
            {
                List <List <Card> > sflushs = new List <List <Card> >();
                foreach (var flush in flushGroups)
                {
                    var str = GetStraightGroups(flush);
                    if (str.Count > 0)
                    {
                        sflushs.Add(str[0]);
                    }
                }
                if (sflushs.Count > 0)
                {
                    if (sflushs[0][0].Value == 14)
                    {
                        result.CombinationType = Combination.RoyalFlush;
                    }
                    else
                    {
                        result.CombinationType = Combination.StraightFlush;
                    }
                    result.HighCard = sflushs[0][0];
                    return(result);
                }
            }
            // Check for quads
            if (sameValueGroups.Count > 0)
            {
                foreach (var group in sameValueGroups)
                {
                    if (group.Count > 3)
                    {
                        result.CombinationType = Combination.FourOfAKind;
                        result.HighCard        = group[0];
                        result.HighGroup       = group;
                        var remain = result.SortedCards.Except(group).ToList();
                        result.Kickers.Add(remain[0]);
                        return(result);
                    }
                }
            }
            // Check for full house
            if (sameValueGroups.Count > 1)
            {
                var biggest = sameValueGroups.OrderByDescending(g => g.Count).ToList()[0];
                if (biggest.Count > 2)
                {
                    result.CombinationType = Combination.FullHouse;
                    result.HighCard        = biggest[0];
                    result.HighGroup       = biggest;
                    result.LowGroup        = sameValueGroups.OrderByDescending(g => g.Count).ToList()[1];
                    return(result);
                }
            }
            // Return if flush
            if (flushGroups.Count > 0)
            {
                result.CombinationType = Combination.Flush;
                result.HighGroup       = flushGroups[0];
                result.HighCard        = result.HighGroup[0];
                return(result);
            }
            // Return if straight
            if (straightGroups.Count > 0)
            {
                result.CombinationType = Combination.Straight;
                result.HighGroup       = straightGroups[0];
                result.HighCard        = result.HighGroup[0];
                return(result);
            }
            // Return if tris
            if (sameValueGroups.Count == 1 && sameValueGroups[0].Count == 3)
            {
                result.CombinationType = Combination.ThreeOfAKind;
                result.HighCard        = sameValueGroups[0][0];
                result.HighGroup       = sameValueGroups[0];
                var remain = result.SortedCards.Except(result.HighGroup).ToList();
                result.Kickers.Add(remain[0]);
                result.Kickers.Add(remain[1]);
                return(result);
            }
            // Return if 2 pairs
            if (sameValueGroups.Count > 1)
            {
                result.CombinationType = Combination.TwoPairs;
                result.HighCard        = sameValueGroups[0][0];
                result.HighGroup       = sameValueGroups[0];
                result.LowGroup        = sameValueGroups[1];
                var remain = result.SortedCards.Except(result.HighGroup).Except(result.LowGroup).ToList();
                result.Kickers.Add(remain[0]);
                return(result);
            }
            // Return if a pair
            if (sameValueGroups.Count == 1)
            {
                result.CombinationType = Combination.APair;
                result.HighCard        = sameValueGroups[0][0];
                result.HighGroup       = sameValueGroups[0];
                var remain = result.SortedCards.Except(result.HighGroup).ToList();
                result.Kickers.Add(remain[0]);
                result.Kickers.Add(remain[1]);
                result.Kickers.Add(remain[2]);
                return(result);
            }
            // If nothing else, it's a high card
            result.CombinationType = Combination.HighCard;
            result.HighCard        = result.SortedCards[0];
            result.Kickers.Add(result.SortedCards[1]);
            result.Kickers.Add(result.SortedCards[2]);
            result.Kickers.Add(result.SortedCards[3]);
            result.Kickers.Add(result.SortedCards[4]);
            return(result);
        }
Exemple #8
0
 public static string PokerCombinationToString(PokerCombination pc)
 {
     return(pc.ToString().Replace('_', ' '));
 }
Exemple #9
0
 /// <summary>
 /// Name
 /// </summary>
 public static String Name(this PokerCombination value) => value switch
 {