Java question Question Define a class called TicTacToe. An object of type TicTac
ID: 3694121 • Letter: J
Question
Java question
Question Define a class called TicTacToe. An object of type TicTacToe is a single game of tic-tac-toe. Store the game board as a single two-dimensional array of base tpe char that has three rows and three columns. Include methods to add a move, to display the board, to tell whose turn it is (X or O), to tell whether there is a winner, to say who the winner is, and to reinitialize the game to the beginning. Write a main method for the class that will allow two players to enter their moves in turn at the same keyboard.
List of Variables
// Use a 3 X 3 (two-dimensional) array for the game board. private static char[][] board = new char[3][3];
private static char turn;
private static int row; // Loop controls to
private static int col; // display the board
private static int turnRow; // User input to
private static int turnCol; // select move
private static boolean entryError;
private static boolean anotherGame = true;
private static char repeat; // User input: y or Y to repeat
private static int entryCount = 0; // Game ends when board is full // (when entryCount = 9);
List of Methods
private static void writeBoard()
private static void getMove()
private static boolean winner()
private static void newGame()
Explanation / Answer
Ans;
import java.util.Scanner;
public class TicTacToeML2
{
public static void main(String[] args)
{
Scanner console = new Scanner (System.in);
Game x = new Game();
String n = "n";
System.out.println("Welcome Mitchell's version of Tic-Tac-Toe!");
System.out.println("Please press ENTER to begin");
String input = console.nextLine();
while(!input.equals(n))
{
x.play();
System.out.println("Thank you for playing Tic-Tac-Toe.");
System.out.println("Would you like to play again? Press any key for yes, press n for no: ");
input=console.nextLine();
}
}
}
class Game
{
private final int empty = 0;
private final int player = 1;
private final int com = 2;
private final int size = 3;
private int[][] board;
public Game()
{
}
public void displayBoard()
{
int col;
int row;
System.out.println();
System.out.print(" ");
for (col = 0; col < size; col ++)
{
System.out.print(" " + (col+1));
}
System.out.println();
System.out.print(" ");
for (col = 0; col < size; col ++)
{
System.out.print("+-");
}
System.out.println("+");
for (row = 0; row < size; row ++)
{
System.out.print((row+1) + "|");
for (col = 0; col < size; col ++)
{
if (board[row][col] == empty)
{
System.out.print(" ");
}
else if (board[row][col] == player)
{
System.out.print("X");
}
else if (board[row][col] == com)
{
System.out.print("O");
}
System.out.print("|");
}
System.out.println();
System.out.print(" ");
for (col = 0; col < size; col ++)
{
System.out.print("+-");
}
System.out.println("+");
}
}
public void clear()
{
int col;
int row;
board = new int[size][size];
for (row = 0; row < size; row ++)
{
for (col = 0; col < size; col ++)
{
board[row][col] = empty;
}
}
}
public void comSelection()
{
int col;
int row;
int count;
int select;
count = 0;
for (row = 0; row < size; row ++)
for (col = 0; col < size; col ++)
if (board[row][col] == empty)
count ++;
select = (int) (Math.random() * count);
count = 0;
for (row = 0; row < size; row ++)
{
for (col = 0; col < size; col ++)
{
if (board[row][col] == empty)
{
if (count == select)
{
board[row][col] = com;
System.out.println("The computer selects row" + (row+1) + " column " + (col+1) + ".");
}
count ++;
}
}
}
}
public void humanSelection()
{
Scanner console = new Scanner (System.in);
boolean a;
int col;
int row;
a = true;
while (a)
{
System.out.println("What is your move? Select a row number from 1 to " + size + " and a column number from 1 to " + size + ".");
row = console.nextInt();
col = console.nextInt();
if ((row < 1) || (row > size) || (col < 1) || (col > size))
{
System.out.println("Invalid choice, row " + row + " or column " + col + " must be from 1 to " + size + ".");
}
else
{
row --;
col --;
if (board[row][col] != empty)
{
System.out.println("That spot is already filled");
displayBoard();
}
else
{
board[row][col] =player;
a = false;
}
}
}
}
public boolean end()
{
int col;
int row;
int count;
int win;
win = empty;
for (row = 0; row < size; row ++)
{
count = 0;
if (board[row][0] != empty)
for (col = 0; col < size; col ++)
if (board[row][0] == board[row][col])
count ++;
if (count == size)
win = board[row][0];
}
for (col = 0; col < size; col ++)
{
count = 0;
if (board[0][col] != empty)
for (row = 0; row < size; row ++)
if (board[0][col] == board[row][col])
count ++;
if (count == size)
win = board[0][col];
}
count = 0;
if (board[0][0] != empty)
for (row = 0; row < size; row ++)
if (board[0][0] == board[row][row])
count ++;
if (count == size)
win = board[0][0];
count = 0;
if (board[0][size-1] != empty)
for (row = 0; row < size; row ++)
if (board[0][size-1] == board[row][size-row-1])
count ++;
if (count == size)
win = board[0][size-1];
if (win != empty)
{
if (win == player)
System.out.println("YOU ARE THE WINNER!");
else if (win == com)
System.out.println("The Computer wins it this time, better luck next time :(");
return true;
}
count = 0;
for (row = 0; row < size; row ++)
for (col = 0; col < size; col ++)
if (board[row][col] == empty)
count ++;
if (count == 0)
{
System.out.println("Its a tie!");
return true;
}
return false;
}
public void play()
{
boolean e;
clear();
e = false;
while (!e)
{
displayBoard();
humanSelection();
displayBoard();
e = end();
if (!e)
{
comSelection();
e = end();
if (e)
displayBoard();
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.