コード例 #1
0
        private int TopCardIndex; //represents the top card on the deck, ie the first non-discarded card


        //METHODS
        //called when deck is initialized
        public void ShuffleDeck()
        {
            String allDescriptions = Resources.CardDescriptions;
            int    cardIDMod       = 0; //used to assign correct cardIDs
            UI     ui = new UI();

            if (this.DeckType == DeckType.CommunityChest)
            {
                Cards           = new Card[17];                                                     //wipe deck
                allDescriptions = allDescriptions.Substring(16, allDescriptions.IndexOf("Chance")); //+14 for 'communitychest', +2 for cr and lf
                ui.UIDebug("Community Chest Deck initializing...");
            }
            else
            {
                Cards           = new Card[16];
                cardIDMod       = 17;
                allDescriptions = allDescriptions.Substring(allDescriptions.IndexOf("Chance") + 8); //+6 for 'chance', + 2 for cr and lf
                ui.UIDebug("Chance Deck initializing...");
            }

            String[] cardDescriptions = new String[Cards.Length];
            Card[]   orderedCards     = new Card[Cards.Length];

            StringReader reader = new StringReader(allDescriptions);

            for (int i = 0; i <= (Cards.Length - 1); i++)
            {
                String line = reader.ReadLine();
                orderedCards[i] = new Card(i + cardIDMod, line);
            }//foreach
            reader.Close();
            ui.UIDebug("Deck is now in proper order. Shuffling...");

            //Begin Fisher-Yates shuffling algorithm
            Random rand = new Random();
            int    n    = orderedCards.Length;

            for (int i = 0; i <= n - 1; i++)
            {
                int  j        = rand.Next(i, n);
                Card tempCard = orderedCards[i];
                orderedCards[i] = orderedCards[j];
                orderedCards[j] = tempCard;
            }//for

            this.Cards        = orderedCards;
            this.TopCardIndex = 0;
            ui.UIDebug("Deck has been shuffled.");
        }//ShuffleDeck
コード例 #2
0
ファイル: Deck.cs プロジェクト: jonathanargo/Monopoly
        //METHODS
        //called when deck is initialized
        public void ShuffleDeck()
        {
            String allDescriptions = Resources.CardDescriptions;
            int cardIDMod = 0; //used to assign correct cardIDs
            UI ui = new UI();
            if (this.DeckType == DeckType.CommunityChest)
            {
                Cards = new Card[17]; //wipe deck
                allDescriptions = allDescriptions.Substring(16, allDescriptions.IndexOf("Chance")); //+14 for 'communitychest', +2 for cr and lf
                ui.UIDebug("Community Chest Deck initializing...");
            } else {
                Cards = new Card[16];
                cardIDMod = 17;
                allDescriptions = allDescriptions.Substring(allDescriptions.IndexOf("Chance") + 8); //+6 for 'chance', + 2 for cr and lf
                ui.UIDebug("Chance Deck initializing...");
            }

            String[] cardDescriptions = new String[Cards.Length];
            Card[] orderedCards = new Card[Cards.Length];

            StringReader reader = new StringReader(allDescriptions);

            for(int i = 0; i <= (Cards.Length - 1); i++)
            {
                String line = reader.ReadLine();
                orderedCards[i] = new Card(i + cardIDMod, line);
            }//foreach
            reader.Close();
            ui.UIDebug("Deck is now in proper order. Shuffling...");

            //Begin Fisher-Yates shuffling algorithm
            Random rand = new Random();
            int n = orderedCards.Length;
            for(int i = 0; i <= n - 1; i++)
            {
                int j = rand.Next(i, n);
                Card tempCard = orderedCards[i];
                orderedCards[i] = orderedCards[j];
                orderedCards[j] = tempCard;
            }//for

            this.Cards = orderedCards;
            this.TopCardIndex = 0;
            ui.UIDebug("Deck has been shuffled.");
        }
コード例 #3
0
        }//BoardfileReader

        public Board CreateBoard()
        {
            String line = "";

            String[]     strValues;
            int[]        intValues;
            StreamReader stmReader;
            Char         delimitChar = ',';
            Board        resultBoard;

            ui.UIDebug("Attempting to create board...");

            try{
                stmReader   = new StreamReader(mBoardPath);
                resultBoard = new Board();
                ui.UIDebug("Board stream initalized!");
                for (int i = 1; i <= resultBoard.BoardSpaces.Length - 1; i++)
                {
                    line      = stmReader.ReadLine();
                    strValues = line.Split(delimitChar);
                    intValues = new int[strValues.Length];

                    //create int array for int values such as rent, mortagage, etc
                    for (int j = 0; j <= strValues.Length - 1; j++)
                    {
                        try
                        {
                            intValues[j] = int.Parse(strValues[j]);
                        }//try
                        catch (FormatException ex)
                        {
                            intValues[j] = 0;
                            ex.GetType();
                        } //catch
                    }     //for

                    //ugly switch, need to revise in the future
                    switch (strValues[0])
                    {
                    case "Property":
                        Rent           tempRent = new Rent(intValues[5], intValues[6], intValues[7], intValues[8], intValues[9], intValues[10]);
                        Tiles.Property thisProp = new Tiles.Property(intValues[2], strValues[1], (PropertyColor)intValues[4], intValues[3], tempRent, intValues[11]);
                        resultBoard.BoardSpaces[i] = thisProp;
                        //add the property to the proper color group, see Enums for more details
                        resultBoard.ColorGroups[(int)thisProp.Color].AddProperty(thisProp);
                        break;

                    case "Railroad":
                        resultBoard.BoardSpaces[i] = new Tiles.Railroad(intValues[2], strValues[1]);
                        break;

                    case "Utility":
                        resultBoard.BoardSpaces[i] = new Tiles.Utility(intValues[2], strValues[1]);
                        break;

                    case "LuxuryTax":
                        resultBoard.BoardSpaces[i] = new Tiles.LuxuryTax(intValues[2], strValues[1]);
                        break;

                    case "IncomeTax":
                        resultBoard.BoardSpaces[i] = new Tiles.IncomeTax(intValues[2], strValues[1]);
                        break;

                    case "CommunityChest":
                        resultBoard.BoardSpaces[i] = new Tiles.CommunityChest(intValues[2], strValues[1]);
                        break;

                    case "Chance":
                        resultBoard.BoardSpaces[i] = new Tiles.Chance(intValues[2], strValues[1]);
                        break;

                    case "Go":
                        resultBoard.BoardSpaces[i] = new Tiles.Go(intValues[2], strValues[1]);
                        break;

                    case "GoToJail":
                        resultBoard.BoardSpaces[i] = new Tiles.GoToJail(intValues[2], strValues[1]);
                        break;

                    case "Jail":
                        resultBoard.BoardSpaces[i] = new Tiles.Jail(intValues[2], strValues[1]);
                        break;

                    case "FreeParking":
                        resultBoard.BoardSpaces[i] = new Tiles.FreeParking(intValues[2], strValues[1]);
                        break;

                    default:
                        ui.Error("The BoardReader was unable to determine space type!");
                        break;
                    } //switch
                }     //for
            }         //try (open streamreader)

            catch (FileNotFoundException ex) {
                ui.Error("Stream couldn't be found.", ex);
                return(null);
            }//catch FileNotFound (streamreader)

            return(resultBoard);
        } //CreateBoard