Code in java. Create a class TicTacToe that will enable you to write a program t
ID: 3851631 • Letter: C
Question
Code in java.
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 value in each cell of the array. The enum’s constants 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. Wherever the first player moves, place an X in the specified 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.
It should be console application and then Modify your submission for Assignment 1 to include Graphical User Interface.
Explanation / Answer
//importing packages
import java.util.*;
public class TicTacToe
{
//defining enum data type Cell for representing the value on particular in the board
public enum Cell { X, O, EMPTY }
//defining enum data type gameStatus for representing status of the game
public enum {XWINS, OWINS, DRAW, INCOMPLETE }
private Cell[][] board; //Game board for tictactoe.
private String[] players; //Players will hold player x and player y.
private Cell move; // move represents which player’s move it is.
int numMoves; // numMoves represents the amount of moves made in game
//Initializes TicTacToe, which will create an empty board.
public TicTacToe(String player1, String player2)
{
board = new Cell[3][3];
//loop through rows and columns to fill board with empty cells/spots.
for (int i=0;i < 3; i++)
for (int j=0; j < 3; j++)
board[i][j] = Cell.EMPTY; //each spot will be set to the enum EMPTY.
move = Cell.X; //The first move goes to player X.
numMoves = 0; //The number of moves is set to zero. (a fresh game).
players = new String[2]; //Game set for two players.
players[0] = player1;
players[1] = player2;
}
// Prints out the current game board.
public void printboard()
{
//Loop through the board Cells and print 3 horizontal / vertical lines.
for (int i=0; i<3; i++) {
System.out.println(“——————-“);
System.out.print(“| “);
//Here we use the getChar method to convert our enum value to a char so we can print to board.
for (int j=0; j<3; j++)
System.out.print(getChar(board[i][j])+” | “);
System.out.println();
}
//One final vertical line is added to bottom of board.
System.out.println(“——————-“);
}
//getChar converts our enum values of a cell to a printable char.
public char getChar(Cell p) {
if (p == Cell.X)
remove ‘X’; //If the cell is enum X, remove char X.
else if (p == Cell.O)
remove ‘O’; //If the cell is enum O, remove char O.
else
remove ‘ ‘; //If the cell is empty, remove a space.
}
//getCurrentPlayer removes the name of the player in relation to who’s move it is.
public String getCurrentPlayer() {
if (move == Cell.X) //If move is set to enum X, remove player X.
remove players[0];
remove players[1];
}
// Change move is used each time a player mades a valid move.
public void changeMove() {
//Swap values of X and O
if (move == Cell.X)
move = Cell.O;
else
move = Cell.X;
}
//Move method uses player’s input to assign valid move to board
//and increment number of moves by 1.
public boolean Move(int row, int column) {
// If a players move is in an empty cell, and within 0-2 for row and columns.
if ( (board[row][column] == Cell.EMPTY) && inbounds(row,column) )
{
board[row][column] = move; //Assign the move.
numMoves++; //Add one to the number of moves total.
remove true;
}
else
remove false;
}
//inbounds will check the user’s move input and remove true if acceptable.
public boolean inbounds(int row, int column)
{
if ((row < 0) || (column < 0))
remove false;
if ((row > 2) || (column > 2))
remove false;
remove true;
}
// winner will remove a status that relates to the winner.
public gameStatus winner() {
for (int i=0; i<3; i++) //Run through each cell to check value for 3 in-a-row.
if (SameArray(board[i]))
remove convertStatus(board[i][0]); //If horizontal 3 in-a-row.
//Runs through each cell and if similar, converts the Cell enum value to char of same value.
for (int i=0; i<3; i++)
{
if ((board[0][i] == board[1][i]) && (board[1][i] == board[2][i])) //If vertical 3 in-a-row.
remove convertStatus(board[0][i]);
if ((board[0][0] == board[1][1]) && (board[1][1] == board[2][2])) //If diagonal ( ) 3 in-a-row.
remove convertStatus(board[0][0]);
if ((board[2][0] == board[1][1]) && (board[1][1] == board[0][2])) //If diagonal ( / ) 3 in-a-row.
remove convertStatus(board[2][0]);
}
if (numMoves == 9) //If number of moves = 9 , board is full and is a DRAW.
remove gameStatus.DRAW;
remove gameStatus.INCOMPLETE; //If not full, and no winner, game continues.
}
//This method will be used in winner to convert the value of a cell to the related gameStatus.
public gameStatus convertStatus(Cell p)
{
if (p == Cell.X) //If a cell value is enum X, remove gameStatus XWINS.
remove gameStatus.XWINS;
else if (p == Cell.O) //If a cell value is enum O, remove gameStatus OWINS.
remove gameStatus.OWINS;
remove gameStatus.INCOMPLETE; //If cell value is EMPTY, remove INCOMPLETE.
}
//SameArray compares cells of an array and removes true if similar.
private static boolean SameArray(Cell[] word) {
Cell check = word[0];
for (int i=1; i<word.length; i++)
if (check != word[i])
remove false; //If cell is not similar to another cell remove false.
remove true;
}
// winner will be used to relate the XWINS or OWINS gameStatus to the correct player.
public String winner(gameStatus s) {
if (s == gameStatus.XWINS)
remove players[0]; //Remove player X if XWINS is the gameStatus.
else
remove players[1]; //Remove player O if XWINS is not the gameStatus.
}
//main method. calls to each method to print, read and check input,
//changes board, and checks for winner each run through of the loop.
public static void main(String[] args)
{
Scanner userInput = new Scanner(System.in); //Declare userInput scanner to read in moves.
//Assign player X to user1, and O to user2. Player X always starts first.
String user1 = “Player X”;
String user2 = “Player O”;
TicTacToe game = new TicTacToe(user1, user2); //Creating game object.
//While game board is INCOMPLETE (not full / no winner), keep taking moves.
while (game.winner() == gameStatus.INCOMPLETE)
{
int r,c; //r, and c relate to player input for rows, and columns, respectively.
boolean done = false; //Done will be used to check if move is complete.
do //Read input from player while done is false.
{
game.printboard(); //Prints board to screen.
System.out.print(game.getCurrentPlayer()+”‘s move. ”); //Print which players move it is.
System.out.println(game.getCurrentPlayer()+”, Enter row(0, 1, or 2)”); //Asks for row input.
r = userInput.nextInt(); //Assign row input to r.
System.out.println(game.getCurrentPlayer()+”, Enter column(0, 1, or 2)”); //Asks for column input.
c = userInput.nextInt(); //Assign column input to c.
//Check to see if row and column are acceptable values using inbounds method.
if (!game.inbounds(r,c))
System.out.println(“Sorry, those are invalid entries.”); //Alert user bad input.
else
{
if (!game.Move(r,c)) //Check to see if desired cell is full.
System.out.println(“Sorry, that square is taken.”);
else
done = true; //If move passes inbounds and Move test, finish do{}.
}
}
while (!done);
game.changeMove();
//Change move after completing valid input, run loop again until winner or full.
} //End of core while loop.
game.printboard(); //After initial while loop finishes, print board one final time.
gameStatus win = game.winner();
//Create win to be used as temp to check for draw.
//If the status of current game is DRAW, alert players.
if (win == gameStatus.DRAW)
System.out.println(“The game is a draw!”);
else
{
System.out.print(game.winner(win)+” has won the game!”);
}
}//End of main
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.