public Game() { //at the start of the application read in all the words into dictionary SearchWord.MakeDictionary(); //starts a new game NewGame(); }
//Function takes two one list of Pairs. //Searches left and up for all buildable words from each added tile //Then checks each built word to see if it is valid // .Value is y coordinate .Key is x coordinate //returns total points scored by all the letters otherwise it returns 0 which means not all letters were used public static int CheckWords(List <KeyValuePair <int, int> > CoordinatePairs) { //these are two of the strings returned at the end of the function string VerticalWord = ""; string HorizontalWord = ""; //keeps track if there is a connection to an island int Connect = 0; //this is the total score of all valid words returned at the end int TotalScore = 0; //keeps track to see if all the letters placed on the board are used int flag = 0; //stores all used words to prevent duplicate words List <string> DuplicateWords = new List <string>(); //stores the X, Y coordinates of used words List <KeyValuePair <int, int> > FoundWords = new List <KeyValuePair <int, int> >(); bool[,] UsedLetters = new bool[15, 15]; for (int a = 0; a < 15; a++) { for (int b = 0; b < 15; b++) { UsedLetters[a, b] = false; } } for (int i = 0; i < CoordinatePairs.Count; i++) //Checks each column for full word { //stores x, y coordinates of current word being built List <KeyValuePair <int, int> > Storage = new List <KeyValuePair <int, int> >(); int j = CoordinatePairs[i].Key - 1;//move down the starting row string FrontWord = ""; string tile; //Get char at tile position, moving up until empty tile found if (j >= 0 && ScrabbleBoard[j, CoordinatePairs[i].Value] != " ") { Storage.Add(new KeyValuePair <int, int>(j, CoordinatePairs[i].Value)); } while (j >= 0 && ScrabbleBoard[j, CoordinatePairs[i].Value] != " ") { tile = ScrabbleBoard[j, CoordinatePairs[i].Value]; FrontWord = tile + FrontWord; j--; if (j >= 0 && ScrabbleBoard[j, CoordinatePairs[i].Value] != " ") { //Get char at tile position, moving up until empty tile found Storage.Add(new KeyValuePair <int, int>(j, CoordinatePairs[i].Value)); } } FrontWord.Trim(); //main letter that is guarnteed to be placed string MainLetter = ScrabbleBoard[CoordinatePairs[i].Key, CoordinatePairs[i].Value]; Storage.Add(new KeyValuePair <int, int>(CoordinatePairs[i].Key, CoordinatePairs[i].Value)); j = CoordinatePairs[i].Key + 1; //Move up a row string BackWord = ""; Storage.Add(new KeyValuePair <int, int>(j, CoordinatePairs[i].Value)); while (j <= 14 && ScrabbleBoard[j, CoordinatePairs[i].Value] != " ") { tile = ScrabbleBoard[j, CoordinatePairs[i].Value]; BackWord += tile; j++; if (j <= 14 && ScrabbleBoard[j, CoordinatePairs[i].Value] != " ") { //Get char at tile position, moving up until empty tile found Storage.Add(new KeyValuePair <int, int>(j, CoordinatePairs[i].Value)); } } BackWord.Trim(); int FrontLength = FrontWord.Length; int BackLength = BackWord.Length; for (int a = 0; a <= FrontLength; a++) { for (int b = 0; b <= BackLength; b++) { if (SearchWord.ValidWord(FrontWord.Substring(a, FrontLength - a) + MainLetter + BackWord.Substring(0, BackLength - b)) && !DuplicateWords.Contains(FrontWord.Substring(a, FrontLength - a) + MainLetter + BackWord.Substring(0, BackLength - b))) { List <KeyValuePair <int, int> > Temp = Storage.GetRange(a, FrontLength - a + 1 + BackLength - b); DuplicateWords.Add(FrontWord.Substring(a, FrontLength - a) + MainLetter + BackWord.Substring(0, BackLength - b)); //storing all x, y coordinates of valid word to be used to check if all letters used foreach (KeyValuePair <int, int> item in Temp) { FoundWords.Add(new KeyValuePair <int, int>(item.Key, item.Value)); } //check to make sure board is not empty and checks if there is any islands if (MainWindow.ValidPairs.Count == 0 || IsConnect(Temp)) { Connect = 1; } VerticalWord = FrontWord.Substring(a, FrontLength - a) + MainLetter + BackWord.Substring(0, BackLength - b); int points = CalculateScore(VerticalWord); TotalScore += points; MainWindow.OutPutTextBox.AppendText("The word " + VerticalWord + " is worth " + points + " points" + "\n"); MainWindow.OutPutTextBox.ScrollToCaret(); //marking all letters in the hash table as true foreach (KeyValuePair <int, int> item in FoundWords) { UsedLetters[item.Key, item.Value] = true; } } } } } //horizontal words do not collide with vertical words for duplicates DuplicateWords.Clear(); for (int i = 0; i < CoordinatePairs.Count; i++) //identical to above loop, but instead checks words to the LEFT { List <KeyValuePair <int, int> > Storage = new List <KeyValuePair <int, int> >(); int j = CoordinatePairs[i].Value - 1; //move to the left one. string FrontWord = ""; string tile; if (j >= 0 && ScrabbleBoard[CoordinatePairs[i].Key, j] != " ") { Storage.Add(new KeyValuePair <int, int>(CoordinatePairs[i].Key, j)); } while (j >= 0 && ScrabbleBoard[CoordinatePairs[i].Key, j] != " ") { tile = ScrabbleBoard[CoordinatePairs[i].Key, j]; FrontWord = tile + FrontWord; j--; if (j >= 0 && ScrabbleBoard[CoordinatePairs[i].Key, j] != " ") { //adds tiles to the LEFT of the played tile Storage.Add(new KeyValuePair <int, int>(CoordinatePairs[i].Key, j)); } } FrontWord = FrontWord.Trim(); //adding original coordinate Storage.Add(new KeyValuePair <int, int>(CoordinatePairs[i].Key, CoordinatePairs[i].Value)); string MainLetter = ScrabbleBoard[CoordinatePairs[i].Key, CoordinatePairs[i].Value]; j = CoordinatePairs[i].Value + 1; string BackWord = ""; if (j <= 14 && ScrabbleBoard[CoordinatePairs[i].Key, j] != " ") { Storage.Add(new KeyValuePair <int, int>(CoordinatePairs[i].Key, j)); } while (j <= 14 && ScrabbleBoard[CoordinatePairs[i].Key, j] != " ") { tile = ScrabbleBoard[CoordinatePairs[i].Key, j]; BackWord += tile; j++; if (j <= 14 && ScrabbleBoard[CoordinatePairs[i].Key, j] != " ") { //adds tiles to the RIGHT of the played tile Storage.Add(new KeyValuePair <int, int>(CoordinatePairs[i].Key, j)); } } //removes trailing white space BackWord = BackWord.Trim(); int FrontLength = FrontWord.Length; int BackLength = BackWord.Length; for (int a = 0; a <= FrontLength; a++) { for (int b = 0; b <= BackLength; b++) { if (SearchWord.ValidWord(FrontWord.Substring(a, FrontLength - a) + MainLetter + BackWord.Substring(0, BackLength - b)) && !DuplicateWords.Contains(FrontWord.Substring(a, FrontLength - a) + MainLetter + BackWord.Substring(0, BackLength - b))) { List <KeyValuePair <int, int> > Temp = Storage.GetRange(a, FrontLength - a + 1 + BackLength - b); DuplicateWords.Add(FrontWord.Substring(a, FrontLength - a) + MainLetter + BackWord.Substring(0, BackLength - b)); foreach (KeyValuePair <int, int> item in Temp) { FoundWords.Add(new KeyValuePair <int, int>(item.Key, item.Value)); } //check to make sure board is not empty and checks if there is any islands if (MainWindow.ValidPairs.Count == 0 || IsConnect(Temp)) { Connect = 1; } HorizontalWord = FrontWord.Substring(a, FrontLength - a) + MainLetter + BackWord.Substring(0, BackLength - b); int points = CalculateScore(HorizontalWord); TotalScore += points; MainWindow.OutPutTextBox.AppendText("The word " + HorizontalWord + " is worth " + points + " points" + "\n"); MainWindow.OutPutTextBox.ScrollToCaret(); //marking all letters in the hash table as true foreach (KeyValuePair <int, int> item in FoundWords) { UsedLetters[item.Key, item.Value] = true; } //checking to see if all letters placed on the board are used foreach (KeyValuePair <int, int> item in CoordinatePairs) { if (UsedLetters[item.Key, item.Value] == false) { flag = 1; } } } } } } if (flag == 0 && Connect == 1) { return(TotalScore); } //otherwise they are islands return(-1); }
public Game() { SearchWord search = new SearchWord(); }