Latest Post

Ads

Tic-Tac-Toe Code

#include<stdio.h>
#include<string.h>
#include<malloc.h>
#include<stdlib.h>
int main()
{
char *ch;
FILE *fptr;
FILE * fp2;
char str3[100];
int i=0,e=0,j=0,q=0,temp=0,temp1=0,pt;
int n[9];
int scp1=0,scp2=0,game=0;
char a,b,c,d,e1,f,g,h,I;
char ch1,sign,sign1;
char m[4][4];
char str1[15],str2[15];
int choice,choice1,choice2;
    char name,name1;

begin:
temp = 0;
temp1 = 0;
i = 0;
e = 0;
game = 0;
scp1 = 0;
scp2 = 0;
q = 0;
j = 0;

printf("\n\n\t\t 1. START  GAME ");
printf("\n\n\t\t 2. VIEW SCORE ");
printf("\n\n\t\t 3. EXIT");
printf("\n\n\t Enter Your Choice : ");
scanf("%d",&choice);
switch(choice)

{
case 1:

printf("\n\n\t SHUNYA - CHOKDI");
printf("\n");
printf("\n \n\tBoard Positions:");
printf("\n\n\t  1  |  2  |  3 ");
printf("\n\t-----|-----|-----");
printf("\n\t  4  |  5  |  6 ");
printf("\n\t-----|-----|-----");
printf("\n\t  7  |  8  |  9 ");
printf("\n\nStart Game - Press Enter to start: ");
scanf("%c",&ch1);
printf("\n\nPlayer 1 Enter Your Name: ");
gets(str1);
printf("\n\nPlayer 2 Enter Your Name: ");
gets(str2);
sn:
printf("\n%s Select your sign: X or O ",str1);
printf("\n Sign :  ");
scanf("%c",&sign);

if (sign == 'X' || sign == 'x')
{
printf("\n\n %s your sign is X",str1);
printf("\n\n %s your sign is O",str2);
sign = 'X';
sign1 = 'O';
goto start;

}
else if (sign == 'O' || sign == 'o')
{
printf("\n\n %s your sign is O",str1);
printf("\n\n %s your sign is X",str2);
sign = 'O';
sign1 = 'X';
goto start;
}
else
{
printf("\nEnter Correct sign...");
goto sn;
}
start:
i=0;
e=0;
j=0;
q=0;
temp=0;
m[0][0]='-';
m[0][1]='-';
m[0][2]='-';
m[1][0]='-';
m[1][1]='-';
m[1][2]='-';
m[2][0]='-';
m[2][1]='-';
m[2][2]='-';

a = m[0][0];

b = m[0][1];

c = m[0][2];

d = m[1][0];

e1 = m[1][1];

f = m[1][2];

g = m[2][0];

h = m[2][1];

I = m[2][2];

n[0]=0;
n[1]=0;
n[2]=0;
n[3]=0;
n[4]=0;
n[5]=0;
n[6]=0;
n[7]=0;
n[8]=0;
j=0;
i=0;
game = temp1;;


if(temp1<3)
{

printf("\n\n\t Game : %d",temp1);
for (game=0;game<2;game++)
{

for(q=0;q<8;q++)
{
st1:
if ((a!='-')&&(b!='-')&&(c!='-')&&(d!='-')&&(e!='-')&&(f!='-')&&(g!='-')&&(h!='-')&&(I!='-'))
{
printf("\n\nNo winner");
temp1 = temp1 + 1;
game = temp1;
goto start;
}
i=temp;
printf("\n\n %s Enter position:",str1);
scanf("%d",&e);
temp = i;

for(i=0;i<9;i++)
{
if(n[i] == e)
{
printf("\n\nThis Position Already Used");
printf("\nPositions Used:  ");
for(i=0;i<9;i++)
{
printf(" %d ",n[i]);
}
printf("\n");
q=q-1;
goto st1;
}
}
i = temp;
n[i]=e;
temp = temp + 1;

printf("\n");

if (e == 1)
{
a = sign;
goto fun;
}
else if (e == 2)
{
b = sign;
goto fun;
}
else if (e == 3)
{
c = sign;
goto fun;
}
else if (e == 4)
{
d = sign;
goto fun;
}
else if (e == 5)
{
e1 = sign;
goto fun;
}
else if (e == 6)
{
f = sign;
goto fun;
}
else if (e == 7)
{
g = sign;
}
else if (e == 8)
{
h = sign;
goto fun;
}
else if (e == 9)
{
I = sign;
goto fun;
}
else
{
printf("\n\nNot a valid number.");
goto end;
}




fun:
printf("\n\t\t  %c  |  %c  |  %c ",a,b,c);
printf("\n\t\t-----|-----|-----");
printf("\n\t\t  %c  |  %c  |  %c ",d,e1,f);
printf("\n\t\t-----|-----|-----");
printf("\n\t\t  %c  |  %c  |  %c ",g,h,I);
if((a&b&c) == sign || (a&d&g) == sign || (g&h&I) ==  sign || (c&f&I) ==  sign || (a&e1&I) ==  sign || (c&e1&g) ==  sign || (b&e1&h) ==  sign ||(d&e1&f) ==  sign )
{
printf("\n\t\t\n %s Wins !!",str1);
temp1 = temp1 + 1;
game = temp1;
scp1 = scp1 + 1;
printf("\n\n\t %s your score is : %d",str1,scp1);
printf("\n\t %s your score is : %d",str2,scp2);
goto start;
}
else if((a&b&c) ==  sign1 || (a&d&g) ==  sign1 || (g&h&I) ==  sign1 || (c&f&I) ==  sign1 || (a&e1&I) ==  sign1 || (c&e1&g) ==  sign1 || (b&e1&h) ==  sign1 ||(d&e1&f) ==  sign1 )
{
printf("\n\t\t\n %s Wins !!",str2);
temp1 = temp1 + 1;
game = temp1;
scp2 = scp2 + 1;
printf("\n\n\t %s your score is : %d",str1,scp1);
printf("\n\t %s your score is : %d",str2,scp2);
goto start;
}


p2:
if ((a!='-')&&(b!='-')&&(c!='-')&&(d!='-')&&(e!='-')&&(f!='-')&&(g!='-')&&(h!='-')&&(I!='-'))
{
printf("\n\nNo winner");
temp1 = temp1 + 1;
game = temp1;
goto start;
}
i=temp;
printf("\n\n %s Enter Position:",str2);
scanf("%d",&e);
q=q+1;
temp=i;

for(i=0;i<9;i++)
{
if(n[i] == e)
{
printf("\n\nThis Position Already Used");
printf("\nPositions Used:  ");
for(i=0;i<9;i++)
{
printf(" %d ",n[i]);
}
printf("\n");
q=q-1;
goto p2;
}
}
i = temp;
temp = temp +1;
n[i]=e;

printf("\n");

if (e == 1)
{
a = sign1;
goto fun1;

}
else if (e == 2)
{
b = sign1;
goto fun1;
}
else if (e == 3)
{
c = sign1;
goto fun1;
}
else if (e == 4)
{
d = sign1;
goto fun1;
}
else if (e == 5)
{
e1 = sign1;
goto fun1;
}
else if (e == 6)
{
f = sign1;
goto fun1;
}
else if (e == 7)
{
g = sign1;
goto fun1;
}
else if (e == 8)
{
h = sign1;
goto fun1;
}
else if (e == 9)
{
I = sign1;
goto fun1;

}
else
{
printf("\n\nNot a valid number.");
goto end;
}


fun1:
printf("\n\t\t  %c  |  %c  |  %c ",a,b,c);
printf("\n\t\t-----|-----|-----");
printf("\n\t\t  %c  |  %c  |  %c ",d,e1,f);
printf("\n\t\t-----|-----|-----");
printf("\n\t\t  %c  |  %c  |  %c ",g,h,I);
if((a&b&c) == sign || (a&d&g) == sign || (g&h&I) ==  sign || (c&f&I) ==  sign || (a&e1&I) ==  sign || (c&e1&g) ==  sign || (b&e1&h) ==  sign ||(d&e1&f) ==  sign )
{
printf("\n\t\t\n %s Wins !!",str1);
temp1 = temp1 + 1;
game = temp1;
scp1 = scp1 + 1;
printf("\n\n\t %s your score is : %d",str1,scp1);
printf("\n\t %s your score is : %d",str2,scp2);
goto start;
}
else if((a&b&c) ==  sign1 || (a&d&g) ==  sign1 || (g&h&I) ==  sign1 || (c&f&I) ==  sign1 || (a&e1&I) ==  sign1 || (c&e1&g) ==  sign1 || (b&e1&h) ==  sign1 ||(d&e1&f) ==  sign1 )
{
printf("\n\t\t\n %s Wins !!",str2);
temp1 = temp1 + 1;
game = temp1;
scp2 = scp2 + 1;
printf("\n\n\t %s your score is : %d",str1,scp1);
printf("\n\t %s your score is : %d",str2,scp2);
goto start;
}

else
{
goto st1;
}


}
}
}
else if (temp1 == 3)
{
FILE *fptr = fopen("sample.txt", "a+");
if (fptr == NULL)
{
printf("Could not open file");
return 0;
}
 
fprintf(fptr,"\n\t\tPlayer 1: %s \t\t Score: %d  \n", str1,scp1);
fprintf(fptr,"\n\t\tPlayer 2: %s \t\t Score: %d  \n\n", str2,scp2);

fclose(fptr);

if (scp1 > scp2)
{
printf("\n\n\t %s YOU ARE THE WINNER....!!!!!!!",str1);
}
else if (scp2 > scp1)
{
printf("\n\n\t %s YOU ARE THE WINNER....!!!!!!!",str2);
}
else
{
printf("\n\n\t GAME DRAW......... ");
}
}
else
{
goto end;
}

printf("\n\n\n\t Do You Want to Continue ? ");
printf("\n\n\t 1. HOME");
printf("\n\n\t 2. SCORE TABLE");
printf("\n\n\t 3. EXIT");
printf("\n\n\t Enter your choice : ");
scanf("%d",&choice1);

switch(choice1)
{
case 1:
goto begin;
break;

case 2:
goto scores;
break;

case 3:
goto end;
break;
}

break;

case 2:
scores:

printf(" \n\n Score Table : \n\n");
fp2 = fopen( "sample.txt" , "r");
if (fp2)
{
while(fgets(str3, 99, fp2)!=NULL)
{
printf("\t%s",str3);
}
printf("\n");
fclose(fp2);
}


printf("\n\n\t Do You Want to Continue ? ");
printf("\n\n\t 1. HOME");
printf("\n\n\t 2. EXIT");
printf("\n\n\t Enter your choice : ");
scanf("%d",&choice2);

switch(choice2)
{
case 1:
goto begin;
break;

case 2:
goto end;
break;
}

break;

case 3:
goto end;
break;

default:
printf("\n\t Enter Valid Choice : ");
goto begin;
break;
}


end:
return 0;
}


C Programs:
------------------------
|  Basic Programs  
|  Loop Programs  
|  Matrix Programs  
|  Array Programs  
|  String Programs  
|  File Handling  
|  Other Programs  
|  Graphics in C   
|  Pattern Programs
------------------------

Ads

Popular posts from this blog

VLSI: BCD to Excess 3 and Excess 3 to BCD Dataflow Modelling

Full Subtractor Verilog Code in Structural/Gate Level Modelling with Testbench

VLSI: 4-1 MUX Dataflow Modelling with Testbench

VLSI: Half Subtractor and Full Subtractor Gate Level Modelling

VLSI: 1-4 DEMUX (Demultiplexer) Dataflow Modelling with Testbench