Hi, ive tried to make a chessboard with loops, specifically 2D arrays, however the output is 1 dimensional and im not sure what is wrong...
Any advice and help is much appreciated (P.S. im new to programming!) Also, how would i get numbering around the edges, 1-8 for the squares?
Code: in Java-
OUTPUT:package chess; public class ex5 { 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 void printBoard (Chessmen [][] chessboard){ for (int i=0; i<chessboard.length;i++){ for (int j = 0; j<chessboard.length;j++){ switch (chessboard [i][j]){ case WHITE_KING: System.out.print("WK\t"); break; case WHITE_QUEEN: System.out.print("WQ\t"); break; case WHITE_ROOK: System.out.print("WR\t"); break; case WHITE_BISHOP: System.out.print("WB\t"); break; case WHITE_KNIGHT: System.out.print("WKn\t"); break; case WHITE_PAWN: System.out.print("WP\t"); break; case BLACK_KING: System.out.print("BK\t"); break; case BLACK_QUEEN: System.out.print("BQ\t"); break; case BLACK_ROOK: System.out.print("BR\t"); break; case BLACK_BISHOP: System.out.print("BB\t"); break; case BLACK_KNIGHT: System.out.print("BKn\t"); break; case BLACK_PAWN: System.out.print("BP\t"); break; default: break; } } } } public static void main(String[] args) { int Rows = 8; int Columns = 8; Chessmen [][] chessboard = new Chessmen [Rows][Columns]; for (int i=0;i<chessboard.length; i++){ for (int j=0;j<chessboard[0].length;j++){ if (i==1){ chessboard [i][j]= Chessmen.WHITE_PAWN; }else if (i==6){ chessboard [i][j]= Chessmen.BLACK_PAWN; }else if ((i==0&&j==7)||(i==0&&j==0)){ chessboard [i][j]= Chessmen.WHITE_ROOK; }else if ((i==0&&j==1)||(i==0&&j==6)){ chessboard [i][j] = Chessmen.WHITE_KNIGHT; }else if ((i==0&&j==2)||(i==0&&j==5)){ chessboard [i][j] = Chessmen.WHITE_BISHOP; }else if (i==0&&j==3){ chessboard [i][j] = Chessmen.WHITE_KING; }else if (i==0&&j==4){ chessboard [i][j] = Chessmen.WHITE_QUEEN; }else if ((i==7&&j==0)||(i==7&&j==7)){ chessboard [i][j]= Chessmen.BLACK_ROOK; }else if ((i==7&&j==1)||(i==7&&j==6)){ chessboard [i][j] = Chessmen.BLACK_KNIGHT; }else if ((i==7&&j==2)||(i==7&&j==5)){ chessboard [i][j] = Chessmen.BLACK_BISHOP; }else if (i==7&&j==3){ chessboard [i][j] = Chessmen.BLACK_KING; }else if (i==7&&j==4){ chessboard [i][j] = Chessmen.BLACK_QUEEN; }else { chessboard [i][j]= Chessmen.EMPTY; } } } printBoard (chessboard); } }
WR WKn WB WK WQ WB WKn WR WP WP WP WP WP WP WP WP BP BP BP BP BP BP BP BP BR BKn BB BK BQ BB BKn BR