Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: A chessboard with loops!

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation A chessboard with loops!

    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-

    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);
     
    	}
    }
    OUTPUT:
    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


  2. #2
    Member
    Join Date
    Oct 2011
    Posts
    36
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: A chessboard with loops!

    I think you need to print chessmen in new line after the inner for loop is finished each time. So one solution might be

    ...
    default:
    break;
    }
    }
    System.out.println(" "); // to change to a new line
    }
    }

Similar Threads

  1. For loops
    By JCTexas in forum Loops & Control Statements
    Replies: 4
    Last Post: September 21st, 2011, 05:43 PM
  2. help with loops
    By kidza12 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 26th, 2011, 11:42 PM
  3. help with loops... :(
    By Macgrubber in forum Loops & Control Statements
    Replies: 2
    Last Post: November 2nd, 2010, 12:38 PM
  4. Chessboard filling issue
    By olemagro in forum AWT / Java Swing
    Replies: 7
    Last Post: January 23rd, 2010, 07:07 PM
  5. need help with loops plz
    By Kilowog in forum Loops & Control Statements
    Replies: 4
    Last Post: September 28th, 2009, 08:11 AM