0% found this document useful (0 votes)
37 views

Int X (Int) Int y (Int) Int Check (Int ) Char Gamehault (Char (Void Main Char CHD Do Int Teryp1

This C++ program implements a tic-tac-toe game that can be played between two players or a player versus the computer. The program uses functions to check for valid moves, determine row/column positions, and check for a winner. It displays a 3x3 grid for play and prompts for symbol and turn order selection before beginning the game loop where players take turns until there is a winner or draw.

Uploaded by

Deep
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Int X (Int) Int y (Int) Int Check (Int ) Char Gamehault (Char (Void Main Char CHD Do Int Teryp1

This C++ program implements a tic-tac-toe game that can be played between two players or a player versus the computer. The program uses functions to check for valid moves, determine row/column positions, and check for a winner. It displays a 3x3 grid for play and prompts for symbol and turn order selection before beginning the game loop where players take turns until there is a winner or draw.

Uploaded by

Deep
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

#include<iostream.

h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
int x(int);
int y(int);
int check(int []);
char gamehault(char [4][4]);
void main()
{
char chd;
do
{
int teryp1=0,teryp2=0;
clrscr();
cout<<"-------------TIC-TAC-TOE GAME------------"<<endl;
cout<<" ________________ ";
cout<<"\n";
char h;
cout<<"choose your symbol (X or O) :";
cin>>h;
int a;
cout<<"choose with whom to start ";
cout<<"\n 1. Computer 2. Player :";
la1 :cin>>a;
if (a==1)
{
char r;
if(h=='X'){r='O';}
else{r='X';}
randomize();
cout<<"you are to play with reference to this square grid" ;
char ar[4][4];
ar[0][0]='\0';
ar[0][1]='\0';
ar[0][2]='\0';
ar[1][0]='\0';
ar[1][1]='\0';
ar[1][2]='\0';
ar[2][0]='\0';
ar[2][1]='\0';
ar[2][2]='\0';
cout<<"\n-------------"<<endl;
for(int i=0;i<3;++i)
{
cout<<"|";
for(int j=0;j<3;j++)
{
cout<<" "<<3*i+j+1<<" "<<"|";
}
cout<<"\n-------------"<<endl;
}
cout<<"just type the number of the square where you want to put your "<<char(h);
cout<<"\nthe result is shown after each player plays";
int q[9];
for(int d=0;d<9;d++)
{
q[d]=0;
}
for(int j=0;j<9;++j)
{
if(j%2==0)
{
l2 : q[j]=random(9)+1;
if(check(q)==1)
{
cout<<"\ncomputer : "<<q[j];
ar[y(q[j])][x(q[j])]=r;
cout<<"\n\nresult : "<<endl;
cout<<"-------------"<<endl;
for(int k=0;k<3;++k)
{
cout<<"|";
for(int l=0;l<3;++l)
{
cout<<" "<<char(ar[k][l])<<" "<<"|";
}
cout<<"\n-------------"<<endl;
}
}
else {goto l2;}
}
else
{
cout<<"\nplayer : ";
l3 : cin>>q[j];
if(check(q)==1)
{
ar[y(q[j])][x(q[j])]=h;
cout<<"\nresult : "<<endl;
cout<<"-------------"<<endl;
for(int k=0;k<3;++k)
{
cout<<"|";
for(int l=0;l<3;++l)
{
cout<<" "<<char(ar[k][l])<<" "<<"|";
}
cout<<"\n-------------"<<endl;
}
}
else
{
cout<<"respond again since your number was chosen previously : ";
goto l3;
}
}
if(gamehault(ar)==h)
{
cout<<"***** gameover! player wins *****";
++teryp1;
break;
}
else if(gamehault (ar)==r)
{
cout<<"***** gameover! computer wins *****";
++teryp1;
break;
}
else
{
continue;
}
}
if(teryp1==0){cout<<"***** gameover! match drawn*****"; }
}
else if (a==2)
{
char r;
if(h=='X'){r='O';}
else{r='X';}
randomize();
cout<<"you are to play with reference to this square grid";
char ar[4][4];
ar[0][0]='\0';
ar[0][1]='\0';
ar[0][2]='\0';
ar[1][0]='\0';
ar[1][1]='\0';
ar[1][2]='\0';
ar[2][0]='\0';
ar[2][1]='\0';
ar[2][2]='\0';
cout<<"\n-------------"<<endl;
for(int i=0;i<3;++i)
{
cout<<"|";
for(int j=0;j<3;j++)
{
cout<<" "<<3*i+j+1<<" "<<"|";
}
cout<<"\n-------------"<<endl;
}
cout<<"just type the number of the square where you want to put your "<<char(h);
cout<<"\nthe result is shown after each player plays";
int q[9];
for(int d=0;d<9;d++)
{
q[d]=0;
}
for(int j=0;j<9;++j)
{
if(j%2==0)
{
cout<<"\nplayer : ";
l6 : cin>>q[j];
if(check(q)==1)
{
ar[y(q[j])][x(q[j])]=h;
cout<<"\nresult : "<<endl;
cout<<"-------------"<<endl;
for(int k=0;k<3;++k)
{
cout<<"|";
for(int l=0;l<3;++l)
{
cout<<" "<<char(ar[k][l])<<" "<<"|";
}
cout<<"\n-------------"<<endl;
}
}
else
{
cout<<"respond again since your number was chosen previously : ";
goto l6;
}
}
else
{
l7 : q[j]=random(9)+1;
if(check(q)==1)
{
cout<<"\ncomputer : "<<q[j];
ar[y(q[j])][x(q[j])]=r;
cout<<"\n\nresult : "<<endl;
cout<<"-------------"<<endl;
for(int k=0;k<3;++k)
{
cout<<"|";
for(int l=0;l<3;++l)
{
cout<<" "<<char(ar[k][l])<<" "<<"|";
}
cout<<"\n-------------"<<endl;
}
}
else {goto l7;}
}
if(gamehault(ar)==h)
{
cout<<"***** gameover! player wins *****";
++teryp2;
break;
}
else if(gamehault (ar)==r)
{
cout<<"***** gameover! computer wins *****";
++teryp2;
break;
}
else
{
continue;
}
}
if(teryp2==0){cout<<"***** gameover! match drawn*****"; }
}
else{
cout<<"choose between 1 & 2 only : ";
goto la1;
}
cout<<"\nDo you want to play again (Y/y for Yes & N/n for No) ? ";
cin>>chd;
}
while(chd=='y'||chd=='Y');
getch();
}
int check(int as[9])
{
int res=0;
for(int i=0;i<9;++i)
{
if(as[i]!=0)
{
for(int j=i+1;j<9;++j)
{
if(as[i]==as[j])
{ ++res; }
}
}
}
if(res!=0){return 0;}
else{return 1;}
}
int y(int n)
{
return (n-1)/3;
}
int x(int n)
{
return (n-1)-3*y(n);
}
char gamehault(char er[4][4])
{
int q=0;
char g;
for(int i=0;i<3;++i)
{
if( er[i][0]!='\0')
{
if( er[i][0]==er[i][1] && er[i][1]==er[i][2] )
{
++q;
g=er[i][0];
}
}
if( er[0][i]!='\0')
{
if( er[0][i]==er[1][i] && er[1][i]==er[2][i] )
{
++q;
g=er[0][i];
}
}
}
if(er[0][0]==er[1][1] && er[1][1]==er[2][2])
{
++q;
g=er[1][1];
}
if(er[0][2]==er[1][1] && er[1][1]==er[2][0])
{
++q;
g=er[1][1];
}
if(q!=0){return g;}
else {return 'N';}
}

You might also like