0

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). When my inner nested for loop in my printBoard method is limited to j<1 the first line of pieces are printed in the correct place, but when widening the limit to j<8 to print the whole board, pieces are printed in incorrect places and I'm not sure why. (I have acknowledged problems with readability and lacking efficiency in the code e.g. not simply initialising board, dispensing with lengthy if/else block and giving enums a toString method to shorten the switch statement).

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<8;j++){
                System.out.print("\t");
                switch(chessboard[i][j]){
                case WHITE_KING : System.out.print("WK");
                break;
                case WHITE_QUEEN : System.out.print("wQ");
                break;
                case WHITE_ROOK : System.out.print("wR");
                break;
                case WHITE_BISHOP : System.out.print("wB");
                break;
                case WHITE_KNIGHT : System.out.print("wK");
                break;
                case WHITE_PAWN : System.out.print("wP");
                break;
                case BLACK_KING : System.out.print("BK");
                break;
                case BLACK_QUEEN : System.out.print("bQ");
                break;
                case BLACK_ROOK : System.out.print("bR");
                break;
                case BLACK_BISHOP : System.out.print("bB");
                break;
                case BLACK_KNIGHT : System.out.print("bK");
                break;
                case BLACK_PAWN : System.out.print("bP");
                break;

                } //switch

            } /// j for 


        } //i for
        k--;
        System.out.println("\n" + "\n"+k);
    }

    public static void main(String[] args) {


        createBoard();
        printBoard(chessboard);

    }

}
Fabio Filippi
  • 1,732
  • 25
  • 40
jacka92
  • 55
  • 1
  • 1
  • 7

2 Answers2

0

You do not have a case for EMPTY nor a default in your switch statement; when j<1 this case will not occur. Otherwise your output will be garbled.

Uwe Allner
  • 3,399
  • 9
  • 35
  • 49
0
  1. Right now you iterate through the rows of a column, but you want to iterate over the columns of a row. Swap i and j in the array.

  2. You want a linebreak after the inner for-Loop, move

    k--;

    System.out.print("\n" + "\n"+k);

    into the outer for-Loop

This should already solve your problems.

Hal
  • 1