Here is the entire structure of the program ,it is written in java
Main class -stats the program by calling Knight
public class Main {
public static void main(String[] args) {
Knight knight=new Knight( 33,6);
knight.controller(false);
// knight.possiblemoves();
}
}
Then knight class controls knight moves and initiates other method and classes
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashSet;
import java.util.Random;
import java.util.Set;
public class Knight {
Board boarde;
static ArrayList<Integer> possiblity;
static ArrayList<Integer> boarder;
public static int MaxCOl;
public static int MaxRow;
static int position;
int h;
guiframe frame;
backtracklogic logic;
int fposition;
Knight(int fposition, int size) {
MaxCOl = MaxRow = size;
this.fposition = fposition;
logic = new backtracklogic(fposition);
frame = new guiframe(MaxCOl, fposition);
boarde = new Board(MaxCOl, MaxRow);
boarder = new ArrayList<>();
for (int i = 0; i < MaxCOl; i++) {
for (int j = 0; j < MaxRow; j++) {
boarder.add(Board.board[i][j]);
}
}
possiblity = new ArrayList<>();
Knight.position = fposition;
// Board.used.add(fposition);
knightmove(fposition);
}
public void knightmove(int tomove) {
position = tomove;
Board.used.add(tomove);
frame.knightposition(tomove);
}
public void controller(Boolean random) {
boolean n = true;
if (random) {
while (n) {
for (int a = 0; a < MaxCOl * MaxCOl; a++) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
possiblemoves(position, Board.used);
if (possiblity.size() < 1)
break;
knightmove(randommove());
h = a;
if (h >= MaxCOl * MaxCOl) {
n = false;
}
}
}
} else if (!random)
{
ArrayList<Integer> used = new ArrayList<>();
used.add(fposition);
long startTime = new Date().getTime();
logic.move(fposition, new ArrayList<>(used), new ArrayList<Integer>(), 1);
knightmover(logic.toplay);
long endTime = new Date().getTime();
long executionTime = endTime - startTime;
System.out.println("Execution time: " + executionTime + " milliseconds");
}
}
public void knightmover(ArrayList<Integer> list) {
for (int i : list)
{
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
knightmove(i);
System.out.println(i + " " + list.indexOf(i));
}
System.out.println(list);
}
public static ArrayList<Integer> possiblemoves(int pos, ArrayList<Integer> used) {
possiblity.clear();
int x[] = { 2, 1, -1, -2, -2, -1, 1, 2 };
int y[] = { 1, 2, 2, 1, -1, -2, -2, -1 };
for (int j = 0, k = 0; j < x.length || k < y.length; j++, k++) {
int a = x[j];
int d = y[k];
if (check(pos + (a * 10) + d) == 1 && !used.contains(pos + (a * 10) + d))
possiblity.add(pos + (a * 10) + d);
}
// remove duplicate fix it in the future if possible
Set<Integer> p = new LinkedHashSet<>(possiblity);
possiblity.addAll(p);
return possiblity;
}
public static int check(int a) {
if (boarder.contains(a)) {
return 1;
} else
return 0;
}
public int randommove() {
Random random = new Random();
int a = random.nextInt(possiblity.size());
return possiblity.get(a);
}
}
then the backtract logic class
import java.util.ArrayList;
public class backtracklogic {
ArrayList<Integer> toplay;
backtracklogic(int fposition) {
toplay = new ArrayList<>();
}
public void move(int position, ArrayList<Integer> used, ArrayList<Integer> movetoplay, int depth) {
if (depth == Knight.MaxCOl * Knight.MaxCOl) {
toplay = movetoplay;
return;
}
// System.out.println(depth);
int move;
ArrayList<Integer> possible = new ArrayList<>(Knight.possiblemoves(position, used));
if (possible.size() <= 0 && depth != Knight.MaxCOl * Knight.MaxCOl) {
return;
} else if (depth == Knight.MaxCOl * Knight.MaxCOl) {
return;
}
for (int i = 0; i < possible.size(); i++) {
move = possible.get(i);
used.add(move);
movetoplay.add(move);
move(move, new ArrayList<>(used), new ArrayList<>(movetoplay), depth + 1);
movetoplay.removeLast();
used.removeLast();
}
}
}
then the class which creates the board
import java.util.ArrayList;
public class Board {
public static int Max_row;
public static int Max_col;
public static int[][] board;
static ArrayList<Integer> used;
Board(int maxcol,int maxrow){
Max_col=maxcol;
Max_row=maxrow;
board=new int[Max_row][Max_col];
used=new ArrayList<>();
for(int y =1;y<=Max_row;y++){
int f=1;
for(int x=Max_col;x>0;x--)
{
board[x-1][y-1]=y*10+f;
f++;
}
}
for(int i = 0;i<Max_col;i++)
{
for (int j = 0; j < Max_col; j++) {
System.out.print(board[i][j] + " ");
}
System.out.println();
}
}}
Sorry i am new to programming and i just cant find a solution to this upto 5 X5 it takes 4 sec after that it just doesnt give a solution
i tried to change the way i intitiate the board from a4 ,a5 notation to standard cordinat sstem 11 12 but it didnt do any good
Sourav Tripathy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.