| ||||||||||||||||||||||
| ||||||||||||||||||||||
Sunday, March 21, 2010
The open source, cross platform, free C++ IDE.
Saturday, March 20, 2010
Tick tack toe in C language
#include
#include
#include
//using namespace std;
struct player
{
char name1[20];
char name2[20];
int score1;
int score2;
};
char A[3][3] = {49,50,51,52,53,54,55,56,57};
int a=0,p=0,c,z=1,t=1,x,y,l=0,h=0;
FILE *fp;
void ahmed();
int test1(int);
int test2(int);
void test();
void display();
void cheat1();
void cheat2();
void display_about();
void display_rules();
struct player n;
void loop();
int main()
{
n.score1=100;
n.score2=100;
int play;
fp=fopen("score.txt","a+");
system("color f0");
display_about();
system("color 16");
display_rules();
system("color f0");
printf("Enter your choice");
printf("Press 1 for playing");
scanf("%d",&play);
if(l=1)
{
printf("Enter the first player name");
scanf("%s",n.name1);
printf("Enter the 2nd player name");
scanf("%s",n.name2);
loop();
ahmed();
printf("Hahaha");
fclose(fp);
fp=fopen("score.txt","r");
if(fp==NULL)
{
puts("cannot open");
exit(1);
}
else
{
rewind(fp);
while(fscanf(fp,"%s %d %s %d",n.name1,&n.score1,n.name2,&n.score2)!=EOF);
{
printf("%s %d %s %d",n.name1,n.score1,n.name2,n.score2);
}}
system("pause");
fclose(fp);
}
return 0;
}
void loop()
{
while(1)
{
h=0;
system("cls");
display ();
printf("%s\t",n.name1);
scanf("%d",&x);
test1(x);
test();
if (h==2)
continue;
l++;
if (p==1)
break;
if (l==5)
break;
int g=4;
while (g==4)
{
h=0;
system("cls");
display();
printf("%s\t",n.name2);
scanf("%d",&y);
test2(y);
test();
if (p==1)
break;
if (h==3)
continue;
else
g=6;
}
if (p==1)
break;
system("cls");
display();
}
}
void display()
{
//--------------------------------------------------------------------
printf("\n");
printf( "\t\t\t -------------------------\n");
printf("\t\t\t | | | |\n");
printf("\t\t\t | %c | %c | %c |\n",A[0][0],A[0][1],A[0][2]);
printf("\t\t\t | | | |\n");
printf( "\t\t\t -------------------------\n");
printf("\t\t\t | | | |\n");
printf("\t\t\t | %c | %c | %c |\n",A[1][0],A[1][1],A[1][2]);
printf("\t\t\t | | | |\n");
printf( "\t\t\t -------------------------\n");
printf("\t\t\t | | | |\n");
printf("\t\t\t | %c | %c | %c |\n",A[2][0],A[2][1],A[2][2]);
printf("\t\t\t | | | |\n");
printf( "\t\t\t -------------------------\n");
printf("\n");
//--------------------------------------------------------------------
}
// THIS FINCTION IS DOING 2 TYPES OF WORK FOR "PLAYER 1"
// fIRST IT IS CHECKING THAT USER IS CHEATING OR NOT
// AND ALSO TAKING INPUT FROM USER AND SAVING IT ON USER DESIRED LOCATION.
int test1(int x)
{
n.score1=n.score1-10;
switch(x)
{
case 1:
if(A[0][0]==88 || A[0][0]== 42)
{
cheat1 ();
break;
}
else
{
A[0][0]=88;
break;
}
case 2:
if(A[0][1]==88 || A[0][1]== 42)
{
cheat1 ();
break;
}
else
{
A[0][1]=88;
break;
}
case 3:
if(A[0][2]==88 || A[0][2]== 42)
{
cheat1 ();
break;
}
else
{
A[0][2]=88;
break;
}
case 4:
if(A[1][0]==88 || A[1][0]== 42)
{
cheat1 ();
break;
}
else
{
A[1][0]=88;
break;
}
case 5:
if(A[1][1]==88 || A[1][1]== 42)
{
cheat1 ();
break;
}
else
{
A[1][1]=88;
break;
}
case 6:
if(A[1][2]==88 || A[1][2]== 42)
{
cheat1 ();
break;
}
else
{
A[1][2]=88;
break;
}
case 7:
if(A[2][0]==88 || A[2][0]== 42)
{
cheat1 ();
break;
}
else
{
A[2][0]=88;
break;
}
case 8:
if(A[2][1]==88 || A[2][1]== 42)
{
cheat1 ();
break;
}
else
{
A[2][1]=88;
break;
}
case 9:
if(A[2][2]==88 || A[2][2]== 42)
{
cheat1 ();
break;
}
else
{
A[2][2]=88;
break;
}
default:
{
cheat1 ();
}
}
}
// THIS FINCTION IS DOING 2 TYPES OF WORK FOR "PLAYER 2"
// fIRST IT IS CHECKING THAT USER IS CHEATING OR NOT
// AND ALSO TAKING INPUT FROM USER AND SAVING IT ON USER DESIRED LOCATION.
int test2(int y)
{
n.score2=n.score2-10;
switch(y)
{
case 1:
if(A[0][0]==88 || A[0][0]== 42)
{
cheat2 ();
break;
}
else
{
A[0][0]=42;
break;
}
case 2:
if(A[0][1]==88 || A[0][1]== 42)
{
cheat2 ();
break;
}
else
{
A[0][1]=42;
break;
}
case 3:
if(A[0][2]==88 || A[0][2]== 42)
{
cheat2 ();
break;
}
else
{
A[0][2]=42;
break;
}
case 4:
if(A[1][0]==88 || A[1][0]== 42)
{
cheat2 ();
break;
}
else
{
A[1][0]=42;
break;
}
case 5:
if(A[1][1]==88 || A[1][1]== 42)
{
cheat2 ();
break;
}
else
{
A[1][1]=42;
break;
}
case 6:
if(A[1][2]==88 || A[1][2]== 42)
{
cheat2 ();
break;
}
else
{
A[1][2]=42;
break;
}
case 7:
if(A[2][0]==88 || A[2][0]== 42)
{
cheat2 ();
break;
}
else
{
A[2][0]=42;
break;
}
case 8:
if(A[2][1]==88 || A[2][1]== 42)
{
cheat2 ();
break;
}
else
{
A[2][1]=42;
break;
}
case 9:
if(A[2][2]==88 || A[2][2]== 42)
{
cheat2 ();
break;
}
else
{
A[2][2]=42;
break;
}
default:
{
cheat2 ();
}
}
}
//THIS FUNCTION IS TESTING THE INPUTS BY USERS TO GIVE RESULTS .
//THERE ARE 8 CASES AND IF ANY OF THEM FULFILED THAN IT GIVE RESULTS.
void test()
{
if(A[0][0]==A[1][1] && A[0][0]==A[2][2])
{
if (A[0][0]==88)
{
t=2;p=1;
}
else
if (A[0][0]==42)
{
z=2;p=1;
}
}
else
if(A[0][2]==A[1][1] && A[0][2]==A[2][0])
{
if (A[0][2]==88)
{
t=2;p=1;
}
else
if (A[0][2]==42)
{
z=2;p=1;
}
}
else
if(A[0][0]==A[0][1] && A[0][0]==A[0][2])
{
if (A[0][0]==88)
{
t=2;p=1;
}
else
if (A[0][0]==42)
{
z=2;p=1;
}
}
else
if(A[1][0]==A[1][1] && A[1][0]==A[1][2])
{
if (A[1][0]==88)
{
t=2;p=1;
}
else
if (A[1][0]==42)
{
z=2;p=1;
}
}
else
if(A[2][0]==A[2][1] && A[2][0]==A[2][2])
{
if (A[2][0]==88)
{
t=2;p=1;
}
else
if (A[2][0]==42)
{
z=2;p=1;
}
}
else
if(A[0][0]==A[1][0] && A[0][0]==A[2][0])
{
if (A[0][0]==88)
{
t=2;p=1;
}
else
if (A[0][0]==42)
{
z=2;p=1;
}
}
if(A[0][1]==A[1][1] && A[0][1]==A[2][1])
{
if (A[0][1]==88)
{
t=2;p=1;
}
else
if (A[0][1]==42)
{
z=2;p=1;
}
}
if(A[0][2]==A[1][2] && A[0][2]==A[2][2])
{
if (A[0][2]==88)
{
t=2;p=1;
}
else
if (A[0][2]==42)
{
z=2;p=1;
}
}
}
// ITS A FUNCTION TO AVOID CHEATING BY USER 1 AND TO HELP ANOTHER FUNCTION.
// I HAVE MADE THIS FUNCTION TO AVOID PASTING ALL THESE LINES AGAIN AND AGAIN.
void cheat1()
{
h=2;
system ("cls");
display ();
printf( "INVALID ENTRY!\nTRY AGAIN!\n");
system("pause");
}
// ITS A FUNCTION TO AVOID CHEATING BY USER 2 AND TO HELP ANOTHER FUNCTION.
// I HAVE MADE THIS FUNCTION TO AVOID PASTING ALL THESE LINES AGAIN AND AGAIN.
void cheat2 ()
{
h=3;
system ("cls");
display ();
printf( "INVALID ENTRY!\n");
system("pause");
}
void display_about()
{
printf( "\n\n\n\t BILAL AHMED \t\t\t\n");
printf( "\n\n\n\n");
system ("pause");
system ("cls");
}
void display_rules()
{
printf("\n\n\n\n1=> YOU CAN ONLY USE INTEGARS NO CHARACTERS ARE ALLOWED.\n");
printf( "2=> PLAYER 1 SIGN IS \" X \" \n");
printf( "3=> PLAYER 2 SIGN IS \" * \" \n");
printf("4=> YOU JUST HAV TO CHOSE LOCATION BY ENTERING INTEGARS");
printf( " \n\n\n \t\t\t\tTHANK YOU\n\n\n\n") ;
system ("pause");
system ("cls");
}
void ahmed()
{
char temp;
if (t==2)
{
system("color 0e");
system("cls");
display();
printf("\n\t\t\t\t%sWin\t\t\n\n",n.name1);
n.score2=0;
fprintf(fp,"%s\t%d\t%s\t%d\n",n.name1,n.score1,n.name2,n.score2);
system("pause");
}
else
if (z==2)
{
system("color 0a");
system("cls");
display();
printf("\n\t\t\t\t%s WIN\n\n\n",n.name2);
n.score2=0;
fprintf(fp,"%s\t%d\t%s\t%d\n",n.name1,n.score1,n.name2,n.score2);
system("pause");
}
else
{
system("color 09");
system("cls");
display();
printf("\t\t\t\tDRAW\n\n\n");
fprintf(fp,"%s\t%d\t%s\t%d\n",n.name1,n.score1,n.name2,n.score2);
printf("Sorry you have no idea how to play");
system("pause");
}
}
Subscribe to:
Posts (Atom)
