private bool CheckCorrectWords(List <Letter> letters, WordDirection direction, List <Word> words) { foreach (var word in words) { if (word.IsComplete) { continue; } if (word.Direction != direction) { continue; } if (!letters.IsEqualTo(word.Letters)) { continue; } word.Complete(); FoundWords.Add(GetBounds(letters)); return(true); } return(false); }
public bool?FindVerticalWords() { bool foundAtLeastOneWord = false; for (int columnIndex = 0; columnIndex < Width; columnIndex++) { StringBuilder columnBuilder = new StringBuilder(); for (int row = 0; row < Height; row++) { columnBuilder.Append(Lines[row][columnIndex]); } string column = columnBuilder.ToString().ToLower(); //Does this string contains a word? for (int length = 6; 3 <= length; length--) { for (int row = 0; row <= Height - 3; row++) { if (6 < row + length) { break; } string wordCandidate = column.Substring(row, length); if (repository.IsAWord(wordCandidate)) { WordLocation locationToAdd = new WordLocation() { Column = columnIndex, Horizontal = false, Length = length, Row = row, Word = wordCandidate }; if (!IsAlreadyIncluded(FoundWords, locationToAdd)) { FoundWords.Add(locationToAdd); } foundAtLeastOneWord = true; break; //don't look for a shorter word. } if (Verbose) { Console.WriteLine($"{wordCandidate} is not a word."); } } } } return(foundAtLeastOneWord); }
private bool AddWordIfInWordList(string direction, IEnumerable <int> charIndexes, string wordString) { if (!wordList.IsInWordList(wordString)) { return(false); } // else we have a word! FoundWords.Add(wordString); FireFoundWordEvent(direction, charIndexes, wordString); return(true); }
public void Search(List <Letter> letters, List <Letter> dropLetters, List <Word> words) { // Initialize FoundWords.Clear(); FoundErrorWords.Clear(); // create location dictionary _location = new Dictionary <Vector3, Letter>(); letters.ForEach(l => _location.Add(l.transform.position, l)); // discover complete words foreach (WordDirection direction in Enum.GetValues(typeof(WordDirection))) { for (var i = 0; i < letters.Count; i++) { var letter = letters[i]; if (!IsStart(letter, direction)) { continue; } List <List <Letter> > foundLettersList = FindLettersList(letter, direction); foreach (var foundLetters in foundLettersList) { if (dropLetters.Count > 0 && !foundLetters.Any(dropLetters.Contains)) { continue; } if (!CheckCorrectWords(foundLetters, direction, words)) { CheckErrorWords(foundLetters, direction, words); } } } } if (FoundErrorWords.Count > 0 || FoundWords.Count > 0) { FollowMachine.SetOutput("Found"); } else { FollowMachine.SetOutput("Not found"); } }
public short FindAndCount(List <string> sentences) { if (sentences == null && sentences.Count == 0) { return(-1); } try { foreach (var wordToSearch in this.Words) { var foundWord = new FoundWord { Word = wordToSearch }; var sentenceNumber = 0; for (var i = 0; i < sentences.Count; i++) { sentenceNumber++; var sourceSentence = sentences[i].Split(new char[0], StringSplitOptions.RemoveEmptyEntries); var count = CountPerSentence(wordToSearch, sourceSentence); foundWord.NumOfFoundByWord += count; if (count > 0) { for (var x = 0; x < count; x++) { foundWord.AddInSentenceNumberFound(sentenceNumber); } } } FoundWords.Add(foundWord); } } catch (Exception) { return(-4); } return(0); }
static void Main(string[] args) { Console.SetWindowSize(Console.LargestWindowWidth - 35, Console.LargestWindowHeight); Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); string[] allWords = System.IO.File.ReadAllLines(@"words.txt"); IEnumerable <string> longWords = allWords.Where(word => word.Length > 2); Console.InputEncoding = Encoding.GetEncoding(1251); string gridAsWord = Console.ReadLine(); char[,] wordGrid = GetGrid(gridAsWord); Console.OutputEncoding = Encoding.GetEncoding(1251); for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { DFS(wordGrid, allWords, wordGrid[i, j].ToString(), new bool[4, 4], i, j);//start everywhere } } FoundWords.Sort(delegate(string x, string y) { if (x.Length == y.Length) { return(0); } return(y.Length - x.Length); }); foreach (var sortedWord in FoundWords) { Console.WriteLine(sortedWord); } }
public IList <string> Find(IEnumerable <string> src) { if (src == null) { return(new List <string>()); } //Build Matrix BuildMatrix(src); var numberOfMatrixCols = Matrix.GetLength(1); var numberOfMatrixRows = Matrix.GetLength(0); // Going to iterate over each position in the matrix. for (int colPos = 0; colPos < numberOfMatrixCols; colPos++) { for (int rowPos = 0; rowPos < numberOfMatrixRows; rowPos++) { // Search all directions, not just horizontally and vertically, but diagonally as well for (int d = 0; d < 8; d++) { SearchPostion(Matrix, colPos, rowPos, numberOfMatrixCols, numberOfMatrixRows, "", d); } } } var resultList = new List <string>(); if (FoundWords.Any()) { foreach (KeyValuePair <string, bool> word in FoundWords) { resultList.Add(word.Key); } } return(resultList); }
static void DFS(char[,] grid, string[] allWords, string currentWord, bool[,] visited, int currentI, int currentJ) { if (currentWord.Length > 10)//bottom { return; } if (currentWord.Length >= 3 && IsContained(currentWord, allWords) && !FoundWords.Contains(currentWord)) { FoundWords.Add(currentWord); //Console.WriteLine(currentWord); } visited[currentI, currentJ] = true; //go left if (currentJ > 0 && !visited[currentI, currentJ - 1]) { DFS(grid, allWords, currentWord + grid[currentI, currentJ - 1], visited, currentI, currentJ - 1); } //go right if (currentJ < 3 && !visited[currentI, currentJ + 1]) { DFS(grid, allWords, currentWord + grid[currentI, currentJ + 1], visited, currentI, currentJ + 1); } //go up if (currentI > 0 && !visited[currentI - 1, currentJ]) { DFS(grid, allWords, currentWord + grid[currentI - 1, currentJ], visited, currentI - 1, currentJ); } //go down if (currentI < 3 && !visited[currentI + 1, currentJ]) { DFS(grid, allWords, currentWord + grid[currentI + 1, currentJ], visited, currentI + 1, currentJ); } //go left up if (currentI > 0 && currentJ > 0 && !visited[currentI - 1, currentJ - 1]) { DFS(grid, allWords, currentWord + grid[currentI - 1, currentJ - 1], visited, currentI - 1, currentJ - 1); } //go left down if (currentI < 3 && currentJ > 0 && !visited[currentI + 1, currentJ - 1]) { DFS(grid, allWords, currentWord + grid[currentI + 1, currentJ - 1], visited, currentI + 1, currentJ - 1); } //right up if (currentI > 0 && currentJ < 3 && !visited[currentI - 1, currentJ + 1]) { DFS(grid, allWords, currentWord + grid[currentI - 1, currentJ + 1], visited, currentI - 1, currentJ + 1); } //go right down if (currentI < 3 && currentJ < 3 && !visited[currentI + 1, currentJ + 1]) { DFS(grid, allWords, currentWord + grid[currentI + 1, currentJ + 1], visited, currentI + 1, currentJ + 1); } visited[currentI, currentJ] = false; }
private void SearchPostion(char[,] matrix, int colPosition, int rowPosition, int numberOfCols, int numberOfRows, string build, int direction) { // Array bounds check. if (colPosition >= numberOfCols || colPosition < 0 || rowPosition >= numberOfRows || rowPosition < 0) { return; } // Get letter. char letter = matrix[rowPosition, colPosition]; // Append. string wordToSearchOn = build + letter; // Add a word to "FoundWords" dictionary object if, // as we're building a search string from the matrix position values (our switch statement below), // we find a match against in our dictionary of words. // Skip dups. if (!FoundWords.ContainsKey(wordToSearchOn)) { if (Dictionary.Any(x => x == wordToSearchOn)) { FoundWords.Add(wordToSearchOn, true); return; } } //Keep searching based on direction value: E(0), S(1), SE(2), W(3), N(4), NW(5), SW(6), NE(7) //Advance the search position via Recursion switch (direction) { //East case 0: SearchPostion(matrix, colPosition + 1, rowPosition, numberOfCols, numberOfRows, wordToSearchOn, direction); break; //South case 1: SearchPostion(matrix, colPosition, rowPosition + 1, numberOfCols, numberOfRows, wordToSearchOn, direction); break; //Southeast case 2: SearchPostion(matrix, colPosition + 1, rowPosition + 1, numberOfCols, numberOfRows, wordToSearchOn, direction); break; //West case 3: SearchPostion(matrix, colPosition - 1, rowPosition, numberOfCols, numberOfRows, wordToSearchOn, direction); break; //North case 4: SearchPostion(matrix, colPosition, rowPosition - 1, numberOfCols, numberOfRows, wordToSearchOn, direction); break; //Northwest case 5: SearchPostion(matrix, colPosition - 1, rowPosition - 1, numberOfCols, numberOfRows, wordToSearchOn, direction); break; //Southwest case 6: SearchPostion(matrix, colPosition - 1, rowPosition + 1, numberOfCols, numberOfRows, wordToSearchOn, direction); break; //Northeast case 7: SearchPostion(matrix, colPosition + 1, rowPosition - 1, numberOfCols, numberOfRows, wordToSearchOn, direction); break; } }