Question: In Java, create the knights tour project, a knight must move across the entire board, visiting each square exactly once, using object - oriented principles
In Java, create the knights tour project, a knight must move across the entire board, visiting each square exactly once, using objectoriented principles to represent the chessboard, knight, and potential moves as classes and objects, often utilizing a backtracking algorithm to find a valid sequence of moves. This code should have separate classes, BasicKnight and BasicKnightRunner. BasicKnight should be the main code where BasicKnightRunner should be the code where it will keep track of how many cells are accessible from each cell, should have nothing more than instantiating objects and a run tour method being called. The BasicKnight code should look like the following example and use the same method and object names as it Please use comments to explain each part of the code and what it does.
Example Code:
import java.util.ArrayList;
import java.util.Arrays;
public class BasicKnight
private int board;
private final int NUMROWS ;
private final int NUMCOLS ;
private int moveNum ;
private final int HMOVE ;
private final int VMOVE ;
private int knightRow;
private int knightCol;
public BasicKnightint row, int col
board new intNUMROWSNUMCOLS;
knightCol col;
knightRow row;
moveNum ;
boardknightRowknightCol moveNum;
public boolean makeMoveint move
ifmove we can make a move
make offsets and pick new positoin
int moveRow knightRow VMOVEmove;
int moveCol knightCol HMOVEmove;
update knight's position
knightRow moveRow;
knightCol moveCol;
update board
moveNum;
boardknightRowknightCol moveNum;
return true;
return false;
public boolean runRandomTour
boolean moveMade true;
whilemoveMade && moveNum
int move pickRandomMove;
ifmove
makeMovemove;
else
moveMade false;
return moveMade;
public int pickRandomMove
ArrayList moves getAvailableMoves;
ifmovessize
int rand intMathrandom moves.size;
return moves.getrand;
return ;
public ArrayList getAvailableMoves
ArrayList moves new ArrayList;
forint i ; i HMOVE.length; i
int hOffset HMOVEi;
int vOffset VMOVEi;
int tempH knightCol hOffset;col
int tempV knightRow vOffset;row
ifvalidCellboard tempV, tempH
moves.addi;
return moves;
public boolean validCellint arr, int row, int col
boolean topCheck row ;
boolean bottomCheck row arr.length;
boolean leftCheck col ;
return topCheck && bottomCheck && leftCheck && col arrrowlength
&& arrrowcol;
public void resetBoard
forint i ; i board.length; i
forint j ; j boardilength; j
boardij;
public void printBoard
forint arr: board
System.out.printlnArraystoStringarr;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
