Question
Help me with this . thank you. package knt; public class TestKt { public static void main(String[] args) { // TODO Auto-generated method stub KTour
Help me with this . thank you.
package knt; public class TestKt { public static void main(String[] args) { // TODO Auto-generated method stub KTour k=new KTour(2,3); // use default constructor KTour() to // instantiate an object k of class Ktour // or create an instance of the class KTour } }
package knt; /* this program generates a solution to the Knight's Tour * using a random walk */ public class KTour { // define all instance variables first so that they have global scope int[][] cb=new int[8][8]; // create an 8 by 8 array of integers called cb int x,y; //start pos (x,y) int savex,savey; // you need this for cw/hw int h[]= {1,1,-1,-1,2,2,-2,-2}; //horizontal offset for x int v[]= {2,-2,2,-2,1,-1,1,-1}; // verticle offset for y int g,ctr=1; // randomly guess a move g from 0 to 7 short movesTried[]= {0,0,0,0,0,0,0,0}; //keeps track of guesses made int gctr=1; // counts the number of guesses for any given move KTour(){ //default constructor // set x and y to 0 // initBd(0,0) set all squares to 0 except upper left corner which is set to 1 savex=0; savey=0; setxy(0,0); // start position initBd(); // zero out cb array and put 1 into start position //printBd(); startTour(); // start the random walk } KTour(int m,int n){ // set x and y to m and n // initBd(m,n) set all squares to 0 except cb{m][n] which is set to 1 // print the board savex=m; savey=n; setxy(m,n); initBd(); //printBd(); startTour(); } void initBd() { for (int i=0; i<8; i++) for (int j=0; j<8; j++) cb [i][j]=0; x=savex; //restore x y=savey; cb[x][y]=ctr++; // start pos set to 1 then ctr becomes 2 } void printBd() { for (int i=0; i<8; i++) { for (int j=0; j<8; j++) System.out.print(cb[i][j]+" "); System.out.println(" "); } } void setxy(int m,int n) { x=m; y=n; } void startTour() { boolean going=true; while (going) { g=(int) (8*Math.random()); movesTried[g]=1; gctr=1; while (true) { if (gctr>7) { // printBd(); //break; x=0; y=0; ctr=1; initBd(); resetMovesTried(); break; } if (legal(g)) { makeMove(g); resetMovesTried(); if (ctr==65) { printBd(); break; } g=(int) (8*Math.random()); movesTried[g]=1; gctr=1; } else { g=(int)((8-gctr)*Math.random()); int i,c=-1; for (i=0;c
Add a failure counter that keeps track of the number of tours that failed. Find the average number of moves for each tour
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started