//Find collisions (where two words use the same grid position as two different characters) private static List<IntPoint> findCollisions(Dictionary<string, char>[,] collisionTable) { List<IntPoint> collisions = new List<IntPoint>(); for(int i = 0; i < collisionTable.GetLength(0); i++) //Cols { for(int j = 0; j < collisionTable.GetLength(1); j++) //Rows { Dictionary<string, char> words = collisionTable[i, j]; //If there is more than one word making use of this character, check that they are all using it as the same character if (words.Count > 1) { char? firstChar = null; foreach (char c in words.Values) { //If first iter, store char if (firstChar == null) { firstChar = c; } else //Otherwise this is a later iteration, check that the char this word is using this position as is the same as previous words { //If this word is using this charcter differently to how the first word used it, there's a collision if (c != firstChar) { collisions.Add(new IntPoint(i, j)); } } } } } } return collisions; }
//Generates a collision table (a lookup table for each cell in the wordsearch that shows each word using it and what character // that word uses it as) private static Dictionary<string, char>[,] generateCollisionTable(Dictionary<string, WordPosition> wordPositions, int cols, int rows) { //Build up a representation of the Wordsearch and what characters each word uses Dictionary<string, char>[,] collisionTable = new Dictionary<string, char>[cols, rows]; for(int i = 0; i < collisionTable.GetLength(0); i++) { for(int j = 0; j < collisionTable.GetLength(1); j++) { collisionTable[i, j] = new Dictionary<string, char>(); } } //Iterate over each word, populating the wordsearch foreach(KeyValuePair<string, WordPosition> kvp in wordPositions) { string word = kvp.Key; WordPosition position = kvp.Value; //Foreach character the word goes through int charIdx = 0; foreach(IntPoint charPosition in position.CharIndices) { //Store that this word goes through this character collisionTable[charPosition.X, charPosition.Y].Add(word, word[charIdx]); charIdx++; } } return collisionTable; }