Writing a code in C# console app
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 been won and whether it’s a draw.
using System;
using System.Threading;
namespace TIC_TAC_TOE
{
class Program
{
static char[] arr = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
static int player = 1;
//By default player 1 is set
static int choice;
static int flag =
0;
static void
Main(string[] args)
{
do
{
Console.Clear();
Console.WriteLine("Player1:X and Player2:O");
Console.WriteLine("\n");
if (player % 2 == 0)//checking the chance of the player
{
Console.WriteLine("Player 2 Chance");
}
else
{
Console.WriteLine("Player 1 Chance");
}
Console.WriteLine("\n");
Board();
choice = int.Parse(Console.ReadLine());
if (arr[choice] != 'X' && arr[choice] != 'O')
{
if (player % 2 == 0) //if chance is of player 2 then mark O else
mark X
{
arr[choice] = 'O';
player++;
}
else
{
arr[choice] = 'X';
player++;
}
}
else
{
Console.WriteLine("Sorry the row {0} is already marked with {1}",
choice, arr[choice]);
Console.WriteLine("\n");
Console.WriteLine("Please wait 2 second board is loading
again.....");
Thread.Sleep(2000);
}
flag = CheckWin();// calling of check win
} while (flag != 1 && flag != -1);
Console.Clear();
Board();
if (flag == 1)
{
Console.WriteLine("Player {0} has won", (player % 2) + 1);
}
else
{
Console.WriteLine("Draw");
}
Console.ReadLine();
}
private static void
Board()
{
Console.WriteLine("
| | ");
Console.WriteLine(" {0} | {1} | {2}", arr[1], arr[2],
arr[3]);
Console.WriteLine("_____|_____|_____ ");
Console.WriteLine("
| | ");
Console.WriteLine(" {0} | {1} | {2}", arr[4], arr[5],
arr[6]);
Console.WriteLine("_____|_____|_____ ");
Console.WriteLine("
| | ");
Console.WriteLine(" {0} | {1} | {2}", arr[7], arr[8],
arr[9]);
Console.WriteLine("
| | ");
}
private static int
CheckWin()
{
if (arr[1] == arr[2] && arr[2] == arr[3])
{
return 1;
}
else if (arr[4] == arr[5] && arr[5] == arr[6])
{
return 1;
}
else if (arr[6] == arr[7] && arr[7] == arr[8])
{
return 1;
}
else if (arr[1] == arr[4] && arr[4] == arr[7])
{
return 1;
}
else if (arr[2] == arr[5] && arr[5] == arr[8])
{
return 1;
}
else if (arr[3] == arr[6] && arr[6] == arr[9])
{
return 1;
}
#endregion
else if (arr[1] == arr[5] && arr[5] == arr[9])
{
return 1;
}
else if (arr[3] == arr[5] && arr[5] == arr[7])
{
return 1;
}
else if (arr[1] != '1' && arr[2] != '2' && arr[3]
!= '3' && arr[4] != '4' && arr[5] != '5' &&
arr[6] != '6' && arr[7] != '7' && arr[8] != '8'
&& arr[9] != '9')
{
return -1;
}
else
{
return 0;
}
}
}
}
Writing a code in C# console app Create class TicTacToe that will enable you to write...
(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...
(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...
I need help writing a Java program for a game of TicTacToe. It is a two player game, and the rules are as follows: 1. The game begins with an empty, 3 × 3 grid. 2. The two players then take turns placing a mark in an empty grid cell. Player O will use the ‘O’ (letter ‘O’, not zero) mark and Player X will use the ‘X’ mark. Player O moves first. 3. The game is over in either...
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...
In Java create a program where a user plays Tic Tac Toe against the computer. Create a class TicTacToe that will enable you write a program to play Tic-Tac-Toe. The class contains a private 3x3 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 O). The constructor should initialize the board elements to...
Create a TicTacToe class that initializes a 3x3 board of "-" values. We will use this class in future exercises to fully build out a Tic Tac Toe game! The TicTacToe class should have a 2D array as an instance variable and a constructor that initializes the 2D array with the "-" value. Add a getter method that returns the private 2D instance variable. public class TicTacToeTester { //You don't need to alter any of the code in this class!...
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...
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...
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) ...
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:...