Exemple #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(' ');
        }
        /// <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);
        }
Exemple #3
0
        public MainWindow()
        {
            InitializeComponent();

            // creates an instance of the scrabble game class and draws initial tiles
            ScrabbleGame sg          = new ScrabbleGame();
            string       yourLetters = sg.drawInitialTiles();

            MessageBox.Show("The letters you drew are: " + yourLetters);

            // loop that will deleter the letters you drew from the letters you have, leaving letters you can't use
            for (int i = 0; i < allLetters.Length; i++)
            {
                if (yourLetters.Contains(allLetters[i]))
                {
                    allLetters = allLetters.Remove(i, 1);
                    i--;
                }
            }

            // initializing the streamreader and writer
            streamReader = new StreamReader("Words.txt");
            streamWriter = new StreamWriter("RightWords.txt");

            // loop that will run through the list of words
            while (!streamReader.EndOfStream)
            {
                // resetting the values of some variables
                blankCounter  = 0;
                letterCounter = 0;
                wrongCounter  = 0;

                // setting the line
                line = streamReader.ReadLine().ToUpper();

                // one set of conditions that will run if you don't have a blank tile
                if (!yourLetters.Contains(" "))
                {
                    // if a word has one of the letters you don't have it isn't written
                    for (int j = 0; j < allLetters.Length; j++)
                    {
                        hasLetter = false;
                        if (line.Contains(allLetters[j]))
                        {
                            hasLetter = true;
                            break;
                        }
                    }

                    // this loop will run if the word only has letters you have
                    if (hasLetter != true)
                    {
                        // checking to make sure that the amount of times a letter is in your letters, and in the word are the same
                        for (int i = 0; i < 7; i++)
                        {
                            // resets some variable values
                            counterInLetters = 0;
                            counterInWord    = 0;

                            // runs through the letters in the word
                            foreach (char character in line)
                            {
                                if (character == yourLetters[i])
                                {
                                    counterInWord++;
                                }
                            }

                            // runs through the letters you have in your tiles
                            foreach (char character in yourLetters)
                            {
                                if (character == yourLetters[i])
                                {
                                    counterInLetters++;
                                }
                            }

                            // checks if a letter is in the word more than your tiles
                            if (counterInWord <= counterInLetters)
                            {
                                letterAmount = true;
                            }

                            else
                            {
                                letterAmount = false;
                                break;
                            }
                        }
                    }

                    // checks to see if all the criteria are met for the word to be used
                    if (hasLetter != true &&
                        letterAmount == true)
                    {
                        tempInt = 0;
                        // calculates the score of the word, if it's higher than the previous highest, it is set as the highest
                        foreach (char character in line)
                        {
                            ScrabbleLetter scrabbleLetter = new ScrabbleLetter(character);
                            tempInt += scrabbleLetter.Points;
                            if (tempInt > maxPoints)
                            {
                                maxPoints    = tempInt;
                                maxPointWord = line;
                            }
                        }

                        // writes the word that works into a file
                        streamWriter.Write(line + "\r" + "\n");
                    }
                }

                // this code runs if you have a blank tile
                else
                {
                    // runs through your letters to see how many blank tiles you have
                    for (int l = 0; l < yourLetters.Length; l++)
                    {
                        if (yourLetters[l] == ' ')
                        {
                            blankCounter++;
                        }
                    }

                    // checks to see how many letters the word has that you don't have, if you have the same, or more blank tiles the word is still good
                    for (int j = 0; j < allLetters.Length; j++)
                    {
                        hasLetter = false;

                        if (line.Contains(allLetters[j]))
                        {
                            letterCounter++;
                        }

                        if (letterCounter > blankCounter)
                        {
                            hasLetter = true;
                            break;
                        }
                    }

                    // runs if the word works based off the code above
                    if (hasLetter != true)
                    {
                        // checking to make sure that the amount of times a letter is in your letters, and in the word are the same, and if not, that you have enough blank tiles
                        for (int i = 0; i < 7; i++)
                        {
                            wrongCounter     = 0;
                            counterInLetters = 0;
                            counterInWord    = 0;

                            // runs through and checks all the letters in your word
                            foreach (char character in line)
                            {
                                if (character == yourLetters[i])
                                {
                                    counterInWord++;
                                }

                                // adds one to the counter that sees how many letters don't work
                                if (!yourLetters.Contains(character))
                                {
                                    wrongCounter++;
                                }
                            }

                            // runs through all the letters in your tiles
                            foreach (char character in yourLetters)
                            {
                                if (character == yourLetters[i])
                                {
                                    counterInLetters++;
                                }
                            }

                            // checks to see if all the criteria are met
                            if (counterInWord <= counterInLetters &&
                                wrongCounter <= blankCounter)
                            {
                                letterAmount = true;
                            }

                            else
                            {
                                letterAmount = false;
                                break;
                            }
                        }

                        // checks to see if the word is good, and calculates how many points it would have, if it's higher than the previous highest it becomes the highest
                        if (hasLetter != true &&
                            letterAmount == true)
                        {
                            tempInt = 0;
                            foreach (char character in line)
                            {
                                ScrabbleLetter scrabbleLetter = new ScrabbleLetter(character);
                                tempInt += scrabbleLetter.Points;
                                if (tempInt > maxPoints)
                                {
                                    maxPoints    = tempInt;
                                    maxPointWord = line;
                                }
                            }
                            streamWriter.Write(line + "\r" + "\n");
                        }
                    }
                }
            }

            // closes the reader and writer
            streamReader.Close();
            streamWriter.Flush();
            streamWriter.Close();

            // shows which word has the most points
            MessageBox.Show("The word that will make the most points is: " + maxPointWord + "\r" + "\n" + "With " + maxPoints.ToString() + " points");
            MessageBox.Show("All the Words you can make are:");

            // opens the text file with all the right words in it
            System.Diagnostics.Process.Start("RightWords.txt");

            // closes the main window
            Application.Current.Shutdown();
        }