Another Java problem

hans030390

Diamond Member
Feb 3, 2005
7,326
2
76
Here's the description of a Numbrix puzzle:

Numbrix is a puzzle published in Parade Magazine each Sunday. The game is played on an nxn grid, where some of the entries contain integers. Starting anywhere, you fill in the blank squares with integers so they make a connected path, in ascending order, using the integers 1 through n^2. You can move horizontally or vertically one square in any direction, but diagonal moves are not allowed.

So far, I've made the following required for this assignment, and they do work:

static boolean contains(int[] a, int x)
-Takes an array of integers a and an integer x, and returns true if x appears as an element in a, and false otherwise.

static int[] neighbors(int[][] puzzle, int r, int c)
-Returns an array of size 4 containing the entries in the north, west, south, and east neighbors of the square at row r, column c in the given puzzle.

And this is what I'm trying to do:

static boolean verify(int[][] puzzle)
-Takes a 2-dimensional square array of integers, representing a Numbrix puzzle, and returns true if the entries correspond to a valid solution, and false otherwise.

Now, it compiles fine. It's just running it gives me an error about "index out of bounds". Here's my code for "verify" so far...it uses neighbors, which I have tested (and it works). I'm just not sure what I'm missing (or what I missed in my code):

public static boolean verify(int[][] puzzle) {
int num = 1;
int i = 0;
int j = 0;

while (i < puzzle.length) {
while (j < puzzle.length) {
if (puzzle[ i][j] == num)
break;
else
j++;
}
i++;
j = j - j;
}

while (num < puzzle.length * puzzle.length) {
int[] neighbors = neighbors(puzzle, i, j);

if (neighbors[0] == num + 1) {
i--;
num++; }
else if (neighbors[1] == num + 1) {
j--;
num++; }
else if (neighbors[2] == num + 1) {
i++;
num++; }
else if (neighbors[3] == num + 1) {
j++;
num++; }
else return false;
}

return true;

}

I really appreciate any help you can give.
 

mundane

Diamond Member
Jun 7, 2002
5,603
8
81
Your error message should tell you which line is using an invalid index.

Not all cells will have four neighbors.
 

hans030390

Diamond Member
Feb 3, 2005
7,326
2
76
Originally posted by: mundane
Your error message should tell you which line is using an invalid index.

Not all cells will have four neighbors.

Sorry, I forgot to specify that if there is no "neighbor", it puts a 0 in that index value. The index values are the numbers N, W, S, and E of the number you're looking at.

The error says the error is on the "int[] neighbors = neighbors(puzzle, i, j);" line, but it's also giving me errors for the neighbors and main code, though neighbors works for me.
 

flashbacck

Golden Member
Aug 3, 2001
1,921
0
76
well, Index out of bounds means you're asking for a value from an array using an index that is negative or too large.

If it says your error's here: int[] neighbors = neighbors(puzzle, i, j);, then it's not in your first two while loops. So is there anyplace in your third while loop where you might end up having an invalid index?
 

hans030390

Diamond Member
Feb 3, 2005
7,326
2
76
Found out it actually was my neighbors code. I also had to basically scrap my verify code.

Anyway, I got it figured out at office hours.