Tic tac tic game
#include<stdio.h>
#include<conio.h>
int gameover=1;
char a[9] = {'1','2','3','4','5','6','7','8','9'};
char p1name[20],p2name[20];
char symbol1='x',symbol2 = '0';
int player1,player2,ch = 0;
void input();
void show();
void win();
int main(){
clrscr();
printf("enter player 1 name : ");
scanf("%s",p1name);
printf("enter player 2 name : ");
scanf("%s",p2name);
while(gameover!= 0){
clrscr();
show();
input();
win();
}
clrscr();
show();
win();
getch();
return 0;
}
void show(){
printf("\t\t\t\t TIC TAC TIC\n");
printf("\n\n");
printf("\t\t\t--|--|--\n");
printf("\t\t\t%c |%c |%c \n",a[0],a[1],a[2]);
printf("\t\t\t--|--|--\n");
printf("\t\t\t%c |%c |%c \n",a[3],a[4],a[5]);
printf("\t\t\t--|--|--\n");
printf("\t\t\t%c |%c |%c \n",a[6],a[7],a[8]);
printf("\t\t\t--|--|--\n");
printf("\n%s: x",p1name);
printf("\n%s : 0",p2name);
}
void input()
{
int i ;
if (ch == 0)
{
printf("\n%s enter your choice",p1name);
scanf("%d",&player1);
ch = 1;
}
else
{
printf("\n%s enter your choice",p2name);
scanf("%d",&player2);
ch = 0;
}
if (ch == 1)
{
for(i=0;i<9;i++)
{
if(i== player1-1)
{
a[i]=symbol1;
}
}
}
else
{
for(i=0;i<9;i++)
{
if(i== player2-1)
{
a[i]=symbol2;
}
}
}
}
void win(){
//012,345,678,036,147,258,048,246
if ( a[0] == 'x'&&a[1] == 'x'&&a[2]=='x')
{
printf("\n%s win the math",p1name);
gameover = 0;
}
else if(a[3] == 'x'&&a[4] == 'x'&&a[5]=='x' ){
printf("\n%s win the math",p1name);
gameover = 0 ;
}
else if ( a[6] == 'x'&&a[7] == 'x'&&a[8]=='x')
{
printf("\n%s win the math",p1name);
gameover = 0;
}
else if ( a[0] == 'x'&&a[3] == 'x'&&a[6]=='x')
{
printf("\n%s win the math",p1name);
gameover = 0;
}
else if ( a[1] == 'x'&&a[4] == 'x'&&a[7]=='x')
{
printf("\n%s win the math",p1name);
gameover = 0;
}
else if ( a[2] == 'x'&&a[5] == 'x'&&a[8]=='x')
{
printf("\n%s win the math",p1name);
gameover = 0;
}
else if ( a[0] == 'x'&&a[4] == 'x'&&a[8]=='x')
{
printf("\n%s win the math",p1name);
gameover = 0;
}
else if ( a[2] == 'x'&&a[4] == 'x'&&a[6]=='x')
{
printf("\n%s win the mathh",p1name);
gameover = 0;
}
//player2
if ( a[0] == '0'&&a[1] == '0'&&a[2]=='0')
{
printf("\n%s win the math",p2name);
gameover = 0;
}
else if(a[3] == '0'&&a[4] == '0'&&a[5]=='0' ){
printf("\n%s win the math",p2name);
gameover = 0 ;
}
else if ( a[6] == '0'&&a[7] == '0'&&a[8]=='0')
{
printf("\n%s win the math",p2name);
gameover = 0 ;
}
else if ( a[0] == '0'&&a[3] == '0'&&a[6]=='0')
{
printf("\n%s win the math",p2name);
gameover = 0 ;
}
else if ( a[1] == '0'&&a[4] == '0'&&a[7]=='0')
{
printf("\n%s win the math",p2name);
gameover = 0 ;
}
else if ( a[2] == '0'&&a[5] == '0'&&a[8]=='0')
{
printf("\n%s win the math",p2name);
gameover = 0 ;
}
else if ( a[0] == '0'&&a[4] == '0'&&a[8]=='0')
{
printf("\n%s win the math",p2name);
gameover = 0 ;
}
else if ( a[2] == '0'&&a[4] == '0'&&a[6]=='0')
{
printf("\n%s win the math",p2name);
gameover = 0;
}
//123,456,789,147,258,369,159,357
//012,345,678,036,147,258,048,246
}
Comments
Post a Comment