I have been given the task of printing a chessboard I have constructed to the console. The board must be bordered with 'a' to 'h' across the top and 1-8 vertically down the left. Each initial should line up with a letter and a number so for example, a black rook ("bR") should be placed at a 8 (and should be initialized as [0][0] in the array). Excluding the obvious problems with inefficiency (should use a char array for the top char labels) why does the nested for loop in the printBoard method fail to print the initials to the console in the correct places?
public class Virtual_Chessboard {
public enum Chessmen{
WHITE_KING,
WHITE_QUEEN,
WHITE_ROOK,
WHITE_BISHOP,
WHITE_KNIGHT,
WHITE_PAWN,
BLACK_KING,
BLACK_QUEEN,
BLACK_ROOK,
BLACK_BISHOP,
BLACK_KNIGHT,
BLACK_PAWN,
EMPTY
}
public static Chessmen[][] chessboard = new Chessmen[8][8];
public static void createBoard(){
int rows = 8;
int columns = 8;
for(int y=0;y<rows;y++){
for(int x=0;x<columns;x++){
if((y==0 && x==0) || (x==7 && y==0) ){
chessboard[x][y] = Chessmen.BLACK_ROOK;
}
else if((x==1 && y==0)||(x==6 && y==0)){
chessboard[x][y] = Chessmen.BLACK_KNIGHT;
}
else if((x==2 && y==0)||(x==5 && y==0)){
chessboard[x][y] = Chessmen.BLACK_BISHOP;
}
else if(x==3 && y==0){
chessboard[x][y] = Chessmen.BLACK_KING;
}
else if(x==4 && y==0){
chessboard[x][y] = Chessmen.BLACK_QUEEN;
}
else if((x==0 && y==7)||(x==7 && y==7)){
chessboard[x][y]= Chessmen.WHITE_ROOK;
}
else if((x==1 && y==7)||(x==6 && y==7)){
chessboard[x][y] = Chessmen.WHITE_KNIGHT;
}
else if((x==2 && y==7)||(x==5 && y==7)){
chessboard[x][y] = Chessmen.WHITE_BISHOP;
}
else if(x==3 && y==7){
chessboard[x][y] = Chessmen.WHITE_KING;
}
else if(x==4 && y==7){
chessboard[x][y] = Chessmen.WHITE_QUEEN;
}
else if(y==1){
chessboard[x][y] = Chessmen.BLACK_PAWN;
}
else if(y==6){
chessboard[x][y] = Chessmen.WHITE_PAWN;
}
else{
chessboard[x][y] = Chessmen.EMPTY;
}
}
}
}
public static void printBoard(Chessmen[][] chessboard){
int k = 8;
System.out.print("\t"+"a" + "\t" + "b" + "\t" + "c" + "\t" + "d" + "\t" + "e"+ "\t" + "f" + "\t" + "g" + "\t" + "h" + "\n" + k);
for(int i = 0; i<8;i++){
for(int j = 0;j<1;j++){
System.out.print("\t");
switch(chessboard[i][j]){
case WHITE_KING : System.out.print("WK");
case WHITE_QUEEN : System.out.print("wQ");
case WHITE_ROOK : System.out.print("wR");
case WHITE_BISHOP : System.out.print("wB");
case WHITE_KNIGHT : System.out.print("wK");
case WHITE_PAWN : System.out.print("wP");
case BLACK_KING : System.out.print("BK");
case BLACK_QUEEN : System.out.print("bQ");
case BLACK_ROOK : System.out.print("bR");
case BLACK_BISHOP : System.out.print("bB");
case BLACK_KNIGHT : System.out.print("bK");
case BLACK_PAWN : System.out.print("bP");
case EMPTY : System.out.print("\t");
} //switch
} /// j for
k--;
System.out.println("\n"+k);
} //i for
}
public static void main(String[] args) {
createBoard();
printBoard(chessboard);
}
}