1. Member
Join Date
Jan 2008
Posts
1
Rep Power
0

## PLEASE!!!help me to find mistake

public class KnightsTour {
Vars locs[];
private int size, max=1, d=0, board[][];

public KnightsTour(int x,int y, int newSize)
{
size=newSize;
locs=new Vars[size*size+1];
for(int n=1;n<=size*size;n++)
{
locs[n]=new Vars();
}
board=new int[size+1][size+1];
for(int n=1;n<=size;n++)
{
for(int n2=1;n2<=size;n2++)
{
board[n][n2]=0;
}
}
locs[max].x=x;
locs[max].y=y;
locs[max].d=1;
board[x][y]=max;
max++;
}
class Vars{
int x;
int y;
int d;
}
public void GO()
{
int n=0;
while(max<=size*size)
{
n++;
d++;
if(d>8)
{
max--;
board[locs[max].x][locs[max].y]=0;
d=locs[max].d+1;
}
move();
}
printBoard();
}

public void move()
{
int x=locs[max-1].x, y=locs[max-1].y;
switch(d)
{
case 1:x--;y-=2;break;
case 2:x++;y-=2;break;
case 3:x+=2;y--;break;
case 4:x+=2;y++;break;
case 5:x++;y+=2;break;
case 6:x--;y+=2;break;
case 7:x-=2;y++;break;
case 8:x-=2;y--;break;
}
//System.out.println(" X: "x" Y: "y" |"+max);
if((x<1)||(x>size)||(y<1)||(y>size)){}
else if(board[x][y]!=0){}
else
{
locs[max].x=x;
locs[max].y=y;
locs[max].d=d;
board[x][y]=max;
max++;
d=0;
//printBoard();
}
}

public void printBoard()
{
for(int n=1;n<=size;n++)
{
for(int n2=1;n2<=size;n2++)
{
if(board[n2][n]<10)
System.out.print(board[n2][n]+" ");
else
System.out.print(board[n2][n]+" ");
}
System.out.println();
}
//System.out.println();
System.out.println();
}
}
public static void main (String[]args){
KnightsTour k = new KnightsTour(1,1,8);
}

2. Member
Join Date
Jan 2008
Posts
4
Rep Power
0
What is the error you are getting.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•