每次我在程序中都不会使它超过第10步,因为q总是传递9,这是我在switch语句中的最后一种情况,它使移动将其发送到默认值。请帮助有人知道我如何控制q,以便它不会通过9或如果它,我可以使该功能做其他事情。
import java.util.Arrays;
class KnightsTour
{
int the_board[][] = new int[8][8];
int moves = 0;
int row = 0, column = 0;
int j = 1;
int check;
final int backtrack = 9;
boolean tour_found;
boolean isSafe;
public KnightsTour()
{
}
/*recursive method, checks how many moves were made if 16 were made tour finished,
else if not moves knight checks if the move is valid if not back tracks*/
public void findTour(int q)
{
while(moves != 64)
{
if(q > 8)
{
isSafe = false;
}
if(moves == 64){
displayTour();
return;
}
else
if(moves < 64){
moves++;
System.out.println(moves + "\n" + "q:" +q);
move(q);
//displayTour();
}
if(isSafe == true)
{
if(q > 9)
{
findTour(0);
}
else findTour(++q);
}
else
if(isSafe == false)
{
moves--;
move(-q);
}
}
}
//used to keep prevent arrayindexoutofbounds error
public boolean arrayInBounds(int x, int y)
{
if(x < 8 && y < 8 && x >= 0 && y >= 0)
{
return true;
}
else return false;
}
/*move method uses switch statement to decide which move the knight should make
based on a case number, negative case numbers back track knight. if statement checks
if the current array element is empty and the index is inbounds*/
public void move(int a)
{
int counter = 0, nextMove = 1;
check = a;
switch (check)
{
case 1:
if(arrayInBounds(row+2, column+1) == true && the_board[row+2][column+1] <= 1){
the_board[row+2][column+1]+= j;
row = (row + 2);
column = (column + 1);
j++;
isSafe = true;
break;
}
else isSafe = false;
break;
case 2:
if (arrayInBounds(row+1, column+2) == true && the_board[row+1][column+2] <= 1)
{
the_board[row+1][column+2]=j;
row = (row+1);
column = (column+2);
j++;
isSafe = true;
break;
}
else isSafe = false;
break;
case 3:
if(arrayInBounds(row-1, column+2) == true && the_board[row-1][column+2] <= 1){
the_board[row-1][column+2]+=j;
row = (row-1);
column = (column+2);
j++;
isSafe = true;
break;
}
else isSafe = false;
break;
case 4:
if (arrayInBounds(row-2, column+1) == true && the_board[row-2][column+1] <= 1){
the_board[row-2][column+1]+=j;
row = (row-2);
column = (column+1);
j++;
isSafe = true;
break;
}
else isSafe = false;
break;
case 5:
if(arrayInBounds(row-2, column-1) == true && the_board[row-2][column-1] <= 1){
the_board[row-2][column-1]+=j;
row = (row-2);
column = (column-1);
j++;
isSafe = true;
break;
}
else isSafe = false;
break;
case 6:
if(arrayInBounds(row-1, column-2) == true && the_board[row-1][column-2] <= 1){
the_board[row-1][column-2]+=j;
row = (row-1);
column = (column-2);
j++;
isSafe = true;
break;
}
else isSafe = false;
break;
case 7:
if(arrayInBounds(row+1, column-2) == true && the_board[row+1][column-2] <= 1){
the_board[row+1][column-2]+=j;
row = (row+1);
column = (column-2);
j++;
isSafe = true;
break;
}
else isSafe = false;
break;
case 8:
if(arrayInBounds(row+2, column-1) == true && the_board[row+2][column-1] <= 1){
the_board[row+2][column-1]+=j;
row = (row+2);
column = (column-1);
j++;
isSafe = true;
break;
}
else isSafe = false;
break;
case -1:
move(backtrack);
if(isSafe == false){
the_board[row-2][column-1]+= 0;
row = (row - 2);
column = (column - 1);
j--;
isSafe = true;
}
break;
case -2:
move(backtrack);
if(isSafe == false){
the_board[row-1][column-2]+=0;
row = (row-1);
column = (column-2);
j--;
isSafe = true;
}
break;
case -3:
move(backtrack);
if(isSafe == false){
the_board[row+1][column-2]+=0;
row = (row+1);
column = (column-2);
j--;
isSafe = true;
}
break;
case -4:
move(backtrack);
if(isSafe == false){
the_board[row+2][column-1]+=0;
row = (row+2);
column = (column-1);
j--;
isSafe = true;
}
break;
case -5:
move(backtrack);
if(isSafe == false){
the_board[row+2][column+1]+=0;
row = (row+2);
column = (column+1);
j--;
isSafe = true;
}
break;
case -6:
move(backtrack);
if(isSafe == false){
the_board[row+1][column+2]+=0;
row = (row+1);
column = (column+2);
j--;
isSafe = true;
}
break;
case -7:
move(backtrack);
if(isSafe == false){
the_board[row-1][column+2]+=0;
row = (row-1);
column = (column+2);
j--;
isSafe = true;
}
break;
case -8:
move(backtrack);
if(isSafe == false){
the_board[row-2][column+1]+=0;
row = (row-2);
column = (column+1);
j--;
isSafe = true;
}
break;
case backtrack:
while(isSafe != true && counter != 7)
{
if(nextMove == check){
isSafe = false;
System.out.println("BACKTRACK12324");
nextMove++;
}
else
if(nextMove != check){
System.out.println("BACKTRACK");
displayTour();
move(nextMove);
nextMove++;
counter++;
}
}
break;
default:
System.out.println("No tour found");
displayTour();
}
}
//for loop to display tour once found//
public void displayTour()
{
String result = "";
int v = 1;
for (int i = 0; i < 8; i++)
{
for (int e = 0; e < 8; e++)
{
if(v % 8 == 0)
{
System.out.println(the_board[i][e] + "\t");
System.out.println("\n");
}
else
System.out.print(the_board[i][e] + "\t");
v++;
}
}
}
}