(Tic-Tac-Toe) Create a class Tic-Tac-Toe 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 enumeration to represent the value in each cell of the array. The enumeration’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. Also, allow the player to specify whether he or she wants to go first or second.
Solution:
Code:
Output:
Copyable Code:
import java.util.Scanner;
//Class
class GameTicTacToe
{
private value[][] brd;
private value crntPlayer;
public enum value
{
EMPTY, X, O
}
public GameTicTacToe(int player)
{
brd = new value[3][3];
if(player == 1)
crntPlayer = value.X;
else
crntPlayer = value.O;
initializebrd();
}
//Initialize the board
public void initializebrd() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
brd[i][j] = value.EMPTY;
}
}
}
public char getPlayer(){
return changeValue(crntPlayer);
}
public char changeValue(value v)
{
if (v == value.EMPTY)
{
return '-';
}
else if (v == value.X)
{
return 'X';
}
else
{
return 'O';
}
}
//Method definition
public void writeBoard()
{
System.out.println("-------------");
for (int i = 0; i < 3; i++)
{
System.out.print("| ");
for (int j = 0; j < 3; j++)
{
System.out.print(changeValue(brd[i][j]) + " | ");
}
System.out.println();
System.out.println("-------------");
}
}
//Method definition
public boolean isBoardFull()
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (brd[i][j] == value.EMPTY)
{
return false;
}
}
}
return true;
}
//Method definition
public boolean isWin() {
return (isRowsWin() || isColumnWin() || isDiagonalWin());
}
//Method definition
private boolean isRowsWin() {
for (int i = 0; i < 3; i++)
{
if (checkRowCol(brd[i][0], brd[i][1], brd[i][2]) == true)
{
return true;
}
}
return false;
}
//Method definition
private boolean isColumnWin()
{
for (int i = 0; i < 3; i++)
{
if (checkRowCol(brd[0][i], brd[1][i], brd[2][i]) == true)
{
return true;
}
}
return false;
}
//Method definition
private boolean isDiagonalWin()
{
return ((checkRowCol(brd[0][0], brd[1][1], brd[2][2]) == true)
|| (checkRowCol(brd[0][2], brd[1][1], brd[2][0]) == true));
}
//Method definition
private boolean checkRowCol(value brd2, value brd3, value brd4)
{
return ((brd2 != value.EMPTY) && (brd2 == brd3) && (brd3 == brd4));
}
//Method definition
public void changePlayer()
{
if (crntPlayer == value.X)
{
crntPlayer = value.O;
}
else
{
crntPlayer = value.X;
}
}
//Method definition
public void writeMark(int row, int col)
{
brd[row][col] = crntPlayer;
}
}
//Main class
public class Main {
//Object for input
public static Scanner in = new Scanner(System.in);
//Method definition
public static int getChoice()
{
while(true)
{
System.out.println("1. X takes first turn");
System.out.println("2. O takes first turn");
System.out.print("Please make a choice: ");
int ch = in.nextInt();
if(ch == 1 || ch == 2){
return ch;
}
else{
System.out.println("Invalid choice. try again!!");
}
}
}
//Main method
public static void main(String[] args){
int ch = getChoice(), row, col;
GameTicTacToe t = new GameTicTacToe(ch);
t.writeBoard();
while(true){
System.out.println("Player " + t.getPlayer() + "'s turn");
System.out.print("Row index: ");
row = in.nextInt();
System.out.print("Column index: ");
col = in.nextInt();
t.writeMark(row, col);
t.writeBoard();
if(t.isWin()){
System.out.println(t.getPlayer() + " has won");
break;
}
else if(t.isBoardFull()){
System.out.println("Draw");
break;
}
t.changePlayer();
}
}
}
(Tic-Tac-Toe) Create a class Tic-Tac-Toe that will enable you to write a program to play Tic-Tac-Toe....
(Tic-Tac-Toe) Create class TicTacToe that will enable you to write a complete app to play the game of Tic-Tac-Toe. The class contains a private 3-by-3 rectangular array of integers. The constructor should initialize the empty board to all 0s. Allow two human players. Wherever the first player moves, place a 1 in the specified square, and place a 2 wherever the second player moves. Each move must be to an empty square. After each move, determine whether the game has...
Write a program to Simulate a game of tic tac toe. A game of tic tac toe has two players. A Player class is required to store /represent information about each player. The UML diagram is given below.Player-name: string-symbol :charPlayer (name:string,symbol:char)getName():stringgetSymbol():chargetInfo():string The tic tac toe board will be represented by a two dimensional array of size 3 by 3 characters. At the start of the game each cell is empty (must be set to the underscore character ‘_’). Program flow:1) ...
PYTHON Exercise 2. Tic-Tac-Toe In this exercise we are going to create a Tic-Tac-Toe game. 1. Create the data structure – Nine slots that can each contain an X, an O, or a blank. – To represent the board with a dictionary, you can assign each slot a string-value key. – String values in the key-value pair to represent what’s in each slot on the board: ■ 'X' ■ 'O' ■ ‘ ‘ 2. Create a function to print the...
Write a c program that will allow two users to play a tic-tac-toe game. You should write the program such that two people can play the game without any special instructions. (assue they know how to play the game). Prompt first player(X) to enter their first move. .Then draw the gameboard showing the move. .Prompt the second player(O) to enter their first move. . Then draw the gameboard showing both moves. And so on...through 9 moves. You will need to:...
Please make this into one class. JAVA Please: Tic Tac Toe class - Write a fifth game program that can play Tic Tac Toe. This game displays the lines of the Tic Tac Toe game and prompts the player to choose a move. The move is recorded on the screen and the computer picks his move from those that are left. The player then picks his next move. The program should allow only legal moves. The program should indicate when...
Tic-Tac-Toe (arrays and multidimensional arrays) C++ Write an interactive program that plays tic-tac-toe. Represent the board as a 3 X 3 character array. Initialize the array to blanks and ask each player in turn to input a position. The first player's position is marked on the board with a O and the second player's position is marked with an X. Continue the process until a player wins or the game is a draw. To win, a player must have 3...
18. Tic-Tac-Toe Game rite a program that allows two players to play a game of tic-tac-toe. Use dimensional char array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (*). The program should run a loop that does the following: Write . Displays the contents of the board array. . Allows player 1 to select a location on the board for an X. The program should ask the...
(Game: play a tic-tac-toe game) In a game of tic-tac-toe, two players take turns marking an available cell in a grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A draw (no winner) occurs when all the cells in the grid have been filled with tokens and neither player has achieved a win. Create...
Tic Tac Toe Game: Help, please. Design and implement a console based Tic Tac Toe game. The objective of this project is to demonstrate your understanding of various programming concepts including Object Oriented Programming (OOP) and design. Tic Tac Toe is a two player game. In your implementation one opponent will be a human player and the other a computer player. ? The game is played on a 3 x 3 game board. ? The first player is known as...
In a game of Tic Tac Toe, two players take turns making an available cell in a 3 x 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A stalemate occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win. Write a program...