Complete Chessboard Save Submit + Continue RUN CODE TEST CASES ASSIGNMENT DOCS GRADE | MORE 1 public class ChessBoard 2 { 3 public static void main(String[] args) 4 { 5 //Create an 8×8 2D String array called chess.

8.1.6: Complete Chessboard Save Submit + Continue RUN CODE TEST CASES ASSIGNMENT DOCS GRADE | MORE 1 public class ChessBoard 2 { 3 public static void main(String[] args) 4 { 5 //Create an 8×8 2D String array called chess. 6 7 8 //Use this method to print the chess board onto the 9 print(chess); 10 11 12 } 13 14 //Do not make alterations to this method! 15 public static void print(String[][] array) 16 { 17 for(String[] row: array) 18 { 19 for(String thing: row) 20 { 21 System.out.print(thing + “\t”); 22 } 23 System.out.println(); 24 } 25 } 26 } 27 5 points Status: Not Submitted In this exercise, you will create a full 8×8 chess board. First, initialize an 8×8 2D String array. Then, populate the first and last array in the 2D array with the correct chess pieces. The rest of the arrays should be fille with Strings with the value “-“. When printed using the provided method, the result should look like: Rook Knight Bishop Queen King Bishop Knight Rook Pawn Pawn Pawn Pawn Pawn Pawn Pawn Pawn – – – – – – – – – – – – – – – – Pawn Pawn Pawn Pawn Pawn Pawn Pawn Pawn Rook Knight Bishop Queen King Bishop Knight Rook

The Correct Answer and Explanation is:

Here’s the correct implementation for your Java assignment:

public class ChessBoard
{
    public static void main(String[] args)
    {
        // Create an 8x8 2D String array called chess.
        String[][] chess = new String[8][8];

        // Set the first row (white major pieces)
        chess[0][0] = "Rook";
        chess[0][1] = "Knight";
        chess[0][2] = "Bishop";
        chess[0][3] = "Queen";
        chess[0][4] = "King";
        chess[0][5] = "Bishop";
        chess[0][6] = "Knight";
        chess[0][7] = "Rook";

        // Set the second row (white pawns)
        for(int i = 0; i < 8; i++) {
            chess[1][i] = "Pawn";
        }

        // Set middle rows to "-"
        for(int i = 2; i < 6; i++) {
            for(int j = 0; j < 8; j++) {
                chess[i][j] = "-";
            }
        }

        // Set the seventh row (black pawns)
        for(int i = 0; i < 8; i++) {
            chess[6][i] = "Pawn";
        }

        // Set the eighth row (black major pieces)
        chess[7][0] = "Rook";
        chess[7][1] = "Knight";
        chess[7][2] = "Bishop";
        chess[7][3] = "Queen";
        chess[7][4] = "King";
        chess[7][5] = "Bishop";
        chess[7][6] = "Knight";
        chess[7][7] = "Rook";

        // Print the chess board
        print(chess);
    }

    // Do not make alterations to this method!
    public static void print(String[][] array)
    {
        for(String[] row: array)
        {
            for(String thing: row)
            {
                System.out.print(thing + "\t");
            }
            System.out.println();
        }
    }
}

Explanation

This program constructs a complete 8×8 chessboard using a 2D array in Java. The 2D array chess represents the chessboard where each element stores a String indicating the piece or a dash ("-") for empty spaces.

The chessboard is initialized with:

  • The first row (index 0) representing white major pieces in their standard order: Rook, Knight, Bishop, Queen, King, Bishop, Knight, Rook.
  • The second row (index 1) is filled with Pawn representing white pawns.
  • The middle rows (indices 2 to 5) are filled with "-" to denote empty squares.
  • The seventh row (index 6) is filled with Pawn representing black pawns.
  • The eighth row (index 7) mirrors the first row for the black major pieces, also in the same order.

To build the board:

  • A loop fills the pawn rows (rows 1 and 6).
  • Another nested loop fills the middle rows with "-".
  • The corner rows (0 and 7) are manually filled with their specific pieces.

Finally, the print method is used to display the chessboard. It uses nested for-each loops to print every row and element, separating the pieces with tab characters for neat alignment.

This solution effectively models a chessboard for visual inspection or further programming logic and helps understand how 2D arrays can represent complex grids like game boards.

Scroll to Top