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