I’m creating a simple Tic Tac Toe game. I’ve declared my 3 by 3 board and initialized each play area to ' ' or a space character.
However, when I try to print the board so that I get something that looks like this:
1 2 3
A | |
----------
B | |
----------
C | |
Nothing gets printed.
Here is my TicTacToe.java:
import java.util.Scanner;
public class TicTacToe{
public static void main(String[] args) {
new TicTacToe();
}
private char[][] board;
private char player;
public TicTacToe() {
for(int i = 0; i < 3; i++)
{
for(int j = 0; j <3; j++)
{
board[i][j] = ' ';
}
}
player = 'X';
System.out.println(" 1 2 3");
System.out.println("A" + board[0][0] + "|" + board[0][1] + "|" + board[0][2]);
System.out.println("-----");
System.out.println("B" + board[1][0] + "|" + board[1][1] + "|" + board[1][2]);
System.out.println("-----");
System.out.println("C" + board[2][0] + "|" + board[2][1] + "|" + board[2][2]);
}
I’ve read from Murach’s 4th edition that when the class is executed, the constructor is executed as well so I assume that the print functions will be executed.
Question:
How do I print my tic tac toe board like the one above to my console?
Edit:
Thank you for the help. Turns out I had to call the constructor instead of it automatically being executed. Code above does not contain the solution.
I have made some changes in the code
Hope this will help you.