Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Grid Traversal

 
Stephen Murphy
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic