#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
#include <dos.h>
#include <string.h>
#include <stdio.h>
#include <graphics.h>
#include <process.h>
char board [3] [3];
void rules(void);
void help(void);
void play(void);
void restart(void);
void credits(void);
void draw(void);
void printplayer(int);
void printcomp(int);
void result(void);
char name[100];
void input(void);
void input2(void);
void mapboard(void);
void main()
{
int ch;
top:
cout<<"\nEnter your name:\n";
cin.getline(name,100);
clrscr();
gotoxy(10,2);
cout<<"This program is a program for the game \"TIC TAC TOE\"\n\n";
cout<<"1. Rules\n"
<<"2. Help\n"
<<"3. Play with computer\n"
<<"4. Play with two player\n"
<<"5. Restart\n"
<<"6. Credits\n"
<<"7. Quit\n";
cin>>ch;
switch(ch)
{
case(1):
rules();
goto top;
case(2):
help();
goto top;
case(3):
draw();
play();
result();
break;
case(4):
draw();
input();
input2();
result();
break;
case(5):
restart();
goto top;
case(6):
credits();
goto top;
case(7):
exit(0);
default:
play();
break;
}
cout<<"\nPress any key to continue\t";
getch();
}
void rules(void)
{
cout<<"The rules of the game are as follows:-\n\n"
<<"Press the number indicated near the playing box \n"
<<"to put the symbol \" X \" in the playing box.\n"
<<"\nDo not press a number in which a symbol such as\n"
<<"\"'X' or 'O'\" already exist. If you do so the\n"
<<"program will terminate due to illegal inputs\n"
<<"\nYour aim is to get a line containing 3 'X'\n"
<<"either hrizontally or vertically or diagonally\n"
<<"with alternative inputs by you and computer\n"
<<"\npress any key to continue\t";
getch();
}
void help(void)
{
cout<<"As the rule of the game get a line containing 3 'X'\n"
<<"either hrizontally or vertically or diagonally\n"
<<"with alternative inputs by you and computer.\n"
<<"\nAlways try to occupy the middle box and\n"
<<"then the corners for confusing the computer\n"
<<"\nPress any key to continue\t";
getch();
}
int place[9]={0,0,0,
0,0,0,
0,0,0};
int flag[9]={0,0,0,
0,0,0,
0,0,0};
void play(void)
{
int i,j,k,l;
int print;
int win;
static int first=0;
static int one23=0;
static int four56=0;
static int seven89=0;
static int one47=0;
static int two58=0;
static int three69=0;
static int one59=0;
static int three57=0;
gotoxy(10,20);
for(i=0;i<9;i+=2)
{
cin>>place[i];
print=0;
if(flag[(place[i]-1)]!=0)
{
cout<<"Read the rules before you play again\n";
cout<<"Sorry press any key to exit\t";
getch();
exit(0);
}
printplayer(place[i]);
flag[place[i]-1]=1;
if(place[0]==5 && flag[0]==0)
{
printcomp(1);
flag[0]=2;
print++;
first=1;
}
if(place[0]!=5 && first==0)
{
printcomp(5);
flag[4]=2;
print++;
first=1;
}
for(win=0;win<=6;win+=3)
{
if(flag[0+win]==0 && flag[1+win]==2 && flag[2+win]==2)
{
printcomp(1+win);
flag[0+win]=2;
print++;
if(win==0)
one23=1;
if(win==3)
four56=1;
if(win==6)
seven89=1;
result();
}
if(flag[0+win]==2 && flag[1+win]==0 && flag[2+win]==2)
{
printcomp(2+win);
flag[1+win]=2;
print++;
if(win==0)
one23=1;
if(win==3)
four56=1;
if(win==6)
seven89=1;
result();
}
if(flag[0+win]==2 && flag[1+win]==2 && flag[2+win]==0)
{
printcomp(3+win);
flag[2+win]=2;
print++;
if(win==0)
one23=1;
if(win==3)
four56=1;
if(win==6)
seven89=1;
result();
}
}
for(win=0;win<=2;win++)
{
if(flag[0+win]==0 && flag[3+win]==2 && flag[6+win]==2)
{
printcomp(1+win);
flag[0+win]=2;
print++;
if(win==0)
one47=1;
if(win==1)
two58=1;
if(win==2)
three69=1;
result();
}
if(flag[0+win]==2 && flag[3+win]==0 && flag[6+win]==2)
{
printcomp(4+win);
flag[3+win]=2;
print++;
if(win==0)
one47=1;
if(win==1)
two58=1;
if(win==2)
three69=1;
result();
}
if(flag[0+win]==2 && flag[3+win]==2 && flag[6+win]==0)
{
printcomp(7+win);
flag[6+win]=2;
print++;
if(win==0)
one47=1;
if(win==1)
two58=1;
if(win==2)
three69=1;
result();
}
}
for(win=0;win<=2;win+=2)
{
if(flag[0+win]==0 && flag[4]==2 && flag[8-win]==2)
{
printcomp(1+win);
flag[0+win]=2;
print++;
if(win==0)
one59=1;
if(win==2)
three57=1;
result();
}
if(flag[0+win]==2 && flag[4]==0 && flag[8-win]==2)
{
printcomp(5);
flag[4]=2;
print++;
if(win==0)
one59=1;
if(win==2)
three57=1;
result();
}
if(flag[0+win]==2 && flag[4]==2 && flag[8-win]==0)
{
printcomp(9-win);
flag[8-win]=2;
print++;
if(win==0)
one59=1;
if(win==2)
three57=1;
result();
}
}
for(k=0;k<=4;k+=4) //Loop for testing 1 5 9 output
for(j=0;j<=4;j+=4)
if((place[0]==1+j-k && place[2]==5+j||
place[0]==1+j-k && place[4]==5+j||
place[0]==1+j-k && place[6]==5+j||
place[0]==1+j-k && place[8]==5+j||
place[2]==1+j-k && place[0]==5+j||
place[2]==1+j-k && place[4]==5+j||
place[2]==1+j-k && place[6]==5+j||
place[2]==1+j-k && place[8]==5+j||
place[4]==1+j-k && place[0]==5+j||
place[4]==1+j-k && place[2]==5+j||
place[4]==1+j-k && place[6]==5+j||
place[4]==1+j-k && place[8]==5+j||
place[6]==1+j-k && place[0]==5+j||
place[6]==1+j-k && place[2]==5+j||
place[6]==1+j-k && place[4]==5+j||
place[6]==1+j-k && place[8]==5+j||
place[6]==1+j-k && place[8]==5+j||
place[6]==1+j-k && place[8]==5+j||
place[8]==1+j-k && place[0]==5+j||
place[8]==1+j-k && place[2]==5+j||
place[8]==1+j-k && place[4]==5+j||
place[8]==1+j-k && place[6]==5+j)
&& one59==0 && print==0)
{
one59=1;
if(j==0 && k==0 && flag[8]==0)
{
printcomp(9);
flag[8]=2;
print++;
}
if(j==4 && k==0 && flag[0]==0)
{
printcomp(1);
flag[0]=2;
print++;
}
if(j==4 && k==4 && flag[4]==0)
{
printcomp(5);
flag[4]=2;
print++;
}
}
for(k=0;k<=2;k+=2)
for(j=0;j<=2;j+=2)
if((place[0]==3+j-k && place[2]==5+j||
place[0]==3+j-k && place[4]==5+j||
place[0]==3+j-k && place[6]==5+j||
place[0]==3+j-k && place[8]==5+j||
place[2]==3+j-k && place[0]==5+j||
place[2]==3+j-k && place[4]==5+j||
place[2]==3+j-k && place[6]==5+j||
place[2]==3+j-k && place[8]==5+j||
place[4]==3+j-k && place[0]==5+j||
place[4]==3+j-k && place[2]==5+j||
place[4]==3+j-k && place[6]==5+j||
place[4]==3+j-k && place[8]==5+j||
place[6]==3+j-k && place[0]==5+j||
place[6]==3+j-k && place[2]==5+j||
place[6]==3+j-k && place[4]==5+j||
place[6]==3+j-k && place[8]==5+j||
place[6]==3+j-k && place[8]==5+j||
place[6]==3+j-k && place[8]==5+j||
place[8]==3+j-k && place[0]==5+j||
place[8]==3+j-k && place[2]==5+j||
place[8]==3+j-k && place[4]==5+j||
place[8]==3+j-k && place[6]==5+j)
&& three57==0 && print==0)
{
three57=1;
if(j==0 && k==0 && flag[6]==0)
{
printcomp(7);
flag[6]=2;
print++;
}
if(j==2 && k==0 && flag[2]==0)
{
printcomp(3);
flag[2]=2;
print++;
}
if(j==2 && k==2 && flag[4]==0)
{
printcomp(5);
flag[4]=2;
print++;
}
}
for(l=0;l<7;l+=3)
for(k=0;k<2;k++)
for(j=0;j<2;j++)
if((place[0]==1+j-k+l && place[2]==2+j+l||
place[0]==1+j-k+l && place[4]==2+j+l||
place[0]==1+j-k+l && place[6]==2+j+l||
place[0]==1+j-k+l && place[8]==2+j+l||
place[2]==1+j-k+l && place[0]==2+j+l||
place[2]==1+j-k+l && place[4]==2+j+l||
place[2]==1+j-k+l && place[6]==2+j+l||
place[2]==1+j-k+l && place[8]==2+j+l||
place[4]==1+j-k+l && place[0]==2+j+l||
place[4]==1+j-k+l && place[2]==2+j+l||
place[4]==1+j-k+l && place[6]==2+j+l||
place[4]==1+j-k+l && place[8]==2+j+l||
place[6]==1+j-k+l && place[0]==2+j+l||
place[6]==1+j-k+l && place[2]==2+j+l||
place[6]==1+j-k+l && place[4]==2+j+l||
place[6]==1+j-k+l && place[6]==2+j+l||
place[8]==1+j-k+l && place[0]==2+j+l||
place[8]==1+j-k+l && place[2]==2+j+l||
place[8]==1+j-k+l && place[4]==2+j+l||
place[8]==1+j-k+l && place[6]==2+j+l)
&& (one23==0) && (four56==0)
&& (seven89)==0 && print==0)
{
if(j==0 && k==0 && l==0 && flag[2]==0)
{
printcomp(3);
flag[2]=2;
print++;
one23=1;
}
if(j==1 && k==0 && l==0 && flag[0]==0)
{
printcomp(1);
flag[0]=2;
print++;
one23=1;
}
if(j==1 && k==1 && l==0 && flag[1]==0)
{
printcomp(2);
flag[1]=2;
print++;
one23=1;
}
if(j==0 && k==0 && l==3 && flag[5]==0)
{
printcomp(6);
flag[5]=2;
print++;
four56=1;
}
if(j==1 && k==0 && l==3 && flag[3]==0)
{
printcomp(4);
flag[3]=2;
print++;
four56=1;
}
if(j==1 && k==1 && l==3 && flag[4]==0)
{
printcomp(5);
flag[4]=2;
print++;
four56=1;
}
if(j==0 && k==0 && l==6 && flag[8]==0)
{
printcomp(9);
flag[8]=2;
print++;
seven89=1;
}
if(j==1 && k==0 && l==6 && flag[6]==0)
{
printcomp(7);
flag[6]=2;
print++;
seven89=1;
}
if(j==1 && k==1 && l==6 && flag[7]==0)
{
printcomp(8);
flag[7]=2;
print++;
seven89=1;
}
}
for(l=0;l<3;l++)
for(k=0;k<4;k+=3)
for(j=0;j<4;j+=3)
if((place[0]==1+j-k+l && place[2]==4+j+l||
place[0]==1+j-k+l && place[4]==4+j+l||
place[0]==1+j-k+l && place[6]==4+j+l||
place[0]==1+j-k+l && place[8]==4+j+l||
place[2]==1+j-k+l && place[0]==4+j+l||
place[2]==1+j-k+l && place[4]==4+j+l||
place[2]==1+j-k+l && place[6]==4+j+l||
place[2]==1+j-k+l && place[8]==4+j+l||
place[4]==1+j-k+l && place[0]==4+j+l||
place[4]==1+j-k+l && place[2]==4+j+l||
place[4]==1+j-k+l && place[6]==4+j+l||
place[4]==1+j-k+l && place[8]==4+j+l||
place[6]==1+j-k+l && place[0]==4+j+l||
place[6]==1+j-k+l && place[2]==4+j+l||
place[6]==1+j-k+l && place[4]==4+j+l||
place[6]==1+j-k+l && place[8]==4+j+l||
place[8]==1+j-k+l && place[0]==4+j+l||
place[8]==1+j-k+l && place[2]==4+j+l||
place[8]==1+j-k+l && place[4]==4+j+l||
place[8]==1+j-k+l && place[6]==4+j+l)
&& (one47==0) && (two58==0)
&& (three69==0) && print==0)
{
if(j==0 && k==0 && l==0 && flag[6]==0)
{
printcomp(7);
flag[6]=2;
print++;
one47=1;
}
if(j==3 && k==0 && l==0 && flag[0]==0)
{
printcomp(1);
flag[0]=2;
print++;
one47=1;
}
if(j==3 && k==3 && l==0 && flag[3]==0)
{
printcomp(4);
flag[3]=2;
print++;
one47=1;
}
if(j==0 && k==0 && l==1 && flag[7]==0)
{
printcomp(8);
flag[7]=2;
print++;
two58=1;
}
if(j==3 && k==0 && l==1 && flag[1]==0)
{
printcomp(2);
flag[1]=2;
print++;
two58=1;
}
if(j==3 && k==3 && l==1 && flag[4]==0)
{
printcomp(5);
flag[4]=2;
print++;
two58=1;
}
if(j==0 && k==0 && l==2 && flag[8]==0)
{
printcomp(9);
flag[8]=2;
print++;
three69=1;
}
if(j==3 && k==0 && l==2 && flag[2]==0)
{
printcomp(3);
flag[2]=2;
print++;
three69=1;
}
if(j==3 && k==3 && l==2 && flag[5]==0)
{
printcomp(6);
flag[5]=2;
print++;
three69=1;
}
}
if(print==0 && flag[0]==0)
{
printcomp(1);
flag[0]=2;
print++;
}
if(print==0 && flag[1]==0)
{
printcomp(2);
flag[1]=2;
print++;
}
if(print==0 && flag[2]==0)
{
printcomp(3);
flag[2]=2;
print++;
}
if(print==0 && flag[3]==0)
{
printcomp(4);
flag[3]=2;
print++;
}
if(print==0 && flag[4]==0)
{
printcomp(5);
flag[4]=2;
print++;
}
if(print==0 && flag[5]==0)
{
printcomp(6);
flag[5]=2;
print++;
}
if(print==0 && flag[6]==0)
{
printcomp(7);
flag[6]=2;
print++;
}
if(print==0 && flag[7]==0)
{
printcomp(8);
flag[7]=2;
print++;
}
if(print==0 && flag[8]==0)
{
printcomp(9);
flag[8]=2;
print++;
}
if(print==0 && flag[0]==0)
{
gotoxy(60,22);
cout<<"Sorry game cannot advance\n";
gotoxy(60,23);
cout<<"press any key to exit\n";
getch();
exit(0);
}
//Checks for player's winning condition
if(flag[0]==1 && flag[1]==1 && flag[2]==1||
flag[3]==1 && flag[4]==1 && flag[5]==1||
flag[6]==1 && flag[7]==1 && flag[8]==1||
flag[0]==1 && flag[3]==1 && flag[6]==1||
flag[1]==1 && flag[4]==1 && flag[7]==1||
flag[2]==1 && flag[5]==1 && flag[8]==1||
flag[1]==1 && flag[4]==1 && flag[8]==1||
flag[2]==1 && flag[4]==1 && flag[6]==1)
result();
}
void PlayerX()
{
int row,col;
if(win==1)
return;
printf("\nEnter the row no. : ");
fflush(stdin);
scanf("%d",&row);
printf("Enter the column no. : ");
fflush(stdin);
scanf("%d",&col);
if(pos_marked[row][col]==1 || row<1 || row>3 || col<1 || col>3)
{
printf("\nWRONG POSITION!! Press any key.....");
wrong_X=1;
getch();
Board();
}
else
{
pos_for_X[row][col]=1;
pos_marked[row][col]=1;
Board();
}
}
void PlayerO()
{
int row,col;
if(win==1)
return;
printf("\nEnter the row no. : ");
scanf("%d",&row);
printf("Enter the column no. : ");
scanf("%d",&col);
if(pos_marked[row][col]==1 || row<1 || row>3 || col<1 || col>3)
{
printf("\nWRONG POSITION!! Press any key....");
wrong_O=1;
getch();
Board();
}
else
{
pos_for_O[row][col]=1;
pos_marked[row][col]=1;
Board();
}
}
void Player_win()
{
int i;
for(i=1;i<=3;i++)
{
if(pos_for_X[i][1]==1 && pos_for_X[i][2]==1 && pos_for_X[i][3]==1)
{
win=1;
printf("\n\nRESULT: %s wins!!",name_X);
printf("\nPress any key............");
return;
}
}
for(i=1;i<=3;i++)
{
if(pos_for_X[1][i]==1 && pos_for_X[2][i]==1 && pos_for_X[3][i]==1)
{
win=1;
printf("\n\nRESULT: %s wins!!",name_X);
printf("\nPress any key............");
return;
}
}
if(pos_for_X[1][1]==1 && pos_for_X[2][2]==1 && pos_for_X[3][3]==1)
{
win=1;
printf("\n\nRESULTL: %s wins!!",name_X);
printf("\nPress any key......");
return;
}
else if(pos_for_X[1][3]==1 && pos_for_X[2][2]==1 &&
pos_for_X[3][1]==1)
{
win=1;
printf("\n\nRESULT: %s wins!!",name_X);
printf("\nPress any key.....");
return;
}
for(i=1;i<=3;i++)
{
if(pos_for_O[i][1]==1 && pos_for_O[i][2]==1 && pos_for_O[i][3]==1)
{
win=1;
printf("\n\nRESULT: %s wins!!",name_O);
printf("\nPress any key.....");
return;
}
}
for(i=1;i<=3;i++)
{
if(pos_for_O[1][i]==1 && pos_for_O[2][i]==1 && pos_for_O[3][i]==1)
{
win=1;
printf("\n\nRESULT: %s wins!!",name_O);
printf("\nPress any key.....");
return;
}
}
if(pos_for_O[1][1]==1 && pos_for_O[2][2]==1 && pos_for_O[3][3]==1)
{
win=1;
printf("\n\nRESULT: %s wins!!",name_O);
printf("\nPress any key.....");
return;
}
else if(pos_for_O[1][3]==1 && pos_for_O[2][2]==1 &&
pos_for_O[3][1]==1)
{
win=1;
printf("\n\nRESULT: %s wins!!",name_O);
printf("\nPress any key.....");
return;
}
}
void check()
{
int i,j;
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
if(pos_marked[i][j]==1)
chk++;
else
continue;
}
}}
void printplayer(int place)
{
if(place==1)
{
gotoxy(30,6);
cout<<"X";
gotoxy(10,20);
}
else if(place==2)
{
gotoxy(40,6);
cout<<"X";
gotoxy(10,20);
}
else if(place==3)
{
gotoxy(50,6);
cout<<"X";
gotoxy(10,20);
}
else if(place==4)
{
gotoxy(30,11);
cout<<"X";
gotoxy(10,20);
}
else if(place==5)
{
gotoxy(40,11);
cout<<"X";
gotoxy(10,20);
}
else if(place==6)
{
gotoxy(50,11);
cout<<"X";
gotoxy(10,20);
}
else if(place==7)
{
gotoxy(30,16);
cout<<"X";
gotoxy(10,20);
}
else if(place==8)
{
gotoxy(40,16);
cout<<"X";
gotoxy(10,20);
}
else if(place==9)
{
gotoxy(50,16);
cout<<"X";
gotoxy(10,20);
}
}
void printcomp(int place)
{
if(place==1)
{
gotoxy(30,6);
cout<<"O";
gotoxy(10,20);
}
else if(place==2)
{
gotoxy(40,6);
cout<<"O";
gotoxy(10,20);
}
else if(place==3)
{
gotoxy(50,6);
cout<<"O";
gotoxy(10,20);
}
else if(place==4)
{
gotoxy(30,11);
cout<<"O";
gotoxy(10,20);
}
else if(place==5)
{
gotoxy(40,11);
cout<<"O";
gotoxy(10,20);
}
else if(place==6)
{
gotoxy(50,11);
cout<<"O";
gotoxy(10,20);
}
else if(place==7)
{
gotoxy(30,16);
cout<<"O";
gotoxy(10,20);
}
else if(place==8)
{
gotoxy(40,16);
cout<<"O";
gotoxy(10,20);
}
else if(place==9)
{
gotoxy(50,16);
cout<<"O";
gotoxy(10,20);
}
}
void result(void)
{
int l;
l=strlen(name);
//Checks for user's winning condition
if(flag[0]==1 && flag[1]==1 && flag[2]==1||
flag[3]==1 && flag[4]==1 && flag[5]==1||
flag[6]==1 && flag[7]==1 && flag[8]==1||
flag[0]==1 && flag[3]==1 && flag[6]==1||
flag[1]==1 && flag[4]==1 && flag[7]==1||
flag[2]==1 && flag[5]==1 && flag[8]==1||
flag[0]==1 && flag[4]==1 && flag[8]==1||
flag[2]==1 && flag[4]==1 && flag[6]==1)
{
gotoxy(40,22);
cout.write(name,l);
cout<<" You are victorious\n";
cout<<"\n\npress any key to continue\t";
getch();
}
else
{
gotoxy(40,22);
cout.write(name,l);
cout<<" Sorry better luck next time\n";
cout<<"\n\npress any key to continue\t";
getch();
}
exit(0);
}
/*********************************/
/***** restarting function *****/
/*********************************/
void restart(void)
{
sleep(1);
}
void credits(void)
{
clrscr();
cout<<"Thabk you sa pc ng pinsan q haha š\n\n"
<<"Created in \"BORLAND C++\" version 5.02\n\n"
<<"created by JOHOLLIE TANGHAL and IMO AGUSTIN\n\n"
<<"Press any key to continue\t";
getch();
}
void draw(void)
{
int i,j;
int num=1;
char h=205;
char v=186;
char c=206;
for(i=6;i<12;i+=5)
{
gotoxy(25,i+3);
for(j=0;j<=30;j++)
cout<<h;
}
for(j=10;j<=20;j+=10)
{
for(i=2;i<17;i++)
{
gotoxy(25+j,i+3);
cout<<v;
}
}
for(i=35;i<=45;i+=10)
for(j=9;j<=14;j+=5)
{
gotoxy(i,j);
cout<<c;
}
for(j=0;j<=10;j+=5)
for(i=0;i<=20;i+=10)
{
gotoxy(34+i,8+j);
cout<<num++;
}
} };
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.