Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I search a two dimensional array in any direction
    primarykey
    data
    text
    <p>I'm writing a word search puzzle in C# and I would like to be able to search the two dimensional array of characters for the words in an elegant manner. </p> <p>The basic searches left to right, top to bottom etc, are not to difficult to write, however things start getting a little verbose when searching diagonally accross the array. I've got it working but I'm sure there's a better solution out there.</p> <p>Here's an example of a puzzle I'm trying to solve, any ideas would be greatly appreciated.</p> <p>BXXD<br> AXEX<br> TRXX<br> FXXX </p> <p>BAT FRED</p> <p><strong>EDIT:</strong> Kudos to Steve for giving me the idea of searching compass points</p> <p><strong>EDIT:</strong> The result of the search needs to return the x1, y1 and x2, y2 co-ordinates of the words within the array.</p> <p><strong>EDIT:</strong> Thanks to Antti for providing a good algorithm for searching the array.</p> <p>This the final result I came up with. I've based it on the algorithm in Antti's answer, having modified it to return the array offsets for the beginning and the end of any words found. This algorithm is going to be used a Word Search game I'm writing in WPF, for my kids. Thanks to everyone for helping me along. I'll post a link here to the app when it's respectable.</p> <pre><code>public class Range { public Range(Coordinate start, Coordinate end) { Start = start; End = end; } public Coordinate Start { get; set; } public Coordinate End { get; set; } } public class Coordinate { public Coordinate(int x, int y) { X = x; Y = y; } public int X { get; set; } public int Y { get; set; } } public class WordSearcher { public WordSearcher(char[,] puzzle) { Puzzle = puzzle; } public char[,] Puzzle { get; set; } // represents the array offsets for each // character surrounding the current one private Coordinate[] directions = { new Coordinate(-1, 0), // West new Coordinate(-1,-1), // North West new Coordinate(0, -1), // North new Coordinate(1, -1), // North East new Coordinate(1, 0), // East new Coordinate(1, 1), // South East new Coordinate(0, 1), // South new Coordinate(-1, 1) // South West }; public Range Search(string word) { // scan the puzzle line by line for (int y = 0; y &lt; Puzzle.GetLength(0); y++) { for (int x = 0; x &lt; Puzzle.GetLength(1); x++) { if (Puzzle[y, x] == word[0]) { // and when we find a character that matches // the start of the word, scan in each direction // around it looking for the rest of the word var start = new Coordinate(x, y); var end = SearchEachDirection(word, x, y); if (end != null) { return new Range(start, end); } } } } return null; } private Coordinate SearchEachDirection(string word, int x, int y) { char[] chars = word.ToCharArray(); for (int direction = 0; direction &lt; 8; direction++) { var reference = SearchDirection(chars, x, y, direction); if (reference != null) { return reference; } } return null; } private Coordinate SearchDirection(char[] chars, int x, int y, int direction) { // have we ve moved passed the boundary of the puzzle if (x &lt; 0 || y &lt; 0 || x &gt;= Puzzle.GetLength(1) || y &gt;= Puzzle.GetLength(0)) return null; if (Puzzle[y, x] != chars[0]) return null; // when we reach the last character in the word // the values of x,y represent location in the // puzzle where the word stops if (chars.Length == 1) return new Coordinate(x, y); // test the next character in the current direction char[] copy = new char[chars.Length - 1]; Array.Copy(chars, 1, copy, 0, chars.Length - 1); return SearchDirection(copy, x + directions[direction].X, y + directions[direction].Y, direction); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload