示例#1
0
        /// <summary>
        /// Set the tiles based on the Scrabble Game
        /// </summary>
        public ScrabbleGame()
        {
            int startingLetter = (char)'A';
            int total          = 0;

            //Loop through the letters and determine how many tiles per letter
            for (int i = startingLetter; i < (startingLetter + 26); i++)
            {
                total += ScrabbleLetter.HowManyTiles((char)i);
            }
            //Set variable values and instantiate the array
            m_tilesLeft       = total + 2;
            m_totalTiles      = total + 2;
            m_scrabbleLetters = new ScrabbleLetter[total + 2]; //2 blank tiles!
            int counter = 0;                                   //use to control the index of the array

            //Loop through the letters to create the tiles
            for (int i = startingLetter; i < (startingLetter + 26); i++)
            {
                //Loop through the number of tiles for that letter
                for (int j = 0; j < ScrabbleLetter.HowManyTiles((char)i); j++)
                {
                    m_scrabbleLetters[counter] = new ScrabbleLetter((char)i);
                    counter++;//always increase the counter!
                }
            }
            //add the two blanks
            m_scrabbleLetters[counter] = new ScrabbleLetter(' ');
            counter++;
            m_scrabbleLetters[counter] = new ScrabbleLetter(' ');
        }
示例#2
0
        /// <summary>
        /// Returns how many tiles in scrabble have the letter.
        /// </summary>
        /// <param name="L">The letter.</param>
        /// <returns></returns>
        public static int HowManyTiles(char L)
        {
            ScrabbleLetter s = new ScrabbleLetter(L);

            return(s.NumberOfLetters);
        }
        public MainWindow()
        {
            InitializeComponent();
            ///takes the dictionary and puts into a stream reader to be used
            System.Net.WebClient wc = new System.Net.WebClient();
            Stream       download   = wc.OpenRead("http://darcy.rsgc.on.ca/ACES/ICS4U/SourceCode/Words.txt");
            StreamReader sr         = new StreamReader(download);
            ///picks random letters, and finds and removes blanks
            ScrabbleGame sg      = new ScrabbleGame();
            string       letters = sg.drawInitialTiles().ToUpper();//E C K S O I H = testing letters

            while (letters.IndexOf(" ") != -1)
            {
                letters = letters.Remove(letters.IndexOf(" "), 1);
                BlankTileCounter++;
            }
            ///finds the tiles in the alphabet and removes them, leaving all other letters
            for (int i = 0; i < letters.Length; i++)
            {
                int letterLocation = badLetters.IndexOf(letters.Substring(i, 1));
                if (letterLocation != -1)
                {
                    badLetters = badLetters.Remove(letterLocation, 1);
                }
            }

            string lastWord = "";

            lblOutput.Content += "\t\t" + Environment.NewLine; ///formating

            while (!sr.EndOfStream)
            {
                int counter = 0;
                currentWord = sr.ReadLine().ToUpper();
                string CheckWord = currentWord;                        ///
                if (currentWord.Length < 8 && currentWord != lastWord) ///only have 7 tiles, can't be longer
                {
                    for (int i = 0; i < badLetters.Length; i++)
                    {
                        if (currentWord.Contains(badLetters.Substring(i, 1)))
                        {///checks for any letters that aren't tiles
                            counter++;
                            if (counter > BlankTileCounter)
                            {///if there are more wrong letters than blank tiles, exit for loop
                                i = 26;
                            }
                        }
                    }//end for loop
                    if (counter <= BlankTileCounter)
                    {///checks if the word has only letters available
                        int currentPoints = 0;
                        for (int i = 0; i < letters.Length; i++)
                        {
                            string currentLetter = letters.Substring(i, 1);          ///finds specific letter in current word
                            int    RemoveLetter  = CheckWord.IndexOf(currentLetter); ///finds index of that letter
                            if (RemoveLetter != -1)
                            {                                                        ///removes thre letter form the temp word, and gets the points for the letter
                                CheckWord = CheckWord.Remove(RemoveLetter, 1);
                                ScrabbleLetter sl = new ScrabbleLetter(currentLetter.ToCharArray()[0]);
                                currentPoints += sl.Points;
                            }
                        }
                        if (CheckWord.Length <= BlankTileCounter)
                        {/// if the word can be made with the tiles, writes it to output
                            lastWord           = currentWord;
                            lblOutput.Content += Environment.NewLine + currentWord.Substring(0, 1)
                                                 + currentWord.Substring(1);

                            if (currentPoints > BestScore)
                            {///checks if the current word has a better score than previous best
                                BestWord  = currentWord;
                                BestScore = currentPoints;
                            }
                        } //end check for usable word
                    }     //end if for potential word
                }         //end length check if
            }             //end while loop
            sr.Close();
            //writes best wrod and c=score to output
            lblBest.Content = "Your letters were: " + letters
                              + Environment.NewLine + "Your best possible word is: "
                              + BestWord.Substring(0, 1) + BestWord.Substring(1).ToLower()
                              + ", which gets you " + BestScore + " points";
        }