Results 1 to 2 of 2
  1. #1
    goorioles747 is offline Member
    Join Date
    Nov 2007
    Posts
    1
    Rep Power
    0

    Default Knights Tour problem

    I am trying to make a knights tour program. I was going to use stacks, but decided to just use an object array. The problem I have is when it backtracks, it doesnt always continue in the direction it left off from. here is my code:
    (Vars is a class with variables x, y, and d) d is direction
    my demo is simply:

    Java Code:
    KnightsTour5 knight=new KnightsTour5(1,1,size);
    	knight.GO();


    the rest:

    import java.util.Stack;
    public class KnightsTour5 {
    Vars locs[]=new Vars[0];
    private int size, max=1, d=0, board[][];

    Java Code:
    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=0;
    	board[x][y]=max;
    }
     
    public void GO()
    {
    	int n=0;
    	while(n<250)
    	{
    		n++;
    		d++;
    		if(d>8)
    		{
    			board[locs[max].x][locs[max].y]=0;
    			locs[max].d=0;
    			max--;
    			d=locs[max].d+1;
    		}
    		move();
    	}
    }
     
    public void move()
    {
    	int x=locs[max].x, y=locs[max].y;
    	switch(d)
    	{
    	case 1:x--;y-=2;System.out.print("a");break;
    	case 2:x++;y-=2;System.out.print("b");break;
    	case 3:x+=2;y--;System.out.print("c");break;
    	case 4:x+=2;y++;System.out.print("d");break;
    	case 5:x++;y+=2;System.out.print("e");break;
    	case 6:x--;y+=2;System.out.print("f");break;
    	case 7:x-=2;y++;System.out.print("g");break;
    	case 8:x-=2;y--;System.out.print("h");break;
    	}
    	System.out.println(" X: "+x+" Y: "+y+" |"+(max+1));
    	if((x<1)||(x>size)||(y<1)||(y>size)){}
    	else if(board[x][y]!=0){}
    	else
    	{
    		max++;
    		locs[max].x=x;
    		locs[max].y=y;
    		locs[max].d=d;
    		board[x][y]=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();
    }
    }

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    10

    Default

    Your search algorithm goes from eleven o'clock clockwise through ten o'clock independent of location in the grid. Here's an idea that you might be able to implement: An Extremely Simple Solution.

Similar Threads

  1. Knights Tour.
    By atl2 in forum New To Java
    Replies: 1
    Last Post: 02-06-2008, 08:31 PM

Posting Permissions

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