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 IsAlreadyIncluded(List <WordLocation> foundWords, WordLocation locationToAdd) { foreach (var location in foundWords) { if (location.Word == locationToAdd.Word & location.Column == locationToAdd.Column & location.Row == locationToAdd.Row) { return(true); } } return(false); }
public bool FindHorizontalWords() { bool foundAtLeastOneWord = false; for (int row = 0; row < Height; row++) { for (int column = 0; column <= Width - 3; column++) { for (int length = Width - column; 3 <= length; length--) { var wordCandidate = Lines[row].Substring(column, length).ToLower(); if (repository.IsAWord(wordCandidate)) { if (Verbose) { Console.WriteLine($"{row} {column} {length} {wordCandidate} is a word."); } WordLocation locationToAdd = new WordLocation() { Column = column, Row = row, Length = length, Word = wordCandidate }; if (!IsAlreadyIncluded(FoundWords, locationToAdd)) { FoundWords.Add(locationToAdd); } foundAtLeastOneWord = true; break; //don't look for substrings in this column. } if (Verbose) { Console.WriteLine($"{row} {column} {length} {wordCandidate} is NOT a word."); } } } } return(foundAtLeastOneWord); }