Exemplo n.º 1
0
        public HandStrength GetSevenCardStrength(ulong card, HashMap handRankMap)
        {
            var cards = new List <int>();

            for (int i = 0; i < 52; i++)
            {
                if (((1ul << i) & card) > 0)
                {
                    cards.Add(i);
                }
            }

            var subsets          = new Combinatorics.Collections.Combinations <int>(cards, 5);
            var currentBestValue = ulong.MinValue;
            var maxSubset        = 0ul;

            foreach (List <int> subset in subsets)
            {
                ulong subsetBitmap = subset.Aggregate(0ul, (acc, el) => acc | (1ul << el));
                var   currentValue = handRankMap[subsetBitmap];

                if (currentValue > currentBestValue)
                {
                    currentBestValue = currentValue;
                    maxSubset        = subsetBitmap;
                }
            }


            return(new Hand(maxSubset).GetStrength());
        }
Exemplo n.º 2
0
        public List <List <Summarizator> > allPossibleSummarizatorCombinations()
        {
            List <List <Summarizator> > SummarizatorsCombinations = new List <List <Summarizator> >();

            for (int i = 1; i <= Summarizators.Count; i++)
            {
                Combinatorics.Collections.Combinations <Summarizator> c = new Combinatorics.Collections.Combinations <Summarizator>(Summarizators, i);
                foreach (List <Summarizator> v in c)
                {
                    SummarizatorsCombinations.Add(v);
                }
            }
            return(SummarizatorsCombinations);
        }
Exemplo n.º 3
0
        private void GenerateSevenCardTable()
        {
            var sourceSet    = Enumerable.Range(1, 52).ToList();
            var combinations = new Combinatorics.Collections.Combinations <int>(sourceSet, 7);
            int count        = 0;

            foreach (List <int> cards in combinations)
            {
                if (debug && count++ % 1000 == 0)
                {
                    Console.Write("{0} / {1}\r", count, combinations.Count);
                }
                var subsets      = new Combinatorics.Collections.Combinations <int>(cards, 6);
                var subsetValues = new List <ulong>();
                foreach (List <int> subset in subsets)
                {
                    ulong subsetBitmap = subset.Aggregate(0ul, (acc, el) => acc | (1ul << el));
                    subsetValues.Add(handRankMap[subsetBitmap]);
                }
                ulong bitmap = cards.Aggregate(0ul, (acc, el) => acc | (1ul << el));
                handRankMap[bitmap] = subsetValues.Max();
            }
        }
Exemplo n.º 4
0
        private void GenerateMonteCarloMap(int iterations)
        {
            monteCarloMap = new Dictionary <ulong, ulong>();
            var sourceSet    = Enumerable.Range(1, 52).ToList();
            var combinations = new Combinatorics.Collections.Combinations <int>(sourceSet, 2);
            int count        = 0;

            foreach (List <int> cards in combinations)
            {
                Console.Write("{0}\r", count++);

                ulong bitmap = cards.Aggregate(0ul, (acc, el) => acc | (1ul << el));
                var   hand   = new Hand(bitmap);
                var   deck   = new Deck(removedCards: bitmap);

                ulong evaluationSum = 0;
                for (int i = 0; i < iterations; i++)
                {
                    if (deck.CardsRemaining < 13)
                    {
                        deck.Shuffle();
                    }
                    evaluationSum += handRankMap[bitmap | deck.Draw(5)];
                }

                monteCarloMap[bitmap] = evaluationSum / (ulong)iterations;
            }

            foreach (KeyValuePair <ulong, ulong> kvp in monteCarloMap.OrderBy(kvp => kvp.Value))
            {
                var hand = new Hand(kvp.Key);
                hand.PrintColoredCards("\t");
                Console.WriteLine(kvp.Value);
                handRankMap[kvp.Key] = kvp.Value;
            }
            Console.ReadLine();
        }
Exemplo n.º 5
0
        private void GenerateSevenCardTable()
        {
            var sourceSet    = Enumerable.Range(0, 52).ToList();
            var combinations = new Combinatorics.Collections.Combinations <int>(sourceSet, 7);

            using var progress = new ProgressBar();
            long sharedLoopCounter = 0;

            foreach (List <int> cards in combinations)
            {
                var subsets      = new Combinatorics.Collections.Combinations <int>(cards, 6);
                var subsetValues = new List <ulong>();
                foreach (List <int> subset in subsets)
                {
                    ulong subsetBitmap = subset.Aggregate(0ul, (acc, el) => acc | (1ul << el));
                    subsetValues.Add(handRankMap[subsetBitmap]);
                }
                ulong bitmap = cards.Aggregate(0ul, (acc, el) => acc | (1ul << el));
                handRankMap[bitmap] = subsetValues.Max();

                sharedLoopCounter++;
                progress.Report((double)sharedLoopCounter / combinations.Count, sharedLoopCounter);
            }
        }
Exemplo n.º 6
0
        private void GenerateFiveCardTable()
        {
            var sourceSet    = Enumerable.Range(0, 52).ToList();
            var combinations = new Combinatorics.Collections.Combinations <int>(sourceSet, 5);

            // Generate all possible 5 card hand bitmaps
            Console.WriteLine("Generating bitmaps");
            var handBitmaps = new List <ulong>();
            int count       = 0;

            foreach (List <int> values in combinations)
            {
                if (debug && count++ % 1000 == 0)
                {
                    Console.Write("{0} / {1}\r", count, combinations.Count);
                }
                handBitmaps.Add(values.Aggregate(0ul, (acc, el) => acc | (1ul << el)));
            }

            // Calculate hand strength of each hand
            Console.WriteLine("Calculating hand strength");
            var handStrengths = new Dictionary <ulong, HandStrength>();

            count = 0;
            foreach (ulong bitmap in handBitmaps)
            {
                if (debug && count++ % 1000 == 0)
                {
                    Console.Write("{0} / {1}\r", count, handBitmaps.Count);
                }
                var hand = new Hand(bitmap);
                handStrengths.Add(bitmap, hand.GetStrength());
            }

            // Generate a list of all unique hand strengths
            Console.WriteLine("Generating equivalence classes");
            var uniqueHandStrengths = new List <HandStrength>();

            count = 0;
            foreach (KeyValuePair <ulong, HandStrength> strength in handStrengths)
            {
                if (debug && count++ % 1000 == 0)
                {
                    Console.Write("{0} / {1}\r", count, handStrengths.Count);
                }
                Utilities.BinaryInsert <HandStrength>(uniqueHandStrengths, strength.Value);
            }
            Console.WriteLine("{0} unique hand strengths", uniqueHandStrengths.Count);

            // Create a map of hand bitmaps to hand strength indices
            Console.WriteLine("Creating lookup table");
            count = 0;
            foreach (ulong bitmap in handBitmaps)
            {
                if (debug && count++ % 1000 == 0)
                {
                    Console.Write("{0} / {1}\r", count, handBitmaps.Count);
                }
                var          hand        = new Hand(bitmap);
                HandStrength strength    = hand.GetStrength();
                var          equivalence = Utilities.BinarySearch <HandStrength>(uniqueHandStrengths, strength);
                if (equivalence == null)
                {
                    throw new Exception(string.Format("{0} hand not found", hand));
                }
                else
                {
                    handRankMap[bitmap] = (ulong)equivalence;
                }
            }
        }
Exemplo n.º 7
0
        private void GenerateFiveCardTable()
        {
            var sourceSet    = Enumerable.Range(0, 52).ToList();
            var combinations = new Combinatorics.Collections.Combinations <int>(sourceSet, 5);

            // Generate all possible 5 card hand bitmaps
            var handBitmaps = new List <ulong>();

            using (var progress = new ProgressBar())
            {
                long sharedLoopCounter = 0;

                foreach (List <int> values in combinations)
                {
                    handBitmaps.Add(values.Aggregate(0ul, (acc, el) => acc | (1ul << el)));
                    sharedLoopCounter++;
                    progress.Report((double)sharedLoopCounter / combinations.Count, sharedLoopCounter);
                }
            }

            // Calculate hand strength of each hand
            var handStrengths = new Dictionary <ulong, HandStrength>();

            Console.WriteLine("Calculating hand strength (" + handBitmaps.Count + ")");

            using (var progress = new ProgressBar())
            {
                long sharedLoopCounter = 0;

                foreach (ulong bitmap in handBitmaps)
                {
                    var hand = new Hand(bitmap);
                    handStrengths.Add(bitmap, hand.GetStrength());
                    sharedLoopCounter++;
                    progress.Report((double)sharedLoopCounter / handBitmaps.Count, sharedLoopCounter);
                }
            }

            // Generate a list of all unique hand strengths
            var uniqueHandStrengths = new List <HandStrength>();

            Console.WriteLine("Generating equivalence classes");

            using (var progress = new ProgressBar())
            {
                long sharedLoopCounter = 0;

                foreach (KeyValuePair <ulong, HandStrength> strength in handStrengths)
                {
                    Utilities.BinaryInsert(uniqueHandStrengths, strength.Value);
                    sharedLoopCounter++;
                    progress.Report((double)sharedLoopCounter / handStrengths.Count, sharedLoopCounter);
                }
            }
            Console.WriteLine("{0} unique hand strengths", uniqueHandStrengths.Count);

            // Create a map of hand bitmaps to hand strength indices
            Console.WriteLine("Generating new five card lookup table (2'598'960)");
            using (var progress = new ProgressBar())
            {
                long sharedLoopCounter = 0;

                foreach (ulong bitmap in handBitmaps)
                {
                    var          hand        = new Hand(bitmap);
                    HandStrength strength    = hand.GetStrength();
                    var          equivalence = Utilities.BinarySearch(uniqueHandStrengths, strength);
                    if (equivalence == null)
                    {
                        throw new Exception(string.Format("{0} hand not found", hand));
                    }
                    else
                    {
                        handRankMap[bitmap] = (ulong)equivalence;
                    }
                    sharedLoopCounter++;
                    progress.Report((double)sharedLoopCounter / handBitmaps.Count, sharedLoopCounter);
                }
            }
        }