Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

KnightsTour(heuristic solution)

dd9204a2-204e-43cf-9dcb-ac0566ad24ebMay 12 2015 — edited May 17 2015

can I ask for help?

i have a problem of converting this code into heuristic solution. I don't know what condition should I put to make heuristic solution works.This is just random move that will not complete the 64 tours.

    import java.util.Random;

   

    public class Knight

    {

    Random rand=new Random();

    public void start()

    {

    int[][] square = new int[8][8];

    int currentRow;

    int currentColumn;

    int x,y,count,count1=0;

    int moveNumber;

    int [] horizontal={2,1,-1,-2,-2,-1,1,2};

    int [] vertical={-1,-2,-2,-1,1,2,2,1};

  

  

    for(x=0;x<8;x++)

    {

    for(y=0;y<8;y++)

    {

    square[x][y]=0;

    }

    }

  

    currentRow=rand.nextInt(7);

    currentColumn=rand.nextInt(7);

    square[currentRow][currentColumn]=1;

  

    for(count=2;count<=64;count++)

    {

    for(moveNumber=0;moveNumber<8;moveNumber++)

    {

    currentRow+=vertical[moveNumber];

    currentColumn+=horizontal[moveNumber];

    if(currentColumn >= 0 && currentColumn < 8 && currentRow >= 0 && currentRow < 8 && square[currentRow][currentColumn]==0)

    {

    square[currentRow][currentColumn]=count;

    count1++;

    break;

    }

    else

    {

    currentRow-=vertical[moveNumber];

    currentColumn-=horizontal[moveNumber];

    }

    }

    }

  

    System.out.printf("The tour ended with %d moves\n \t0\t1\t2\t3\t4\t5\t6\t7\n",count1+1);

    for(x=0;x<8;x++)

    {

    System.out.printf("%d\t", x);

    for(y=0;y<8;y++)

    {

    System.out.printf("%d\t",square[x][y]);

    }

    System.out.println();

    }

    }

    }

    

If I were to use the heuristic solution,I need to add this code below but I have no clues how to continue changing it

    int access[  ][  ] = { { 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 } };

Much appreciate for helping.

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 14 2015
Added on May 12 2015
1 comment
801 views