I\'m having some trouble printing out a knights move chessboard this is i have s
ID: 3736152 • Letter: I
Question
I'm having some trouble printing out a knights move chessboard this is i have so far
public class Lab9 {
int[] X ={-1,-1,1,1,2,2,-2,-2};
int[] Y ={-2,2,-2,2,-1,1,-1,1};
public int[] position(int x){
int [] sq = new int[2];
x--;
sq[0]=x/3;
sq[1]=x%3;
return sq;
}
public boolean playablemove(int x, int y){
if(x>=0 && x<=2 && y>=0 && y>=2){
return true;
}
else {
return false;
}
}
public int convert(int x, int y){
return x*3 + y+1;
}
public ArrayList<Integer> path(int x, int y,ArrayList<Integer>trackedmoves ,boolean[] moves){
moves[x-1]=true;
if(x==y){
return trackedmoves;
}
else {
int[] sq = position(x);
for(int i=0;i<8;i++) {
if(playablemove(sq[0] + X[i],sq[1] + Y[i])){
int num = convert(sq[0] + X[i],sq[1] + Y[i]);
if(!moves[num - 1]) {
moves[num - 1] = true;
ArrayList<Integer> List2 = new ArrayList<>(trackedmoves);
List2.add(x);
List2 = path(num,y,List2,moves);
if(List2.size()!=0)
return List2;
moves[num - 1] = false;
}
}
}
return new ArrayList<>();
}
}
public void board(int num){
for (int count = 1; count<=9;count++){
if (count==num){
System.out.println("K");
}
else{
System.out.println(count+" ");
}
if(count%3==0){
System.out.println();
}
System.out.println();
}
}
public void displayPath(ArrayList<Integer>trackedmoves,int endmove) {
trackedmoves.stream().forEach(i->board(i));
board(endmove);
}
public static void main(String[] args) {
Lab9 knightmoves = new Lab9();
boolean[] moves = new boolean[9];
int startmove=2;
int endmove=1;
ArrayList<Integer>trackedmoves=knightmoves.path(startmove, endmove, new ArrayList<>(),moves);
knightmoves.displayPath(trackedmoves, endmove);
}
}
Explanation / Answer
Please find the modified code below.
CODE
======================
import java.util.ArrayList;
public class Lab9 {
int[] X ={-1,-1,1,1,2,2,-2,-2};
int[] Y ={-2,2,-2,2,-1,1,-1,1};
public int[] position(int x){
int [] sq = new int[2];
x--;
sq[0]=x/3;
sq[1]=x%3;
return sq;
}
public boolean playablemove(int x, int y){
if(x>=0 && x<=2 && y>=0 && y>=2){
return true;
}
else {
return false;
}
}
public int convert(int x, int y){
return x*3 + y+1;
}
public ArrayList<Integer> path(int x, int y,ArrayList<Integer>trackedmoves ,boolean[] moves){
moves[x-1]=true;
if(x==y){
return trackedmoves;
}
else {
int[] sq = position(x);
for(int i=0;i<8;i++) {
if(playablemove(sq[0] + X[i],sq[1] + Y[i])){
int num = convert(sq[0] + X[i],sq[1] + Y[i]);
if(!moves[num - 1]) {
moves[num - 1] = true;
ArrayList<Integer> List2 = new ArrayList<>(trackedmoves);
List2.add(x);
List2.addAll(path(num,y,List2,moves));
if(List2.size()!=0)
return List2;
moves[num - 1] = false;
}
}
}
return new ArrayList<>();
}
}
public void board(int num){
for (int count = 1; count<=9;count++){
if (count==num){
System.out.println("K");
}
else{
System.out.println(count+" ");
}
if(count%3==0){
System.out.println();
}
System.out.println();
}
}
public void displayPath(ArrayList<Integer>trackedmoves,int endmove) {
trackedmoves.stream().forEach(i->board(i));
board(endmove);
}
public static void main(String[] args) {
Lab9 knightmoves = new Lab9();
boolean[] moves = new boolean[9];
int startmove=2;
int endmove=1;
ArrayList<Integer>trackedmoves=knightmoves.path(startmove, endmove, new ArrayList<>(),moves);
knightmoves.displayPath(trackedmoves, endmove);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.