Question: Checker java need help debugging or understanding my issue import java.util.Scanner; public class Checkers { static char blackPieces = ' b ' ; static char

Checker java need help debugging or understanding my issue
import java.util.Scanner;
public class Checkers {
static char blackPieces ='b';
static char whitePieces ='w';
static char empty ='';
static char blackKing ='B';
static char whiteKing ='W';
static String exitCommand = "exit";
static String viewCommand = "view";
private static char[][] board = new char[8][8];
static Scanner scanner = new Scanner(System.in);
private static void initialiseBoard(){
for (int i =0; i board.length; i++){
for (int j =0; j board[i].length; j++){
if ((i + j)%2==1){
if (i 3){
board[i][j]= whitePieces;
} else if (i >4){
board[i][j]= blackPieces;
} else {
board[i][j]= empty;
}
} else {
board[i][j]='';
}
}
}
}
private static void displayBoard(){
for (int i =0; i board.length; i++){
System.out.print("|");
for (int j =0; j board[i].length; j++){
System.out.print(board[i][j]+"|");
}
System.out.println();
}
System.out.println();
System.out.println();
}
private static void startGame(){
int playerTurn =1;
while (!isGameOver()){
if (scanner.hasNextLine()){
String move = scanner.nextLine().trim().toLowerCase();
if (move.equalsIgnoreCase(exitCommand)){
return;
} else if (move.equalsIgnoreCase(viewCommand)){
displayBoard();
} else if (processMove(move)){
playerTurn =(playerTurn ==1)?2 : 1; // Switch player turn
} else {
System.out.println("Error!");
}
}
}
}
private static boolean processMove(String move){
if (move.equalsIgnoreCase(viewCommand)){
displayBoard();
return false; // No error for view command
}
String[] positions = move.split(" to");
if (positions.length !=2){
System.out.println("Invalid move format.");
return false;
}
String fromPosition = positions[0].trim();
String toPosition = positions[1].trim();
int fromRow = parseCoordinateRow(fromPosition);
int fromCol = parseCoordinateCol(fromPosition);
int toRow = parseCoordinateRow(toPosition);
int toCol = parseCoordinateCol(toPosition);
if (!isValidMove(fromRow, fromCol, toRow, toCol)){
System.out.println("Invalid move. Please try again.");
return false;
}
board[toRow][toCol]= board[fromRow][fromCol];
board[fromRow][fromCol]= empty;
displayBoard();
return true;
}
private static int parseCoordinateRow(String coordinate){
return 8- Character.getNumericValue(coordinate.charAt(1));
}
private static int parseCoordinateCol(String coordinate){
return coordinate.toLowerCase().charAt(0)-'a';
}
private static boolean isValidMove(int fromRow, int fromCol, int toRow, int toCol){
if (fromRow 0|| fromRow >= board.length || fromCol 0|| fromCol >= board[0].length ||
toRow 0|| toRow >= board.length || toCol 0|| toCol >= board[0].length){
return false;
}
if (board[toRow][toCol]!= empty){
return false;
}
int rowDifference = Math.abs(toRow - fromRow);
int colDifference = Math.abs(toCol - fromCol);
if (rowDifference != colDifference){
return false;
}
int rowChange = toRow - fromRow;
if (rowChange =0){
return false;
}
return true;
}
private static boolean isGameOver(){
boolean blackPiecesExist = false;
boolean whitePiecesExist = false;
for (int i =0; i board.length; i++){
for (int j =0; j board[i].length; j++){
if (board[i][j]== blackKing || board[i][j]== blackPieces){
blackPiecesExist = true;
} else if (board[i][j]== whiteKing || board[i][j]== whitePieces){
whitePiecesExist = true;
}
}
}
if (!blackPiecesExist){
System.out.println("Player 2 won");
return true;
} else if (!whitePiecesExist){
System.out.println("Player 1 won");
return true;
}
return false;
}
public static void main(String[] args){
initialiseBoard();
displayBoard();
startGame();
}
}current right board output is;
 Checker java need help debugging or understanding my issue import java.util.Scanner;

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 Databases Questions!