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);
}
}
}
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);
}
}
}