Sage
|
C project - Tic Tac Toesimple tic tac toe game.
board is made like to following
|123|
|456|
|789|
If you want to move in top right, just type 1.
There are no computer players at this time, and when you enter your names, do not use spaces.
| Code: | /**********************************
*
*Name - Eddie O'Hara
*Assignment - Tic Tac Toe
*Date - 15/1/2008
*Description - It's a Tic Tac Toe
*
**********************************/
#include <stdio.h>
#include <math.h>
char choices[9]; // empty, X, 0
int main()
{
int f, g, help, i, o, piece, player, q;
int game_over;
char name1[30],name2[30];
game_over =0; // =1 when game is over
player=1; // actually player =1
printf("Welcome to Tic Tac Toe game project.\n");
printf("player 1 name X: ");
scanf("%s", name1);
printf("player 2 name O: ");
scanf("%s", name2);
printf(" Game between %s and %s\n\n", name1,name2);
for(i=0; i<= 9; i++) choices [i] = ' ';
print_board();
while(game_over==0){
player =player +1;
player = player%2;
printf("Where do you want your piece?\n");
scanf("%d", &piece);
if (piece < 0 || piece > 9)
printf ("error\n");
else if(choices[piece] == 'X' || choices[piece] == 'O')
printf ("error\n");
else {
printf("%d\n",piece);
if (player ==0) choices[piece] = 'X';
else choices[piece] = 'O';
print_board ();
game_over = check_win();
}
}//endwhile
}//end of main
int print_board()
{
printf(" * * \n");
printf("%c *%c *%c \n", choices[1], choices[2], choices[3]);
printf(" * * \n");
printf("***********\n");
printf(" * * \n");
printf("%c *%c *%c \n", choices[4], choices[5], choices[6]);
printf(" * * \n");
printf("***********\n");
printf(" * * \n");
printf("%c *%c *%c \n", choices[7], choices[8], choices[9]);
printf(" * * \n\n");
return(0);
}
//void take_turn (int player)
//}
int check_win()
{
if(choices[1] != ' ' && choices[1] == choices[2] && choices[2] == choices[3]){
printf("Winna' winna' chicken dinna!\n");
return(1);
}else if(choices[4] != ' ' && choices[4] == choices[5] && choices[5]
== choices[6]){
printf("Winna' winna' chicken dinna!\n");
return(1);
}else if(choices[7] != ' ' && choices[7] == choices[8] && choices[8]
== choices[9]){
printf("Winna' winna' chicken dinna!\n");
return(1);
}else if(choices[1] != ' ' && choices[1] == choices[4] && choices[4]
== choices[7]){
printf("Winna' winna' chicken dinna!\n");
return(1);
}else if(choices[2] != ' ' && choices[2] == choices[5] && choices[5]
== choices[8]){
printf("Winna' winna' chicken dinna!\n");
return(1);
}else if(choices[3] != ' ' && choices[3] == choices[6] && choices[6]
== choices[9]){
printf("Winna' winna' chicken dinna!\n");
return(1);
}else if(choices[1] != ' ' && choices[1] == choices[5] && choices[5]
== choices[9]){
printf("Winna' winna' chicken dinna!\n");
return(1);
}else if(choices[7] != ' ' && choices[7] == choices[5] && choices[5]
== choices[3]){
printf("Winna' winna' chicken dinna!\n");
return(1);
}
return(0);
}
|
|