This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Grid Traversal Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Grid Traversal" Watch "Grid Traversal" New topic

Grid Traversal

Stephen Murphy

Joined: Feb 26, 2004
Posts: 24
I am trying to code a program to traverse a grid searching for certain words.
I need to traverse it in 8 directions: up, down, left, right, and 4 diagonals.
I am lost as how to code the diagonals, since the grids width and height are not constant when moving on a slant.

Here is my code for left ot right traversal:

Code for the other directions are simply reversals of direction or Array subscripts.

Any ideas on how i would implement the diagonal traversal?
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
You need to constrain the starting cell range based on the length of the String you are seeking as you've done above, but this time both axis will be limited. For a 5x5 grid, searchUpRight("bye") would check these starting locations.Tip: As you're dealing with characters in the grid, you could switch grid and findArray to char arrays and use == instead of equals() for comparing cells. String.charAt(index) returns the character as a char at index.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
jQuery in Action, 2nd edition
subject: Grid Traversal
Similar Threads
June Newsletter Puzzle
2-Dimensional Arrays?
Alignment within a table?
Java Code
Newsletter Inverse