I did for a 3x3 board, but I need it to expand not to fall apart:

static void printDesc(char[][] desc) { int d = 0; System.out.println("y\\x 0: 1: 2:"); System.out.println(" -----------"); for (int i = 0; i < desc.length; i++) { System.out.print(d + ": | "); for (int j = 0; j < desc.length; j++) { System.out.print(desc[j][i]); System.out.print(" | "); } System.out.println(); System.out.println(" -----------"); d++; } } 
  • what does it mean expanding and not falling apart? - Mikhail Vaysman
  • Well, when I set the size of the field is not 3 by 3, the whole picture is spreading. - Pavel
  • you have System.out.println("y\\x 0: 1: 2:"); fixed right in the code System.out.println("y\\x 0: 1: 2:"); and System.out.println(" -----------"); . These 2 lines do not depend on the size of your field. they will not stretch. - Mikhail Vaysman

1 answer 1

 static void printDesc(char[][] desc) { int d = 0; System.out.print("y\\x"); for (int i = 0; i<desc.length; i++) System.out.print(" " + i + ":"); System.out.println(""); printDivider(desc.length); for (int i = 0; i < desc.length; i++) { System.out.print(d + ": | "); for (int j = 0; j < desc.length; j++) { System.out.print(desc[j][i]); System.out.print(" | "); } System.out.println(); printDivider(desc.length); d++; } } static void printDivider(int length) { System.out.print(" "); for (int i = 0; i<length-1;i++) { System.out.print("----"); } System.out.println("---"); } 

for the test:

 char[][] d = new char[][] {{'x','x','x','x','x'}, {'x','x','x','x','x'}, {'x','x','x','x','x'}, {'x','x','x','x','x'}, {'x','x','x','x','x'}}; 

Result:

 y\x 0: 1: 2: 3: 4: ------------------- 0: | x | x | x | x | x | ------------------- 1: | x | x | x | x | x | ------------------- 2: | x | x | x | x | x | ------------------- 3: | x | x | x | x | x | ------------------- 4: | x | x | x | x | x | ------------------- 

for the test:

 char[][] d = new char[][] {{'x','x'}, {'x','x'}}; 

Result:

 y\x 0: 1: ------- 0: | x | x | ------- 1: | x | x | ------- 
  • Cool! Thank! - Pavel