# Java 2d Array Tic Tac Toe Program

Java 2d Array Tic Tac Toe Program | Tic Tac Toe is a game on noughts and crosses that is 0’s and X’s. This game is usually played by two players in a three-by-three grid. The Player who places X’s or 0’s horizontally, vertically, or diagonally will be termed the winner. Prerequisite:- Multi-dimensional array in Java

See for example the tic tac toe board after the play should look like this:-

X X X
0 0 X
0 X 0
The player who has placed X horizontally is the winner.

X 0 0
0 0 X
0 X X
The player who has placed 0 diagonally is the winner.

## Java 2d Array Tic Tac Toe Program Code

Let us see the tic tac toe Java code using 2d array.

``````import java.util.Scanner;

public class Main {

public static void main(String[] args) {
char[][] board = new char[3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
board[i][j] = '-';
}
}

Scanner scan = new Scanner(System.in);
System.out.println("Let's start the game TIC TAC TOE.");
System.out.print("Enter the name of Player1: ");
String play1 = scan.nextLine();
System.out.print("Enter the name of Player2: ");
String play2 = scan.nextLine();

boolean player1 = true;
boolean gameEnded = false;

while (!gameEnded) {
drawBoard(board);

if (player1) {
System.out.println(play1 + "'s Turn (X): ");
} else {
System.out.println(play2 + "'s Turn (O): ");
}

char c = '-';
if (player1) {
c = 'X';
} else {
c = 'O';
}

int row = 0;
int col = 0;

while (true) {
System.out.print("Enter a row number (0, 1, or 2): ");
row = scan.nextInt();
System.out.print("Enter a column number (0, 1, or 2): ");
col = scan.nextInt();
if (row < 0 || col < 0 || row > 2 || col > 2) {
System.out.println("This position is off the "+
"bounds of the board! Try again.");
} else if (board[row][col] != '-') {
"move at this position! Try again.");
} else {
break;
}

}

board[row][col] = c;
if (playerHasWon(board) == 'X') {
System.out.println(play1 + " has won!");
gameEnded = true;
} else if (playerHasWon(board) == 'O') {
System.out.println(play2 + " has won!");
gameEnded = true;
} else {
if (boardIsFull(board)) {
System.out.println("It's a tie!");
gameEnded = true;
} else {
player1 = !player1;
}
}
}
drawBoard(board);
scan.close();
}

public static void drawBoard(char[][] board) {
System.out.println("Board:");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(board[i][j]);
}
System.out.println();
}
}

public static char playerHasWon(char[][] board) {
for (int i = 0; i < 3; i++) {
if (board[i][0] == board[i][1] &&
board[i][1] == board[i][2] &&
board[i][0] != '-') {
return board[i][0];
}
}

for (int j = 0; j < 3; j++) {
if (board[0][j] == board[1][j] &&
board[1][j] == board[2][j] &&
board[0][j] != '-') {
return board[0][j];
}
}

if (board[0][0] == board[1][1] &&
board[1][1] == board[2][2] &&
board[0][0] != '-') {
return board[0][0];
}

if (board[2][0] == board[1][1] &&
board[1][1] == board[0][2] &&
board[2][0] != '-') {
return board[2][0];
}

return ' ';
}

public static boolean boardIsFull(char[][] board) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board[i][j] == '-') {
return false;
}
}
}
return true;
}
}``````

The output of tic tac toe Java code using 2d array:-

Let’s start the game TIC TAC TOE.
Enter the name of Player1: Amelia
Enter the name of Player2: Jhon
Board:
`---------`
Amelia’s Turn (X):
Enter a row number (0, 1, or 2): 0
Enter a column number (0, 1, or 2): 0
Board:
`X--------`
Jhon’s Turn (O):
Enter a row number (0, 1, or 2): 0
Enter a column number (0, 1, or 2): 2
Board:
X-O
`------`
Amelia’s Turn (X):
Enter a row number (0, 1, or 2): 1
Enter a column number (0, 1, or 2): 1
Board:
X-O
-X-
`---`
Jhon’s Turn (O):
Enter a row number (0, 1, or 2): 1
Enter a column number (0, 1, or 2): 2
Board:
X-O
-XO
`---`
Amelia’s Turn (X):
Enter a row number (0, 1, or 2): 2
Enter a column number (0, 1, or 2): 2
Amelia has won!
Board:
X-O
-XO
`--X`

In the above Java 2d array tic tac toe, we took a 3×3 matrix. This 2d matrix is initialized with the character ‘-‘. Later input for the matrix is asked from the user. Based on the entered value if a tic tac toe match is found then the user wins.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!