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?
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.