CREATE A TIC TAC TOE TEST:
Problem Description:
the Tic-Tac-Toe Create a class TicTacToe that will enable you to write a program to play Tic-Tac-Toe. The class contains a private 3-by-3 two-dimensional array. Use an enum type to represent the status of the game after a move, WIN, DRAW, CONTINUE. The value in each cell of the array should be named X, O, and EMPTY (for a position that does not contain an X or an O). The constructor should initialize the board elements to EMPTY.
Allow two human players. Whenever the first player moves, place an X in the specific square, and place an O wherever the second player moves. Each move must be to an empty square. After each move, determine whether the game has been won and whether it’s a draw.
Create another class TicTacToeTest contains the main() method, instantiates the object TicTacToe, and invokes the methods of TicTacToe (printBoard(), play()) to play the game.
In your class, you need to have the following • Constructor: construct the board for the game; initialize the instance variables • Method play(): loop until the game is over • Method printStatus(): prompt for the turn of the player, winner, or draw • Method gameStatus(): return the status of the game after a move, WIN, DRAW, CONTINUE • Method printBoard(): Output the 3-by-3 grid board on the screen • Method validMove(): validate the interned move by player
TicTacToeEnum.java
public enum TicTacToeEnum {
WIN,
DRAW,
CONTINUE;
}
//end of TicTacToeEnum.java
// TicTacToe.java : Java program to implement the game of TicTacToe
import java.util.Scanner;
public class TicTacToe {
private String board[][];
// constructor
public TicTacToe()
{
board = new String[3][3];
for(int i=0;i<board.length;i++)
{
for(int j=0;j<board[i].length;j++)
board[i][j] = "";
}
}
// method to display the board
public void printBoard()
{
for(int i=0;i<board.length;i++)
{
for(int j=0;j<board[i].length;j++)
{
if(board[i][j].length() > 0)
System.out.print(board[i][j] +"| ");
else
System.out.print(board[i][j] +" | ");
}
System.out.println();
}
}
// method to return the status of the game
public TicTacToeEnum gameStatus()
{
int x_count=0,o_count=0;
// loop to find the winner in horizontal rows
for(int i=0;i<3;i++) {
for(int j=0;j<3;j++)
{
if(board[i][j].equalsIgnoreCase("X"))
x_count++;
else if(board[i][j].equalsIgnoreCase("O"))
o_count++;
}
if(x_count == 3 || o_count == 3)
return TicTacToeEnum.WIN;
x_count = 0;
o_count=0;
}
x_count = 0;
o_count=0;
// loop to find the winner in vertical columns
for(int i=0;i<3;i++) {
for(int j=0;j<3;j++)
{
if(board[j][i].equalsIgnoreCase("X"))
x_count++;
else if(board[j][i].equalsIgnoreCase("O"))
o_count++;
}
if(x_count == 3 || o_count == 3)
return TicTacToeEnum.WIN;
x_count = 0;
o_count=0;
}
// check in the main diagonal
if((board[0][0].equalsIgnoreCase(board[1][1]) && board[1][1].equalsIgnoreCase(board[2][2])) &&
(board[0][0].equalsIgnoreCase("X") || board[0][0].equalsIgnoreCase("O")))
return TicTacToeEnum.WIN;
// check in the other diagonal
if((board[0][2].equalsIgnoreCase(board[1][1]) && board[1][1].equalsIgnoreCase(board[2][0])) &&
(board[1][1].equalsIgnoreCase("X") || board[1][1].equalsIgnoreCase("O")))
return TicTacToeEnum.WIN;
// check for empty positions
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
if(board[i][j].length() == 0)
return TicTacToeEnum.CONTINUE;
}
return TicTacToeEnum.DRAW;
}
// method to check if the move is a valid move
public boolean validMove(int x, int y)
{
if(x < 0 || x> 2 || y<0 || y>2)
{
System.out.println(" Invalid move. The coordinate ( "+x+","+ y+" ) is out of range.");
return false;
}else if(board[x][y].length() > 0)
{
System.out.println(" Invalid move. The coordinate ( "+x+","+ y+" ) is already taken.");
return false;
}
return true;
}
// method to play a game of TicTacToe with 2 human players
public void play()
{
boolean xTurn = true;
int x,y;
Scanner scan = new Scanner(System.in);
while(gameStatus() == TicTacToeEnum.CONTINUE)
{
printBoard();
if(xTurn)
{
System.out.print(" Player-X turn Enter row and column : ");
x = scan.nextInt();
y = scan.nextInt();
while(!validMove(x,y))
{
System.out.print(" Enter row and column : ");
x = scan.nextInt();
y = scan.nextInt();
}
board[x][y] = "X";
xTurn = false;
}else
{
System.out.print(" Player-O turn Enter row and column : ");
x = scan.nextInt();
y = scan.nextInt();
while(!validMove(x,y))
{
System.out.print(" Enter row and column : ");
x = scan.nextInt();
y = scan.nextInt();
}
board[x][y] = "O";
xTurn = true;
}
}
printBoard();
if(gameStatus() == TicTacToeEnum.DRAW)
System.out.print(" It results in a DRAW.");
else
{
if(xTurn)
System.out.print(" Player-O is the winner");
else
System.out.print(" Player-X is the winner");
}
scan.close();
}
}
//end of TicTacToe.java
// TicTacToeTest .java : Driver program to play the game
public class TicTacToeTest {
public static void main(String[] args) {
TicTacToe game = new TicTacToe();
game.play();
}
}
//end of TicTacToeTest .java
Output:


CREATE A TIC TAC TOE TEST: Problem Description: the Tic-Tac-Toe Create a class TicTacToe that will...