File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
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
I agree. Here's the link:
subject: Grid Traversal
jQuery in Action, 3rd edition