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 object-oriented 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 2 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 NUM_ROWS =8;
private final int NUM_COLS =8;
private int moveNum =0;
private final int[] H_MOVE ={2,1,-1,-2,-2,-1,1,2};
private final int[] V_MOVE ={-1,-2,-2,-1,1,2,2,1};
private int knightRow;
private int knightCol;
public BasicKnight(int row, int col){
board = new int[NUM_ROWS][NUM_COLS];
knightCol = col;
knightRow = row;
moveNum =1;
board[knightRow][knightCol]= moveNum;
}
public boolean makeMove(int move){
if(move >=0){//we can make a move
//make offsets and pick new positoin
int moveRow = knightRow + V_MOVE[move];
int moveCol = knightCol + H_MOVE[move];
//update knight's position
knightRow = moveRow;
knightCol = moveCol;
//update board
moveNum++;
board[knightRow][knightCol]= moveNum;
return true;
}
return false;
}
public boolean runRandomTour(){
boolean moveMade = true;
while(moveMade && moveNum <65){
int move = pickRandomMove();
if(move >=0){
makeMove(move);
}else {
moveMade = false;
}
}
return moveMade;
}
public int pickRandomMove(){
ArrayList moves = getAvailableMoves();
if(moves.size()>0){
int rand =(int)(Math.random()* moves.size());
return moves.get(rand);
}
return -1;
}
public ArrayList getAvailableMoves(){
ArrayList moves = new ArrayList<>();
for(int i =0; i < H_MOVE.length; i++){
int hOffset = H_MOVE[i];
int vOffset = V_MOVE[i];
int tempH = knightCol + hOffset;//col
int tempV = knightRow + vOffset;//row
if(validCell(board, tempV, tempH)){
moves.add(i);
}
}
return moves;
}
public boolean validCell(int[][] arr, int row, int col){
boolean topCheck = row >=0;
boolean bottomCheck = row < arr.length;
boolean leftCheck = col >=0;
return topCheck && bottomCheck && leftCheck && col < arr[row].length
&& arr[row][col]==0;
}
public void resetBoard(){
for(int i =0; i < board.length; i++){
for(int j =0; j < board[i].length; j++){
board[i][j]=0;
}
}
}
public void printBoard(){
for(int[] arr: board){
System.out.println(Arrays.toString(arr));
}
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!