Esempio n. 1
0
        private static ShuffledPackage ShuffleCards_FisherYates(CardPack cardPack, int shuffleCount, bool recordResults)
        //================================================================================================================
        // Shuffle the given pack of cards using the Fisher-Yates algorithm
        //
        // Parameters
        //      cardPack:      Reference to a pack of cards
        //      shuffleCount:  Number of times to shuffle
        //      recordResults: Flag, record the results of each shuffle
        //
        // Returns
        //      A shuffled package
        //================================================================================================================
        {
            // Create a shuffled package and a random number generator
            ShuffledPackage shuffledPackage = new ShuffledPackage();

            // Shuffle the card pack the specified number of times
            int randomIndex;
            int cardsInPack;

            for (int i = 1; i <= shuffleCount; i++)
            {
                // get the number of cards in the deck
                // Create a random number generator
                cardsInPack = cardPack.CardDeck.Count;
                RNGCryptoServiceProvider randomProvider = new RNGCryptoServiceProvider();

                // Shuffle the deck
                int packBottom = cardsInPack;
                while (packBottom > 1)
                {
                    // Get a random number between 0 and the bottom of the pack
                    randomIndex = GetRandomNumber(randomProvider, packBottom);

                    // Decrement the bottom of the pack (zero based collections)
                    packBottom--;

                    //Swap the playing card at the random index with the playing card at the bottom of the pack
                    PlayingCard selectedCard = cardPack.CardDeck[randomIndex];
                    cardPack.CardDeck[randomIndex] = cardPack.CardDeck[packBottom];
                    cardPack.CardDeck[packBottom]  = selectedCard;
                }

                // Record the shuffle result
                if (recordResults)
                {
                    RecordShuffleResult(cardPack, SiteHelpers.ShuffleTypes.FisherYates, i, shuffledPackage.ShuffleResults);
                }
            }

            // After all the shuffling is complete assign the final card pack
            shuffledPackage.CardPack = cardPack;

            return(shuffledPackage);
        }
Esempio n. 2
0
        private static ShuffledPackage ShuffleCards_Naive(CardPack cardPack, int shuffleCount, bool recordResults)
        //================================================================================================================
        // Shuffle the given pack of cards using the naive algorithm
        //
        // Parameters
        //      cardPack:      Reference to a pack of cards
        //      shuffleCount:  Number of times to shuffle
        //      recordResults: Flag, record the results of each shuffle
        //
        // Returns
        //      A shuffled package
        //================================================================================================================
        {
            // Create a shuffled package
            ShuffledPackage shuffledPackage = new ShuffledPackage();

            // Shuffle the card pack the specified number of times
            int randomIndex;
            int cardsInPack;

            for (int i = 1; i <= shuffleCount; i++)
            {
                // get the number of cards in the deck
                // Create a random number generator
                cardsInPack = cardPack.CardDeck.Count;
                Random randomNumberGen = new Random();

                // Shuffle the deck
                for (int cardIndex = 0; cardIndex <= (cardsInPack - 1); cardIndex++)
                {
                    // Get a random number between 0 and the number of cards in the deck
                    randomIndex = randomNumberGen.Next(0, (cardsInPack - 1));

                    // Swap the card at the current index with the card at the random index
                    PlayingCard selectedCard = cardPack.CardDeck[randomIndex];
                    cardPack.CardDeck[randomIndex] = cardPack.CardDeck[cardIndex];
                    cardPack.CardDeck[cardIndex]   = selectedCard;
                }

                // Record the shuffle result
                if (recordResults)
                {
                    RecordShuffleResult(cardPack, SiteHelpers.ShuffleTypes.Naive, i, shuffledPackage.ShuffleResults);
                }
            }

            // After all the shuffling is complete assign the final card pack
            shuffledPackage.CardPack = cardPack;

            return(shuffledPackage);
        }
Esempio n. 3
0
        public ActionResult CardShuffler_PerformAction()
        //================================================================================================================
        // This action is invoked when the card display area is performing an action on the cards.
        //
        // Event Arguments
        //      arg_Action:       Action to be performed
        //      arg_CardPackId:   Unique id of a card pack
        //      arg_ShuffleCount: Number of time to shuffle
        //      arg_ShuffleType:  Shuffle algorithm
        //
        // Returns
        //      The card display view
        //================================================================================================================
        {
            CardPack cardPack = null;

            // Retrieve the action to be performed and the assigned card pack
            string action     = !string.IsNullOrEmpty(Request.Params[ARG_ACTION]) ? Request.Params[ARG_ACTION] : "";
            int    cardPackId = !string.IsNullOrEmpty(Request.Params[ARG_CARD_PACK_ID]) ? int.Parse(Request.Params[ARG_CARD_PACK_ID]) : 0;

            // Connect to the database and perform the action
            SSCasino_DBContext dbCasino = null;

            try
            {
                // Connect the the database
                dbCasino = new SSCasino_DBContext();

                // Perform the action
                switch (action)
                {
                case ACT_SHUFFLE:
                    // Retrieve an unshuffled pack of cards
                    cardPack = CreateCardPackModel(dbCasino, cardPackId);

                    // Shuffle the cards
                    int             shuffleType     = !string.IsNullOrEmpty(Request.Params[ARG_SHUFFLE_TYPE]) ? int.Parse(Request.Params[ARG_SHUFFLE_TYPE]) : 1;
                    int             shuffleCount    = !string.IsNullOrEmpty(Request.Params[ARG_SHUFFLE_COUNT]) ? int.Parse(Request.Params[ARG_SHUFFLE_COUNT]) : 1;
                    ShuffledPackage shuffledPackage = SiteHelpers.ShuffleCards(cardPack, (SiteHelpers.ShuffleTypes)shuffleType, shuffleCount, false);
                    cardPack = shuffledPackage.CardPack;
                    break;

                case ACT_CHANGE_CARD_PACK:
                    // Retrieve an unshuffled pack of cards
                    cardPack = CreateCardPackModel(dbCasino, cardPackId);
                    break;

                default:
                    // Get the default card pack
                    cardPack = CreateCardPackModel(dbCasino);
                    break;
                }
            }
            finally
            {
                if (dbCasino != null)
                {
                    dbCasino.Dispose();
                }
            }

            return(PartialView("_CardShuffler_DisplayCBP", cardPack.CardDeck));
        }