/// <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> /// gets words that are able to be made with the tiles passed. /// </summary> /// <param name="words">the dictionary</param> /// <param name="tiles"></param> /// <returns></returns> private string[] checkWords(string[] words, char[] tiles) { int temp = 0; int[] letters = new int[26]; int blanks = 0; char[] wordChecking; string highScore = ""; int highestScore = 0; List <string> s = new List <string>(); for (int i = 0; i < letters.Length; i++) { letters[i] = 0; } for (int i = 0; i < tiles.Length; i++) { if (tiles[i].Equals(' ')) { blanks++; } else { letters[tiles[i] - 65]++; } } foreach (string word in words) { wordChecking = word.ToCharArray(); int[] wordCheckingChars = new int[26]; for (int i = 0; i < wordCheckingChars.Length; i++) { wordCheckingChars[i] = 0; } for (int i = 0; i < wordChecking.Length; i++) { wordCheckingChars[wordChecking[i] - 65]++; } int extraLetters = 0; for (int i = 0; i < wordCheckingChars.Length; i++) { if (wordCheckingChars[i] - letters[i] > 0) { extraLetters += wordCheckingChars[i] - letters[i]; } if (wordCheckingChars[i] > 0) { temp += new ScrabbleLetter(Convert.ToChar(i + 65)).Points; } } if (extraLetters <= blanks) { s.Add(word); if (temp > highestScore) { highestScore = temp; highScore = word; } } temp = 0; } MessageBox.Show("best word: " + highScore); return(s.ToArray()); }
/// <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); }