In: Computer Science
C# (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 been won and whether it’s a draw. If you feel ambitious, modify your app so that the computer makes the moves for one of the players. Also, allow the player to specify whether he or she wants to go first or second. If you feel exceptionally ambitious, develop an app that will play three-dimensional Tic-Tac-Toe on a 4-by-4-by-4 board. (Use Console Clear)
Code -
using System;
using System.Threading;
namespace TIC_TAC_TOE
{
class Program
{
//by default position array is define from 0-9 and no use of zero
static char[] position = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
static int player = 1; // player 1 is will play first so set it as default
static int positionChoice; //position where player have to mark
//variable to check win
static int checkWin = 0;
static void Main(string[] args)
{
do
{
Console.Clear();// whenever loop will be again start then screen will be clear
Console.WriteLine("Player1 mark is : X and Player2 mark is : 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");
createBoard();// calling the board Function
positionChoice = int.Parse(Console.ReadLine());//Taking users positionChoice
// checking that position where user want to run is marked (with X or O) or not
if (position[positionChoice] != 'X' && position[positionChoice] != 'O')
{
if (player % 2 == 0) //if chance is of player 2 then mark O else mark X
{
position[positionChoice] = 'O';
player++;
}
else
{
position[positionChoice] = 'X';
player++;
}
}
else //If there is any possition where user want to run and that is already marked then show message and load board again
{
Console.WriteLine("Sorry the row {0} is already marked with {1}", positionChoice, position[positionChoice]);
Console.WriteLine("\n");
Console.WriteLine("Please wait 2 second board is loading again.....");
Thread.Sleep(2000);
}
checkWin = checkPlayerWin();// calling of check win
} while (checkWin != 1 && checkWin != -1);// This loof will be run until all cell of the grid is not marked with X and O or some player is not win
Console.Clear();// clearing the console
createBoard();// getting filled board again
if (checkWin == 1)// if checkWin value is 1 then some one has win or means who played marked last time which has win
{
Console.WriteLine("Player {0} has won", (player % 2) + 1);
}
else// if checkWin value is -1 the match will be draw and no one is winner
{
Console.WriteLine("Draw");
}
Console.ReadLine();
}
// createBoard method which create board
private static void createBoard()
{
Console.WriteLine(" | | ");
Console.WriteLine(" {0} | {1} | {2}", position[1], position[2], position[3]);
Console.WriteLine("_____|_____|_____ ");
Console.WriteLine(" | | ");
Console.WriteLine(" {0} | {1} | {2}", position[4], position[5], position[6]);
Console.WriteLine("_____|_____|_____ ");
Console.WriteLine(" | | ");
Console.WriteLine(" {0} | {1} | {2}", position[7], position[8], position[9]);
Console.WriteLine(" | | ");
}
//method to check if any play have won or not
private static int checkPlayerWin()
{
#region Horzontal Winning Condtion
//check for first row win
if (position[1] == position[2] && position[2] == position[3])
{
return 1;
}
//check for second row win
else if (position[4] == position[5] && position[5] == position[6])
{
return 1;
}
//check for third row win
else if (position[6] == position[7] && position[7] ==
position[8])
{
return 1;
}
#endregion
#region check for vertical wining condition
//Winning Condition For First Column
else if (position[1] == position[4] && position[4] == position[7])
{
return 1;
}
//Winning Condition For Second Column
else if (position[2] == position[5] && position[5] == position[8])
{
return 1;
}
//Winning Condition For Third Column
else if (position[3] == position[6] && position[6] == position[9])
{
return 1;
}
#endregion
#region check for diagnol winning condition
else if (position[1] == position[5] && position[5] == position[9])
{
return 1;
}
else if (position[3] == position[5] && position[5] == position[7])
{
return 1;
}
#endregion
#region check for draw
// If all the cells or values filled with X or O then any player has won the match
else if (position[1] != '1' && position[2] != '2' && position[3] != '3' && position[4] != '4' && position[5] != '5' && position[6] != '6' && position[7] != '7' && position[8] != '8' && position[9] != '9')
{
return -1;
}
#endregion
else
{
return 0;
}
}
}
}
Screenshots -
pls do give a like , thanks