1

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

    }

}
ug_
  • 11,267
  • 2
  • 35
  • 52
jacka92
  • 55
  • 1
  • 1
  • 7
  • 6
    Because a `case` should always end with a `break` to prevent stepping into the next `case`. – hoefling Sep 29 '14 at 15:39
  • 2
    You need to `break` the individual cases – nem035 Sep 29 '14 at 15:39
  • In addition, you seem to have made some assumptions about the width of a `\t`. If you are lucky, this might work for your current terminal but if you want portability, use the correct number of spaces. – 5gon12eder Sep 29 '14 at 15:40
  • And a stylistic note: Your code is not very readable. Why the if-cascase in the nested `for` loop? You can just initialize the whole board to empty and then explicitly set the fields that should not be empty. The `switch` could also be eliminated completely if you add a `getTwoCharacterRepresentation` method to the `enum`. – 5gon12eder Sep 29 '14 at 15:44
  • Just answered one hour ago to the same case. http://stackoverflow.com/questions/26101144/switch-statement-not-going-through-all-cases/26101154#26101154 – Alboz Sep 29 '14 at 15:45

2 Answers2

3

You have to terminate every case statement with a break to continue execution after the end of the switch block. Otherwise you fall through into the next case statement.

Simon Fischer
  • 1,154
  • 6
  • 22
0

Your EMPTY case is printing "\t". This is going to shift your columns off. None of the other piece types printt the \t, and you print it anyway in your loop. Therefore, it should be just print("");

ControlAltDel
  • 33,923
  • 10
  • 53
  • 80