Exemplo n.º 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(' ');
        }
Exemplo n.º 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);
        }
Exemplo n.º 3
0
        public MainWindow()
        {
            InitializeComponent();
            ScrabbleGame sg = new ScrabbleGame();

            //Draws upper case letters
            letters = sg.drawInitialTiles().ToUpper();
            //Creates Alphabet without drawn tile letters
            for (int i = 65; i < 65 + 26; i++)
            {
                char c = Convert.ToChar(i);
                Console.WriteLine("Letter: " + c);
                if (!letters.Contains(c))
                {
                    totalletters += c;
                }
            }
            //Outputs tiles you have and the alphabet without them
            if (debug_test == true)
            {
                MessageBox.Show(totalletters, letters);
            }
            MessageBox.Show("Your Tiles are : " + letters);
            try
            {
                while (!reader.EndOfStream)
                {
                    check  = true;
                    check2 = true;
                    check3 = false;
                    string line = reader.ReadLine().ToUpper();
                    //string lineUpper = line.ToUpper();
                    //Checks for situations involving a blank tile.
                    if (letters.Contains(' '))
                    {
                        if (line == "DAFT" && debug_test == true)
                        {
                            MessageBox.Show(line);
                        }
                        int blank_count = 0;
                        int length      = letters.Length;
                        int linelength  = line.Length;
                        foreach (char c in letters)
                        {
                            if (c == ' ')
                            {
                                blank_count++;
                            }
                        }
                        string newline = line;

                        for (int i = 0; i < 7; i++)
                        {
                            //Removes letters until your tiles run out
                            if (newline.Contains(letters[i]))
                            {
                                if (line == "DAFT" && debug_test == true)
                                {
                                    MessageBox.Show(newline + " Contains " + letters[i]);
                                }
                                int index = newline.IndexOf(letters[i]);
                                newline = newline.Remove(index, 1);
                                if (line == "DAFT" && debug_test == true)
                                {
                                    MessageBox.Show(newline);
                                }
                            }
                        }
                        //Checks if cut down word is the same length as the number of blank tiles
                        if (newline.Length <= blank_count)
                        {
                            check3 = true;
                        }
                        else
                        {
                            check3 = false;
                        }
                    }
                    else
                    {
                        check3 = false;
                    }
                    //Checks if letters left are in word
                    for (int i = 0; i < totalletters.Length; i++)
                    {
                        if (line.Contains(totalletters[i]))
                        {
                            check = false;
                        }
                    }
                    //Following code check if the number of times a given letter appears is the same in the word and your 'hand'
                    for (int i = 0; i < 7; i++)
                    {
                        int count1 = 0;
                        int count2 = 0;
                        foreach (char c in line)
                        {
                            if (c == letters[i])
                            {
                                count1++;
                            }
                        }
                        //Console.WriteLine("There is " + count1 + " " + letters[i] + " in " + line);
                        //The commented console lines were for testing
                        foreach (char c in letters)
                        {
                            if (c == letters[i])
                            {
                                count2++;
                            }
                        }
                        //Console.WriteLine("There is " + count2 + " " + letters[i] + " in " + letters);
                        if (count1 <= count2)
                        {
                            //Console.WriteLine("The Same" + '\n');
                        }
                        else
                        {
                            check2 = false;
                            //Console.WriteLine("Different" + '\n');
                        }
                    }

                    //Writes to file if all peramiters are correct
                    if ((check == true && check2 == true) || check3 == true)
                    {
                        //Gets largest score possible and word that corresponds to it
                        int total = 0;
                        foreach (char f in line)
                        {
                            ScrabbleLetter letter = new ScrabbleLetter(f);
                            int            points = letter.Points;
                            total += points;
                        }
                        if (total > maxtotal)
                        {
                            ultimateWord = line;
                            maxtotal     = total;
                        }
                        //Writes correct words to the file
                        writer.Write(line + '\r' + '\n');
                    }
                }
                writer.Flush();
                writer.Close();
                reader.Close();
                MessageBox.Show("Max Points Was: " + maxtotal + '\n' + "With the Word " + ultimateWord);
                MessageBox.Show("Job Done, Press Enter to see Word List");
                Application.Current.Shutdown();
                System.Diagnostics.Process.Start("CorrectWords.txt");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }