0

Total noob to java and I have a homework where I need to make a custom chess game but I am even struggling with printing the gameboard which I am trying all the empty spaces to be with "X". My current "code":

public class Chess {

    public static void main(String[] args) {
        String[][] board = new String[7][7];
        fillBoard(board);


    }
    public static void fillBoard(String[][] board){
        for (int i = 0; i < 7; i++) {
            for (int j = 0; j < 7; j++) {
                board[i][j] = "X";
                System.out.print(Arrays.deepToString(board));
            }
            System.out.println();
        }
    }
}

It prints out: This

When I want it to print out: Is this

knittl
  • 246,190
  • 53
  • 318
  • 364
  • 2
    Just do `System.out.print("X");` inside inner for loop. If you just want to print it, you don't need the array. – Nongthonbam Tonthoi May 01 '20 at 18:35
  • for now I want just to print it yes just so I can see the board is set up and ready to add things on to. But won't I need the array later when I try and add pawns? – NotJhonWick45 May 01 '20 at 18:39
  • A. Don’t print the entire array after each value you fill up – Roy Shahaf May 01 '20 at 18:45
  • Replace `System.out.print(Arrays.deepToString(board));` with `System.out.print("X");` – Arvind Kumar Avinash May 01 '20 at 18:46
  • @NotJhonWick45 Well I am not sure what you mean by adding pawns but yes you can fill the array with `X`'s – Nongthonbam Tonthoi May 01 '20 at 18:46
  • 1
    B. DeepToString will not give you the format you’re expecting (a new line between “rows”), to do that you can print the current value for each value you fill out, then print a column separator (space, comma, nothing). Between iterations of the outer loop you can print a new line (println) – Roy Shahaf May 01 '20 at 18:47
  • @NotJhonWick45 - If one of the answers resolved your issue, you can help the community by marking it as accepted. An accepted answer helps future visitors use the solution confidently. Check https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work to learn how to do it. – Arvind Kumar Avinash May 02 '20 at 16:42

2 Answers2

1

Just print the value of the position directly.

 public static void fillBoard(String[][] board){
            for (int i = 0; i < 7; i++) {
                for (int j = 0; j < 7; j++) {
                    board[i][j] = "X";
                    System.out.print(board[i][j]);
                }
                System.out.println();
            }
        }
Lenny
  • 350
  • 1
  • 13
1

After reading your comments, I realized that it's not just about printing the array that you are concerned about; rather, you are concerned about how you will be able to access it later. The solution to the problem of incorrect printing has already been provided in the comments i.e. replacing System.out.print(Arrays.deepToString(board)); with System.out.print("X");. I am explaining here the other part (i.e. how to access it later) and also some additional information that will help you in designing your application.

  1. When you pass and change an array in some method, the array will be changed for the calling method too (check this to learn more about it). It means that when board[][] will be changed in fillBoard, you will get the changed board[][] in main.
  2. According to Single-responsibility principle, the module/method, fillBoard should do only one thing, which is filling up the board. It should not also do printing (additional thing). You should write a different method for printing.
import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        String[][] board = new String[7][7];
        fillBoard(board);
        displayBoard(board);
    }

    static void fillBoard(String[][] board) {
        for (String[] row : board) {
            Arrays.fill(row, "X");
        }
    }

    static void displayBoard(String[][] board) {
        for (String[] row : board) {
            for (String cell : row) {
                System.out.print(cell);
            }
            System.out.println();
        }
    }
}

Output:

XXXXXXX
XXXXXXX
XXXXXXX
XXXXXXX
XXXXXXX
XXXXXXX
XXXXXXX

Additional note:

A 2-D array is an array of 1-D arrays and therefore there are many ways in which you can write the code given above e.g.

static void fillBoard(String[][] board) {
    for (int i = 0; i < board.length; i++) {
        Arrays.fill(board[i], "X");
    }
}

or using enhanced for loop:

static void fillBoard(String[][] board) {
    for (String[] row : board) {
        Arrays.fill(row, "X");
    }
}

or without using Arrays.fill:

static void fillBoard(String[][] board) {
    for (int i = 0; i < board.length; i++) {
        for (int j = 0; j < board[i].length; j++) {
            board[i][j] = "X";
        }
    }
}

Similarly, the method, displayBoard can be written as:

static void displayBoard(String[][] board) {
    for (int i = 0; i < board.length; i++) {
        for (int j = 0; j < board[i].length; j++) {
            System.out.print(board[i][j]);
        }
        System.out.println();
    }
}

Note that I have used board.length and board[i].length instead of 7 for the number of rows and 7 for the number of columns to make sure you don't have to change it every time you change the size of the array.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110