using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading;
namespace tictoegame
{
public class gamestart
{
int functionalcount, computernumber,usernumber;
char[] box = new char[9];
static int j=6;
public gamestart()
{
for(int k=0;k<9;k++)
{
box[k] = ' ';
}
functionalcount = 0;
Random rd = new Random();
while(true)
{
computernumber = rd.Next(0, 8);
if (box[computernumber]!='x'&& box[computernumber]!='X'&&box[computernumber]!='o'&& box[computernumber]!='O')
{
insertvalue(computernumber,Program.computersymbol);
break;
}
}
functionalcount++;
userturn();
}
public void position()
{
int i=1;
Console.CursorLeft=i;
Console.CursorTop=j;
j++;
}
public void userturn()
{
if (functionalcount != 9)
{
bool flag1 = checkwin(Program.usersymbol);
if (flag1)
{
position();
Console.WriteLine("you won");
Thread.Sleep(3000);
Environment.Exit(0);
}
else
{
while (true)
{
position();
Console.WriteLine("enter the number for symbol");
usernumber = Convert.ToInt32(Console.ReadLine());
if (box[usernumber] != 'x' || box[usernumber] != 'X' && box[usernumber] != 'o' || box[usernumber] != 'O')
{
insertvalue(usernumber, Program.usersymbol);
functionalcount++;
break;
}
}
computerturn();
}
}
}
public void computerturn()
{
int count=0,i;
if (functionalcount == 2)
{
Random rd = new Random();
while (true)
{
computernumber = rd.Next(0, 8);
if((box[computernumber] != 'x' || box[computernumber] != 'X') && (box[computernumber] != 'o' || box[computernumber] != 'O'))
{
insertvalue(computernumber,Program.computersymbol);
functionalcount++;
break;
}
}
userturn();
}
if(functionalcount==4||functionalcount==6)
{
bool flag=checkthirdposition(Program.computersymbol,ref count);
bool flag1 = checkwin(Program.computersymbol);
if(flag1)
{
position();
Console.WriteLine("computer wins");
Thread.Sleep(3000);
Environment.Exit(0);
}
bool flag2=checkthirdposition(Program.usersymbol,ref count);
if(flag2)
{
box[count]=Program.computersymbol;
insertvalue(count,Program.computersymbol);
functionalcount++;
userturn();
}
else
{
Random rd = new Random();
while (true)
{
computernumber = rd.Next(0, 8);
if((box[computernumber] != 'x' || box[computernumber] != 'X' )&&( box[computernumber] != 'o' || box[computernumber] != 'O'))
{
insertvalue(computernumber,Program.computersymbol);
functionalcount++;
break;
}
}
userturn();
}
}
if(functionalcount==8)
{
int con;
for(i=0;i<9;i++)
{
if((box[i]!='x'||box[i]!='X')&&(box[i]!='o'||box[i]!='O'))
{
con=i;
box[con]=Program.computersymbol;
insertvalue(con,Program.computersymbol);
}
}
functionalcount++;
position();
Console.WriteLine("draw");
userturn();
}
}
public bool checkthirdposition(char ch, ref int x)
{
if (checkpos(1, 2, 0, ch) || checkpos(3, 6, 0, ch) || checkpos(4, 8, 0, ch))
{
if (ch == Program.computersymbol)
{
insertvalue(0, ch);
x = 0;
return true;
}
else
if(ch==Program.usersymbol)
{
x=0;
return true;
}
}
if (checkpos(0,2,1, ch) || checkpos(7,4,1,ch))
{
if (ch == Program.computersymbol)
{
insertvalue(1, ch);
x = 1;
return true;
}
else
if(ch==Program.usersymbol)
{
x = 1;
return true;
}
}
if(checkpos(0,1,2,ch) || checkpos(8,5,2,ch)||checkpos(6,4,2,ch))
{
if (ch == Program.computersymbol)
{
insertvalue(2, ch);
x = 2;
return true;
}
else
if(ch==Program.usersymbol)
{
x = 2;
return true;
}
}
if(checkpos(0,6,3,ch) || checkpos(5,4,3,ch))
{
if (ch == Program.computersymbol)
{
insertvalue(3, ch);
x = 3;
return true;
}
else
if(ch==Program.usersymbol)
{
x = 3;
return true;
}
}
if(checkpos(0,8,4,ch) || checkpos(2,6,4,ch)||checkpos(1,7,4,ch)||checkpos(3,5,4,ch))
{
if (ch == Program.computersymbol)
{
insertvalue(4, ch);
x = 4;
return true;
}
else
if(ch==Program.usersymbol)
{
x = 4;
return true;
}
}
if(checkpos(2,8,5,ch) || checkpos(3,4,5,ch))
{
if (ch == Program.computersymbol)
{
insertvalue(5, ch);
x = 5;
return true;
}
else
if(ch==Program.usersymbol)
{
x = 5;
return true;
}
}
if(checkpos(0,3,6,ch) || checkpos(2,4,6,ch)||checkpos(8,7,6,ch))
{
if (ch == Program.computersymbol)
{
insertvalue(6, ch);
x = 6;
return true;
}
else
if(ch==Program.usersymbol)
{
x = 6;
return true;
}
}
if(checkpos(1,4,7,ch) || checkpos(6,8,7,ch))
{
if (ch == Program.computersymbol)
{
insertvalue(7, ch);
x = 7;
return true;
}
else
if(ch==Program.usersymbol)
{
x = 7;
return true;
}
}
if(checkpos(2,5,8,ch) || checkpos(6,7,8,ch)||checkpos(0,4,8,ch))
{
if (ch == Program.computersymbol)
{
insertvalue(8, ch);
x = 8;
return true;
}
else
if(ch==Program.usersymbol)
{
x = 8;
return true;
}
}
return false;
}
public bool checkpos(int i, int j, int k, char ch1)
{
if ((box[i] == box[j] && box[i] == ch1) && (box[k] != 'x' || box[k] != 'X' || box[k] != 'o' || box[k] != 'O'))
{
return true;
}
else
{
return false;
}
}
public bool checkwin(char ch)
{
if((box[0] == box[1] && box[1]==box[2] && box[0]==ch)||(box[3]==box[4]&&box[4]==box[5]&&box[3]==ch)||(box[6]==box[7]&&box[7]==box[8]&&box[6]==ch)||((box[0]==box[3]&&box[3]==box[6]&&box[0]==ch)||
(box[1]==box[4]&&box[4]==box[7]&&box[1]==ch)||(box[2]==box[5]&&box[5]==box[8]&&box[2]==ch)||(box[0]==box[4]&&box[4]==box[8]&&box[0]==ch)||(box[6]==box[4]&&box[4]==box[2])&&box[6]==ch))
{
return true;
}
else
{
return false;
}
}
public void insertvalue(int x, char ch)
{
box[x] = ch;
switch (x)
{
case 0:
Console.SetCursorPosition(1, 0);
Console.Write(ch);
break;
case 1:
Console.SetCursorPosition(5, 0);
Console.Write(ch);
break;
case 2:
Console.SetCursorPosition(9, 0);
Console.Write(ch);
break;
case 3:
Console.SetCursorPosition(1,2);
Console.Write(ch);
break;
case 4:
Console.SetCursorPosition(5, 2);
Console.Write(ch);
break;
case 5:
Console.SetCursorPosition(9, 2);
Console.Write(ch);
break;
case 6:
Console.SetCursorPosition(1,4);
Console.Write(ch);
break;
case 7:
Console.SetCursorPosition(5, 4);
Console.Write(ch);
break;
case 8:
Console.SetCursorPosition(9,4);
Console.Write(ch);
break;
}
}
}
class Program
{
static public char usersymbol;
static public char computersymbol;
static void Main(string[] args)
{
Console.WriteLine(" to enter the symbol in any box number it from 1 to 9");
picksymbol();
Thread.Sleep(3000);
Console.Clear();
display();
resets();
gamestart gs = new gamestart();
Console.ReadLine();
}
public static void display()
{
Console.WriteLine(" " + " " +" "+ "|" + " " + " " +" "+ "|");
Console.WriteLine("_" + "_" + "_" + "|" + "_" + "_" + "_" + "|"+"_"+"_"+"_");
Console.WriteLine(" " + " " + " " + "|" + " " + " " + " " + "|");
Console.WriteLine("_" + "_" + "_" + "|" + "_" + "_" + "_" + "|" + "_" + "_" + "_");
Console.WriteLine(" " + " " + " " + "|" + " " + " " + " " + "|");
Console.WriteLine(" " + " " + " " + "|" + " " + " " + " " + "|");
}
public static void picksymbol()
{
while (true)
{
Console.WriteLine("enter the symbol-- either x or o");
usersymbol = Convert.ToChar(Console.ReadLine());
if (usersymbol == 'x' || usersymbol == 'X' || usersymbol == 'o' || usersymbol == 'O')
{
break;
}
}
if (usersymbol == 'x' || usersymbol == 'X')
{
Console.WriteLine(" you picked {0}", usersymbol);
computersymbol = 'o';
Console.WriteLine(" the computer symbol is {0}", computersymbol);
}
else
{
Console.WriteLine(" you picked {0}", usersymbol);
computersymbol = 'x';
Console.WriteLine(" the computer symbol is {0}", computersymbol);
}
}
static void resets()
{
int i,j;
for (i = 1, j = 0; j <= 4; i = i + 4)
{
Console.SetCursorPosition(i, j);
Console.Write(' ');
if (i == 9)
{
i = -3;
j = j + 2;
}
}
Console.WriteLine();
Console.WriteLine();
}
}
}
|