how can I fix this code so it print the movement. package simplified.single.play
ID: 3688577 • Letter: H
Question
how can I fix this code so it print the movement. package simplified.single.player.chess.game; import java.util.Scanner; /** * * @author cubanitos */ public class SimplifiedSinglePlayerChessGame { /** * @param args the command line arguments */ public static void main(String[] args) { Scanner input = new Scanner(System.in); String [][] chess = new String [8][8]; String [] backPieces = {"rook", "knight","bishop", "king","queen","bishop","knight","rook"}; int [] frontPieces = {0,1,2,3,4,5,6,7}; for(int a = 0; a < 8; a++) { for (int b = 0; b < 8; b++) { if (a > 1 & a < 6) { chess [a][b] = "empty";} if (a == 0) { chess [a][b] = "white " + backPieces [b]; } if (a == 7) { chess [a][b]= "black " + backPieces [b]; } if (a == 1){ chess [a][b] = "white pawn"; } if (a == 6){ chess [a][b] = "black pawn"; } } } //Prompt the user to choose a color. ' int userResponse; System.out.print("Choose the color black(1) or white(2) with which you want to play"); userResponse = input.nextInt(); //Display the status of the chess board and the color that the user is currently playing. System.out.println("The game is ready to be play."); if(userResponse == 1){ System.out.println("You are playing with color black pieces."); } else { System.out.println("You are playing with white pieces."); } // This step prompt the user to make a move. //In this section the user specifies the current location of the piece and the desire destination of the piece. System.out.print("Choose the current location of the piece index(b),row index(a): "); int column = input.nextInt(); int row = input.nextInt(); System.out.print("Choose the desire destination of piece column index(b), row index(a): "); int desiredColumn = input.nextInt(); int desireRow = input.nextInt(); //Check for valid indexes. if(column >= 0 & column < 8){ if(row >= 0 & row < 8) if(desiredColumn >= 0 & desiredColumn < 8) if(desireRow >= 0 & desireRow <8) System.out.println("The location and the desired destination are valid"); } else{ System.out.println("The location and the desired destination are not valid"); } //Check wheter there is a piece at the specified current location. // if(column < 2 || column > 5){ if(row < 2 || column > 5) System.out.println("The current location is empty, you can move your piece there."); } else{ System.out.println("The current location is being occupied by a piece, choose a different square"); } if(desiredColumn > 1 & desiredColumn <6){ if(desireRow > 1 & desireRow <6) System.out.println("The desired destination is empty"); } else{ System.out.println("The desired destination is occupied, pick a different destination square "); } //Switch the color of the user.For example if the user had chosen Black first then now the user will make amove for White. System.out.print("Choose the color black(1) or white(2): "); userResponse = input.nextInt(); if(userResponse == 1){ System.out.println(userResponse + "black"); } else{ System.out.println(userResponse + "white"); } // Ask teh user if he or she want to continue playing the game. System.out.println("Do you want to continue playing chess, select yes(Y) or no(N): "); char Y; char N; char checkResponse = input.next().charAt(0); if(checkResponse == 'Y'){ System.out.print("Choose the current location of the piece index(b),row index(a): "); } else{ System.exit(0); } } }
Explanation / Answer
//package simplified.single.player.chess.game;
import java.util.Scanner;
/** * * @author cubanitos */
public class SimplifiedSinglePlayerChessGame
{ /** * @param args the command line arguments */
public static void main(String[] args)
{ Scanner input = new Scanner(System.in);
String [][] chess = new String [8][8];
String [] backPieces = {"rook", "knight","bishop", "king","queen","bishop","knight","rook"};
int [] frontPieces = {0,1,2,3,4,5,6,7};
for(int a = 0; a < 8; a++)
{ for (int b = 0; b < 8; b++)
{ if (a > 1 & a < 6)
{ chess [a][b] = "empty";
}
if (a == 0)
{ chess [a][b] = "white " + backPieces [b];
}
if (a == 7)
{ chess [a][b]= "black " + backPieces [b];
}
if (a == 1)
{ chess [a][b] = "white pawn";
}
if (a == 6)
{ chess [a][b] = "black pawn";
}
}
} //Prompt the user to choose a color. '
int userResponse;
System.out.print("Choose the color black(1) or white(2) with which you want to play");
userResponse = input.nextInt(); //Display the status of the chess board and the color that the user is currently playing.
System.out.println("The game is ready to be play.");
if(userResponse == 1)
{ System.out.println("You are playing with color black pieces.");
}
else
{ System.out.println("You are playing with white pieces.");
} // This step prompt the user to make a move.
//In this section the user specifies the current location of the piece and the desire destination of the piece.
System.out.print("Choose the current location of the piece index(b),row index(a): ");
int column = input.nextInt();
int row = input.nextInt();
System.out.print("Choose the desire destination of piece column index(b), row index(a): ");
int desiredColumn = input.nextInt();
int desireRow = input.nextInt();
//Check for valid indexes.
if(column >= 0 & column < 8)
{ if(row >= 0 & row < 8)
if(desiredColumn >= 0 & desiredColumn < 8)
if(desireRow >= 0 & desireRow <8)
System.out.println("The location and the desired destination are valid");
}
else
{ System.out.println("The location and the desired destination are not valid");
} //Check wheter there is a piece at the specified current location. //
if(column < 2 || column > 5)
{ if(row < 2 || column > 5)
System.out.println("The current location is empty, you can move your piece there.");
} else
{ System.out.println("The current location is being occupied by a piece, choose a different square"); } if(desiredColumn > 1 & desiredColumn <6){ if(desireRow > 1 & desireRow <6) System.out.println("The desired destination is empty");
} else
{ System.out.println("The desired destination is occupied, pick a different destination square ");
} //Switch the color of the user.For example if the user had chosen Black first then now the user will make amove for White.
System.out.print("Choose the color black(1) or white(2): ");
userResponse = input.nextInt();
if(userResponse == 1)
{ System.out.println(userResponse + "black");
} else
{ System.out.println(userResponse + "white");
} // Ask teh user if he or she want to continue playing the game.
System.out.println("Do you want to continue playing chess, select yes(Y) or no(N): ");
char Y;
char N;
char checkResponse = input.next().charAt(0);
if(checkResponse == 'Y')
{ System.out.print("Choose the current location of the piece index(b),row index(a): ");
} else{ System.exit(0);
}
}
}
The code compiled successfully .
the problem is you made some useful parts of code as comments.
output:
The game is ready to be play.
You are playing with color black pieces.
Choose the current location of the piece index(b),row index(a): 1
1
Choose the desire destination of piece column index(b), row index(a): 2
2
The location and the desired destination are valid
The current location is empty, you can move your piece there.
The desired destination is empty
Choose the color black(1) or white(2): 1
1black
Do you want to continue playing chess, select yes(Y) or no(N):
n
sh-4.3$
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.