コード例 #1
0
        public void TestCardPoolClone()
        {
            CardPool origCardPool = new CardPool(4);

            DealCardsAndCheckProbability(origCardPool, 28);

            CardPool cloneCardPool = (CardPool)origCardPool.Clone();
            Random   rand          = new Random();

            for (int i = 0; i < 10; i++)
            {
                int[] availableValues = origCardPool.GetAvailableCardValues().ToArray();
                int   valIndex        = rand.Next(0, availableValues.Length - 1);

                int dealCard = origCardPool.DealCard(availableValues[valIndex]);
                Assert.AreEqual(dealCard, cloneCardPool.DealCard(dealCard));
                Assert.AreEqual(origCardPool.GetValueRangeProbability(dealCard, dealCard), cloneCardPool.GetValueRangeProbability(dealCard, dealCard));
            }
        }
コード例 #2
0
        private static int DealCardsAndCheckProbability(CardPool cardPool, int cardsToDeal, int[] dealingOrder = null, int dealingPosition = 0)
        {
            int[]  cardsByValueLocal = new int[13];
            int    cardCountLocal    = cardPool.CardCount;
            Random rand = new Random();

            Assert.IsTrue(cardCountLocal > 0, "Empty card pool is used.");

            // Save card pool state to local
            for (int i = 0; i < cardsByValueLocal.Length; i++)
            {
                cardsByValueLocal[i] = (int)Math.Round(cardCountLocal * cardPool.GetValueRangeProbability(i, i), 0);
            }

            // Deal card and update local state
            for (int i = 0; i < cardsToDeal; i++)
            {
                int cardValue;
                if (dealingOrder != null)
                {
                    if (dealingPosition >= dealingOrder.Length)
                    {
                        cardValue = -1;
                    }
                    else
                    {
                        cardValue = cardPool.DealCard(dealingOrder[dealingPosition++]);
                    }
                }
                else
                {
                    int[] availableValues = cardPool.GetAvailableCardValues().ToArray();
                    if (availableValues.Length == 0)
                    {
                        cardValue = -1;
                    }
                    else
                    {
                        int valIndex = rand.Next(0, availableValues.Length - 1);
                        cardValue = cardPool.DealCard(availableValues[valIndex]);
                    }
                }

                Console.WriteLine("Deal Card {0}.", cardValue);

                if (cardValue < 0)
                {
                    break;
                }

                cardCountLocal--;
                cardsByValueLocal[cardValue]--;
            }

            // Check local against the card pool
            Assert.AreEqual(cardPool.CardCount, cardCountLocal, "Local card count is incorrect.");
            if (cardCountLocal > 0)
            {
                for (int i = 0; i < cardsByValueLocal.Length; i++)
                {
                    decimal probabilityLocal = (decimal)cardsByValueLocal[i] / cardCountLocal;
                    Assert.AreEqual(cardPool.GetValueRangeProbability(i, i), probabilityLocal, string.Format("Local probability is incorrect for card value {0}", i));
                }
            }

            return(dealingPosition);
        }