For some reason it prints "*" nine times on one row. Instead of a 3x3 of *.
Should also be noted that the main function simply runs the displayBoard function and would not change the output.
public class TicTacToeTwoPlayer {
static final int SIZE = 3;
/*
Here:
Declare a 2-d character array called board with SIZE rows and SIZE columns.
Be sure to use the static modifier in the declaration
*/
static char[][] board = new char [SIZE][SIZE];
static final char PLAYER1 = 'X';
static final char PLAYER2 = 'O';
static Scanner userInput = new Scanner(System.in);
public static void initializeBoard() {
/*
Here:
Initialize each position of the board array to a space character.
*/
for (int row = 0; row < board.length; row++) {
for(int col = 0; col < board.length; col++) board[row][col] = ' ';
}
}
public static void displayBoard() {
/*
Here:
Complete this method so that it displays the tic-tac-toe board
to the screen. If a board position is available, that is, if it is
a space, output an asterisk followed by a space ("* ".)
*/
for (int row = 0; row < board.length; row++) {
for (int col = 0; col < board[row].length; col++) {
if (board[row][col] == ' ') {
System.out.print("* ");
} else {
System.out.print(board[row][col]);
}
}
}
System.out.println("Board is: ");
}
}
Any help would be greatly appreciated!
Try printing a newline after each row:
public static void displayBoard() {
for (int row=0; row < board.length; row++) {
for (int col=0; col < board[row].length; col++) {
if (board[row][col] == ' ') {
System.out.print("* ");
}
else {
System.out.print(board[row][col]);
}
}
// print a device-independent newline here after each row
System.out.println();
}
System.out.println("Board is: ");
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.