PDA

View Full Version : array index out of bounds error in knights tour problem


klackey19
09-16-2009, 02:17 AM
Hi. Im working on the knights tour problem for a class. When I run the following code, I am told there is an array index out of bounds error at line 195 which is when board is set to the current move at smallestAccessibility == 0. Any help would be greatly appreciated. THanks in advance! Im using netbeans if that helps.

package knightstour;

/**
*
* @author Administrator
*/
public class KnightsTour {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int currentPosX = 0;
int currentPosY = 0;
int nextPossibleMove1X = 0;
int nextPossibleMove1Y = 0;
int nextPossibleMove2X = 0;
int nextPossibleMove2Y = 0;
int nextPossibleMove3X = 0;
int nextPossibleMove3Y = 0;
int nextPossibleMove4X = 0;
int nextPossibleMove4Y = 0;
int nextPossibleMove5X = 0;
int nextPossibleMove5Y = 0;
int nextPossibleMove6X = 0;
int nextPossibleMove6Y = 0;
int nextPossibleMove7X = 0;
int nextPossibleMove7Y = 0;
int nextPossibleMove8X = 0;
int nextPossibleMove8Y = 0;
int nextNextPossibleMove1X = 0; //used when there is a tie in accessibility ratings
int nextNextPossibleMove1Y = 0;
int nextNextPossibleMove2X = 0;
int nextNextPossibleMove2Y = 0;
int nextNextPossibleMove3X = 0;
int nextNextPossibleMove3Y = 0;
int nextNextPossibleMove4X = 0;
int nextNextPossibleMove4Y = 0;
int nextNextPossibleMove5X = 0;
int nextNextPossibleMove5Y = 0;
int nextNextPossibleMove6X = 0;
int nextNextPossibleMove6Y = 0;
int nextNextPossibleMove7X = 0;
int nextNextPossibleMove7Y = 0;
int nextNextPossibleMove8X = 0;
int nextNextPossibleMove8Y = 0;
int move;
move = 2;
int [][] board = {{1,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0}};


int [] toFindLowest = new int [8];

int [][] accessibility = {
{2, 3, 4, 4, 4, 4, 3, 2},
{3, 4, 6, 6, 6, 6, 4, 3},
{4, 6, 8, 8, 8, 8, 6, 4},
{4, 6, 8, 8, 8, 8, 6, 4},
{4, 6, 8, 8, 8, 8, 6, 4},
{4, 6, 8, 8, 8, 8, 6, 4},
{3, 4, 6, 6, 6, 6, 4, 3},
{2, 3, 4, 4, 4, 4, 3, 2}
};

for (move = 2; move < (65);)
{

findNextMove (currentPosX, currentPosY, nextPossibleMove1X,
nextPossibleMove1Y, nextPossibleMove2X,
nextPossibleMove2Y, nextPossibleMove3X,
nextPossibleMove3Y, nextPossibleMove4X,
nextPossibleMove4Y, nextPossibleMove5X,
nextPossibleMove5Y, nextPossibleMove6X,
nextPossibleMove6Y, nextPossibleMove7X,
nextPossibleMove7Y, nextPossibleMove8X,
nextPossibleMove8Y, board, accessibility,
toFindLowest, move);

move++;
}
PrintBoard (board);

};

static void PrintBoard (int board[][]){

for (int row = 0; row < 8; row++)
{
for (int col = 0; col < 8; col++)
System.out.print(board[row][col] + " ");

System.out.print("\n");
}

}



static void findNextMove (int currentPosX, int currentPosY, int nextPossibleMove1X,
int nextPossibleMove1Y, int nextPossibleMove2X,
int nextPossibleMove2Y, int nextPossibleMove3X,
int nextPossibleMove3Y, int nextPossibleMove4X,
int nextPossibleMove4Y, int nextPossibleMove5X,
int nextPossibleMove5Y, int nextPossibleMove6X,
int nextPossibleMove6Y, int nextPossibleMove7X,
int nextPossibleMove7Y, int nextPossibleMove8X,
int nextPossibleMove8Y, int board[][],
int accessibility [][], int toFindLowest[], int move){


System.out.println("currentPosX: " + currentPosX);
System.out.println("currentPosY: " + currentPosY);
System.out.println("move: " + move);
for (int index = 0; index < 8; index++)
toFindLowest[index] = 10;

if (currentPosX != 7 || currentPosX != 6 || currentPosY != 0)
nextPossibleMove1X = (currentPosX + 2);
nextPossibleMove1Y = (currentPosY - 1);
if (nextPossibleMove1X >= 0 && nextPossibleMove1Y >= 0 && nextPossibleMove1X < 8 && nextPossibleMove1Y < 8)
if (board[nextPossibleMove1Y][nextPossibleMove1X] == 0)
toFindLowest[0] = accessibility[nextPossibleMove1Y][nextPossibleMove1X];

if (currentPosX != 6 || currentPosX != 7 || currentPosY != 7)
nextPossibleMove2X = (currentPosX + 2);
nextPossibleMove2Y = (currentPosY + 1);
if (nextPossibleMove2X >= 0 && nextPossibleMove2Y >= 0 && nextPossibleMove2X < 8 && nextPossibleMove2Y < 8)
if (board[nextPossibleMove2Y][nextPossibleMove2X] == 0)
toFindLowest[1] = accessibility[nextPossibleMove2Y][nextPossibleMove2X];

if (currentPosX != 0 || currentPosX != 1 || currentPosY != 0)
nextPossibleMove3X = (currentPosX - 2);
nextPossibleMove3Y = (currentPosY - 1);
if (nextPossibleMove3X >= 0 && nextPossibleMove3Y >= 0 && nextPossibleMove3X < 8 && nextPossibleMove3Y < 8)
if (board[nextPossibleMove3Y][nextPossibleMove3X] == 0)
toFindLowest[2] = accessibility[nextPossibleMove3Y][nextPossibleMove3X];

if (currentPosX != 0 || currentPosX != 1 || currentPosY!= 7)
nextPossibleMove4X = (currentPosX - 2);
nextPossibleMove4Y = (currentPosY + 1);
if (nextPossibleMove4X >= 0 && nextPossibleMove4Y >= 0 && nextPossibleMove4X < 8 && nextPossibleMove4Y < 8)
if (board[nextPossibleMove4Y][nextPossibleMove4X] == 0)
toFindLowest[3] = accessibility[nextPossibleMove4Y][nextPossibleMove4X];

if (currentPosX != 0 || currentPosY != 6 || currentPosY != 7)
nextPossibleMove5X = (currentPosX - 1);
nextPossibleMove5Y = (currentPosY + 2);
if (nextPossibleMove5X >= 0 && nextPossibleMove5Y >= 0 && nextPossibleMove5X < 8 && nextPossibleMove5Y < 8)
if (board[nextPossibleMove5Y][nextPossibleMove5X] == 0)
toFindLowest[4] = accessibility[nextPossibleMove5Y][nextPossibleMove5X];

if (currentPosX != 7 || currentPosY != 6 || currentPosY != 7)
nextPossibleMove6X = (currentPosX + 1);
nextPossibleMove6Y = (currentPosY + 2);
if (nextPossibleMove6X >= 0 && nextPossibleMove6Y >= 0 && nextPossibleMove6X < 8 && nextPossibleMove6Y < 8)
if (board[nextPossibleMove6Y][nextPossibleMove6X] == 0)
toFindLowest[5] = accessibility[nextPossibleMove6Y][nextPossibleMove6X];

if (currentPosX != 0 || currentPosY != 1 || currentPosY != 0)
nextPossibleMove7X = (currentPosX - 1);
nextPossibleMove7Y = (currentPosY - 2);
if (nextPossibleMove7X >= 0 && nextPossibleMove7Y >= 0 && nextPossibleMove7X < 8 && nextPossibleMove7Y < 8)
if (board[nextPossibleMove7Y][nextPossibleMove7X] == 0)
toFindLowest[6] = accessibility[nextPossibleMove7Y][nextPossibleMove7X];

if (currentPosX != 7 || currentPosY != 1 || currentPosY != 0)
nextPossibleMove8X = (currentPosX + 1);
nextPossibleMove8Y = (currentPosY - 2);
if (nextPossibleMove8X >= 0 && nextPossibleMove8Y >= 0 && nextPossibleMove8X < 8 && nextPossibleMove8Y < 8)
if (board[nextPossibleMove8Y][nextPossibleMove8X] == 0)
toFindLowest[7] = accessibility[nextPossibleMove8Y][nextPossibleMove8X];

int smallestAccessibility;
int maxIndex = 0;
for (int index = 1; index < 8; index++)
if (toFindLowest[maxIndex] > toFindLowest[index])
maxIndex = index;
smallestAccessibility = maxIndex;

//make move
if (smallestAccessibility == 0)
{
board[nextPossibleMove1Y][nextPossibleMove1X] = move;
currentPosX = (currentPosX + 2);
currentPosY = (currentPosY - 1);
}
else if (smallestAccessibility == 1)
{
board[nextPossibleMove2Y][nextPossibleMove2X] = move;
currentPosX = (currentPosX + 2);
currentPosY = (currentPosY + 1);
}
else if (smallestAccessibility == 2)
{
board[nextPossibleMove3Y][nextPossibleMove3X] = move;
currentPosX = (currentPosX - 2);
currentPosY = (currentPosY - 1);
}
else if (smallestAccessibility == 3)
{
board[nextPossibleMove4Y][nextPossibleMove4X] = move;
currentPosX = (currentPosX - 2);
currentPosY = (currentPosY + 1);
}
else if (smallestAccessibility == 4)
{
board[nextPossibleMove5Y][nextPossibleMove5X] = move;
currentPosX = (currentPosX - 1);
currentPosY = (currentPosY + 2);
}
else if (smallestAccessibility == 5)
{
board[nextPossibleMove6Y][nextPossibleMove6X] = move;
currentPosX = (currentPosX + 1);
currentPosY = (currentPosY + 2);
}
else if (smallestAccessibility == 6)
{
board[nextPossibleMove7Y][nextPossibleMove7X] = move;
currentPosX = (currentPosX - 1);
currentPosY = (currentPosY - 2);
}
else if (smallestAccessibility == 7)
{
board[nextPossibleMove8Y][nextPossibleMove8X] = move;
currentPosX = (currentPosX + 1);
currentPosY = (currentPosY - 2);
}


}
}

BubikolRamios
09-16-2009, 02:56 AM
it is what error says, if forinstance board is of size 3,3, then

board[3][5] would give you that error.

Owerall your code looks much to complicated for that problem.

consider this:
- all your possible moves x dimension is in distance abs(current_pos_x - 2)
- all your possible moves y dimension is in distance abs(current_pos_y - 2)

good luck !

klackey19
09-16-2009, 10:59 PM
okay, i totally re-did my code to make it less complicated but i'm still getting an array index out of bounds error. HELP !! here is my new code:

package knightsTour;

public class Tour {

/**
* @param args
*/
public static void main(String[] args) {

int NUM_ROWS = 8;
int NUM_COLUMNS = 8;
int [][] board = new int [NUM_COLUMNS][NUM_ROWS];
int [][]moves = new int [][] { {2, 1}, {2, -1}, {1, 2}, {1, -2}, {-1, 2}, {-2, 1}, {-2, -1} };

int currentPosX = 0;
int currentPosY = 0;

int positionX = 0;
int positionY = 0;

board[currentPosX][currentPosY] = 1;

int moveNumber = 2;
int [] move = new int [] {0, 0};
move [0] = 0;
move [1] = 0;

int i;
for (i = 0; i < NUM_ROWS * NUM_COLUMNS; i++)
{
move [0] = currentPosX;
move[1] = currentPosY;
findNextMoves(positionX, positionY, move, currentPosX, currentPosY, moves, board, NUM_ROWS);
currentPosX = move[0];
currentPosY = move[1];
board[currentPosX][currentPosY] = moveNumber;
moveNumber = (moveNumber + 1);
}

printBoard (board, NUM_ROWS, NUM_COLUMNS);

}

public static boolean availableAndOnBoard (int currentPosX, int currentPosY, int board[][], int NUM_ROWS){
if (currentPosX < NUM_ROWS && currentPosX >= 0 && currentPosY < NUM_ROWS && currentPosY >= 0 && board[currentPosX][currentPosY] == 0)
return true;
else
return false;
}

public static int getAccessibility (int newX, int newY, int moves[][], int board [][], int NUM_ROWS){
int x = newX;
int y = newY;
int accessibility = 0;
int i;
for (i = 0; i < NUM_ROWS; i++)
{
if (availableAndOnBoard(x + moves[i][0], y + moves[i][1], board, NUM_ROWS))
accessibility = (accessibility + 1);
}
return accessibility;
}

static void findNextMoves (int positionX, int positionY, int move[], int currentPosX, int currentPosY, int moves[][], int board[][], int NUM_ROWS){

positionX = move[0];
positionY = move[1];
int accessibility = NUM_ROWS;
int i;

for (i = 0; i < NUM_ROWS; i++)
{
int newX = (currentPosX + moves[i][0]);
int newY = (currentPosY + moves[i][1]);
int newAccessibility = getAccessibility(newX, newY, moves, board, NUM_ROWS);
if (availableAndOnBoard(newX, newY, board, NUM_ROWS) && newAccessibility < accessibility)
{
move[0] = newX;
move[1] = newY;
accessibility = newAccessibility;
}
}
}

static void printBoard (int board[][], int NUM_ROWS, int NUM_COLUMNS){
for (int row = 0; row < NUM_ROWS; row++)
{
for (int col = 0; col < NUM_COLUMNS; col++)
System.out.println(" " + board[row][col]);
System.out.print("\n");
}
}

}

sanmk4890
10-26-2009, 11:17 AM
hi,
the same error occurred in my c++ program. in my case the problem was that the code was,for some reason, removing more elements from the array than i had put into it. so try debugging by printing values at critical points of the program like storing and retrieving. i solved my problem by assigning a separate count variable for the number of elements i was storing.then restrict the number of elements popped out at any instant to this count.

Shyama_Mukherji
10-26-2009, 11:46 AM
Hello,

Refer to the following statement in your code:

int smallestAccessibility;

Try initializing the smallestAccessibility to 0 before using it in statement:
if (smallestAccessibility == 0) .... in your code to refer to the array board[][].