Results 1 to 2 of 2
  1. #1
    Join Date
    Aug 2011
    Posts
    1
    Rep Power
    0

    Default Knight's Tour, refusing to pass move #29, code included, please help.

    So, my homework is to code a program to simulate a Knight's Tour. It needs to pass all 64 spaces on an 8x8 grid, without passing the same space twice.

    but my program seems to not want to pass the 29th move. I've checked everything, but I can't manage to find out where I'm wrong.
    Maybe someone could assist me in finding where the problem lies.

    Much appreciated!

    Java Code:
    public class KnightsTour
    {
        int grid[][];
        boolean finished;
        final int Xcord[] = {-1, 1, -1, 1, -2, 2, -2, 2};
        final int Ycord[] = {-2, 2, 2, -2, -1, 1, 1, -1};
    
        public static void main(String args[])
        {
            int totalMoves = 1;
            KnightsTour kt = new KnightsTour();
            kt.check( 0, 0, totalMoves);
            kt.drawGrid();
        }
        
        public KnightsTour()
        {
            grid = new int[8][8];
            finished = false;
            grid[0][0] = 1;
        }
        
        public int check(int x, int y, int totalMoves)
        {
            for(int i = 0; i < grid.length; i++)
            {
                if(finished == true)
                {
                    return totalMoves;
                }
                else
                {
                    if(x+Xcord[i]<8 && x+Xcord[i]>=0)
                    {
                        if(y+Ycord[i]<8 && y+Ycord[i]>=0)
                        {                
                            if((grid[x+Xcord[i]][y+Ycord[i]])==0)
                            {
                                grid[x+Xcord[i]][y+Ycord[i]]=-1;
                                grid[x+Xcord[i]][y+Ycord[i]]=check(x+Xcord[i],y+Ycord[i], totalMoves + 1);
                                finished = true; 
                            }
                        }     
                    }
                }  
            }
            if(totalMoves >= 64 || finished == true)
            {
                finished = true;
                return totalMoves;
            }
            else
            {
                return 0;
            }    
        }
        
        public void drawGrid()
        {
            for(int i = 0; i < grid.length; i++)
            {
                System.out.println();
                for(int j = 0; j < grid.length; j++)
                {
                    if(grid[j][i] < 10)
                    {
                        System.out.print(" 0" + grid[j][i]);
                    }
                    else
                    {
                        System.out.print(" " + grid[j][i]);
                    }
                }
            }
        }
    
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default

    Have you tried debugging the code by adding printlns to show the reason why it stops searching before the end?
    What values of what variables stops the search?

Similar Threads

  1. Replies: 0
    Last Post: 03-21-2011, 09:46 AM
  2. JDBC code not included into JAR file??
    By friday in forum Advanced Java
    Replies: 1
    Last Post: 12-17-2010, 07:13 PM
  3. How to fix this code to move this shape?
    By BeijingDuck in forum AWT / Swing
    Replies: 8
    Last Post: 12-15-2010, 11:50 PM
  4. help on Knights tour
    By frank17 in forum New To Java
    Replies: 4
    Last Post: 01-30-2010, 11:00 PM
  5. Knights Tour.
    By atl2 in forum New To Java
    Replies: 1
    Last Post: 02-06-2008, 07: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
  •