Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/* Included Libraries */
int a=0,b=0,c=0,d=0,e=0,f=0,g=0,h=0,i=0,q=0; //Global integers for each square's value
int update(int player); //Prototype functions
int print_chart(int a, int b, int c, int d, int e, int f, int g, int h, int i); //Prototype functions
int check(int a,int b, int c, int d, int e, int f, int g, int h, int i); //Prototype functions
int printmenu(); //Prototype functions
int main()
{
printmenu(); //Call printmenu()
printf("\n---Type z if you want to print the table again---\n");
printf("---Q or q to quit---\n");
while(check(a,b,c,d,e,f,g,h,i)==0 && (q!=1)) //Start loop looking for a win or a quit
{
update(1);
print_chart(a,b,c,d,e,f,g,h,i);
update(2);
print_chart(a,b,c,d,e,f,g,h,i);
}
return 0; //finish program by returning an int from main
}
int print_chart(int a, int b, int c, int d, int e, int f, int g, int h, int i)
{ //Prints the game board with the players valye
printf("+-----------+\n\n");
printf(" %d | %d | %d\n",a,b,c);
printf("---+---+---\n");
printf(" %d | %d | %d\n",d,e,f);
printf("---+---+---\n");
printf(" %d | %d | %d\n\n",g,h,i);
printf("+-----------+\n\n\n");
return 0;
}
int update(int player) //recieve choice (input) from the player
{
char choice;
printf("Player%d [%d] choose: ",player,player);
scanf("%s",&choice);
if(choice == 'a')
return a = player;
else if(choice == 'b')
return b = player;
else if(choice == 'c')
return c = player;
else if(choice == 'd')
return d = player;
else if(choice == 'e')
return e = player;
else if(choice == 'f')
return f = player;
else if(choice == 'g')
return g = player;
else if(choice == 'h')
return h = player;
else if(choice == 'i')
return i = player;
else if(choice == 'z'||choice == 'Z') //check for z or Z to display menu
{
printmenu();
return 0;
}
else if(choice == 'q'||choice == 'Q') //check for q or Q to quit game
return q=1;
else
return 0;
return 0;
}
int printmenu()
{
printf(" a | b | c\n");
printf("---+---+---\n");
printf(" d | e | f\n");
printf("---+---+---\n");
printf(" g | h | i\n\n");
printf("+-----------+\n");
return 0;
}
int check(int a, int b, int c, int d, int e, int f, int g, int h, int i)
{
/*
{
printf("Tic Tac C, a combo of three!\n");
return 1;
}
else
return 0;
*/
/*
int aei=a+e+i,abc=a+b+c,adg=a+d+g,beh=b+e+h,cfi=c+f+i,gec=g+e+c,ghi=g+h+i,def=d+e+f;
if(aei==3||abc==3||adg==3||beh==3||cfi==3||gec==3||ghi==3||def==3)
{
printf("Player1 Wins!\n");
return 1;
}
else if(aei==6||abc==6||adg==6||beh==6||cfi==6||gec==6||ghi==6||def==6)
{
printf("Player2 Wins!\n");
return 2;
}
else
return 0;
*/
}
/*
if( ( ((a==e)&&(e==i)) ||
((a==b)&&(b==c)) ||
((a==d)&&(d==g)) ||
((b==e)&&(e==h)) ||
((c==f)&&(f==i)) ||
((g==e)&&(e==c)) ||
((g==h)&&(h==i)) ||
((d==e)&&(e==f))))
*/