Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m having some trouble printing out a knights move chessboard printing as a ch

ID: 3736183 • Letter: I

Question

I'm having some trouble printing out a knights move chessboard printing as a chessboard this is i have so far

import java.util.*;

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);

   }

}

Explanation / Answer

Hello buddy! I am a bit confused about what you really want in the program.

Looks like you want to reach a destination point with the minimum amount of moves and also want to keep the track of the path associated with the knight.

Here is the code:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote