0

So I am trying to write a method that populate a board in a game. I am just beginning to learn about programming so I do not know much about special methods.

public static void Board(char[][] array){

  for(int i=0; i<2*array.length+1; i++) {
  if(i %2 ==0){
    for(int j=0; j<2*array.length+1;j++){
      if(j%2==0){
        System.out.print("+");
      }
      else{
        System.out.print("-");
      }
    }
  }
  else {
      for(int j=0; j<2*array.length+1;j++){
        if(j%2==0){
          System.out.print("|");
        }
        else{
          System.out.print(" "); // Maybe should use this spot to insert the x and o from the array.
        }
  }
    }
  System.out.println();

But in the board, where I currently have empty spaces, I want to actually be able to accept input.

For example , if

char [][] b={{'',' x',' ',},{' ','s','t '},{'o','o',' '}};
 Board(a);

Then instead of only having the empty spaces, I would like the element corresponding to the array to go there. I marked on my code where I think this might be done. I don't know of any special methods, but possibly DeepArray to string or such. I understand how to import using Arrays.ToString, the issue is with the index.

Andreas
  • 154,647
  • 11
  • 152
  • 247
Quality
  • 113
  • 6
  • If your goal is to provide custom formatting, please detail how you want the output to look. Otherwise it's really hard to guess from your code. – markspace Oct 31 '17 at 22:51
  • You're almost there. Replace `print(" ")` with `print(array[i/2][j/2])`. – Andreas Oct 31 '17 at 22:55
  • While I'm sure Andreas is correct, your code is completely insane at this point. Even though you might see twice as many characters on a line as array elements, the **index** of the array should never use `x<2*array.length+1`. Just use the normal number of elements in the array, *don't* divide by 2 as suggested, and your code will look a lot cleaner and be easier to understand (esp. for you). – markspace Oct 31 '17 at 23:01

1 Answers1

1

As mentioned in a comment, you're almost there:
Replace print(" ") with print(array[i/2][j/2]).

That will then make the code print correct output, after also fixing the errors in the two lines of code calling the method:

char[][] b={{' ','x',' '},
            {' ','s','t'},
            {'o','o',' '}};
Board(b);

Output

+-+-+-+
| |x| |
+-+-+-+
| |s|t|
+-+-+-+
|o|o| |
+-+-+-+

To followup on comment by @markspace, your code can be simplified like this:

public static void printBoard(char[][] board) {
    StringBuilder line = new StringBuilder();
    for (int i = 0; i < board[0].length; i++)
        line.append("+-");
    String rowSeparatorLine = line.append('+').toString();
    System.out.println(rowSeparatorLine);
    for (char[] row : board) {
        line.setLength(0); // clear
        for (char cell : row)
            line.append('|').append(cell);
        System.out.println(line.append('|').toString());
        System.out.println(rowSeparatorLine);
    }
}

The code uses a StringBuilder to build a whole line in memory before printing it, e.g. so it only has to build the row separator line once.

It also uses enhanced for loops to keep the code simpler.

I also fixed the method name. Java naming convention is for method names to start with lowercase letter, and should usually start with a verb, since the method is actually intended to do something.

Andreas
  • 154,647
  • 11
  • 152
  • 247