Code:
#include <stdio.h>
#include <stdlib.h>
void print_grid();
char player(int number);
int search4win(int *moves, int length);
int tictactoe();
void reset_board();
int wins[8][3]={{1,2,3},{4,5,6},{7,8,9},{1,4,7},{2,5,8},{3,6,9},{1,5,9},{3,5,7}};
char board[9]={'1','2','3','4','5','6','7','8','9'};
/* X will be represented by 1
O will be represented by -1
*/
int main()
{
int play=1, winner, win[3]={0,0,0} /*Xwin=0, Owin=0, draw=0*/;
char again;
while (play >= 1)
{
winner=tictactoe();
switch (winner){
case 1:
//Xwin++;
win[0]++;
break;
case -1:
//Owin++;
win[1]++;
break;
case 0:
//draw++;
win[2]++;
break;
default:
printf("Unknown winner");
break;
}
printf("So far \n X has won: %d times\n O has won: %d times\n and there have been %d draws.\n", win[0], win[1], win[2]);
printf("would you like another go Y or N\n");
scanf("%c", &again);
getchar();
if ((again=='y') || (again=='Y'))
{
play++;
reset_board();
}
else if ((again=='n') || (again=='N'))
{
printf("Thank you for playing goodbye\n");
play=0;
}
else
printf("Invalid entry\n");
}
return 0;
}
int tictactoe()
{
int Xmoves[5], Omoves[4];
int go, goes=0, turn=1, row=0, Xgoes=0, Ogoes=0;
print_grid();
while ((goes < 9) && (row != 3))
{
printf("Would player %c please enter there move\n", (player(turn)));
scanf("%d", &go);
getchar();
//printf("your go was %d \n", go); //This is a test line not needed in finsished game
if((go < 10) && (go > 0) && (board[go-1] != 'X') && (board[go-1] != 'O'))
/*This line first checks if the number is between 1 and 9 and then checks that neither X or O has already been in that location*/
{
board[go-1]=(player(turn));
print_grid();
if (turn > 0)
{
Xmoves[Xgoes]=go;
//printf("Xgoes is %d \n", Xgoes); // Test lines
//printf("Xmoves[Xgoes] is %d \n", Xmoves[Xgoes]);
Xgoes++;
if (Xgoes >2)
{
row = search4win(Xmoves, Xgoes);
}
//printf("row is %d \n", row); //Test line
}
else
{
Omoves[Ogoes]=go;
Ogoes++;
if (Ogoes >2)
{
row=search4win(Omoves, Ogoes);
}
//printf("row is %d \n", row); //Test line
}
if(row==3) // if there is a line of 3 the winner is announced
{
printf("The winner is %c\n", (player(turn)));
return turn;
}
goes++; //increments the number of goes had by both O and X
turn=turn*(-1); //this line changes whose turn it is
}
else
printf("invalid entry\n");
}
return 0;
}
void reset_board()
{
int i;
for(i=0; i<=8; i++)
{
board[i]= i + '1'; // load the character value of the current index
}
}
/*void reset_board()
{ int i=0;
char new_board[9]={'1','2','3','4','5','6','7','8','9'};
for(i=0; i<=8; i++)
{
board[i]=new_board[i];
//printf("reset to %d \n", board[i]);
}
}*/
char player(int number)
{
return (number > 0) ? 'X' : 'O';
/*char temp;
if ( number > 0)
temp = 'X';
else
temp = 'O';
return temp;*/
}
void print_grid()
{
printf("\n %c|%c|%c\n -+-+-\n", board[6], board[7], board[8]);
printf(" %c|%c|%c\n -+-+-\n", board[3], board[4], board[5]);
printf(" %c|%c|%c\n\n", board[0], board[1], board[2]);
}
int search4win(int *moves, int length)
{
int i=0, j=0, k=0, n, line=0, ret;
while(line < 3 && j < 8)
{
for(i=0; i<3; i++)
{
n=wins[j][i];
//printf("n is %d\n", n);
for(k=0; k<length; k++)
{
if (n == moves[k])
line++;
//printf("line is %d \n", line);
}
}
if (line == 3)
return 3;
else
line=0;
j++;
}
return 0;
}
Thanks again everyone!! If you have any more suggestions on how I could improve my code then please keep posting