Doesn't work like that. program below
Code:
#include <stdio.h>
#include <stdlib.h>
#define NUMBER_OF_SQUARES 9
char tic_tac_toe[NUMBER_OF_SQUARES]={'0', '1','2', '3', '4', '5','6', '7', '8'};
void print_board(char*);
void clear_board(char*);
char get_user_character();
char user_move(char*);
char get_a_winner(char*);
int get_computer_move(char*);
int main()
{
int j =0;
char winner;
int O;
clear_board(tic_tac_toe);
print_board( tic_tac_toe);
char choice =get_user_character();
char winner =get_a_winner(tic_tac_toe);
do
{
int i =user_move(tic_tac_toe);
tic_tac_toe[i-1]= choice;
print_board(tic_tac_toe);
char winner =get_a_winner(tic_tac_toe);
}while(winner != 'X' && winner != 'O');
printf("end of game\n");
}
char get_a_winner(char*tic_tac_toe)
{
char X;
char O;
char winner;
if((tic_tac_toe[0] == 'X') && (tic_tac_toe[1] =='X') && (tic_tac_toe[2]=='X')||
(tic_tac_toe[3] == 'X') && (tic_tac_toe[4] =='X') && (tic_tac_toe[5]=='X')||
(tic_tac_toe[6] == 'X') && (tic_tac_toe[7] =='X') && (tic_tac_toe[8]=='X')||
(tic_tac_toe[0] == 'X') && (tic_tac_toe[3] =='X') && (tic_tac_toe[6]=='X')||
(tic_tac_toe[1] == 'X') && (tic_tac_toe[4] =='X') && (tic_tac_toe[7]=='X')||
(tic_tac_toe[2] == 'X') && (tic_tac_toe[5] =='X') && (tic_tac_toe[8]=='X')||
(tic_tac_toe[0] == 'X') && (tic_tac_toe[4] =='X') && (tic_tac_toe[8]=='X')||
(tic_tac_toe[2] == 'X') && (tic_tac_toe[4] =='X') && (tic_tac_toe[6]=='X'))
{
winner = 'X';
printf("player %c is the winner\n",winner);
return (winner);
}
else if((tic_tac_toe[0] == 'O') && (tic_tac_toe[1] =='O') && (tic_tac_toe[2]=='O')||
(tic_tac_toe[3] == 'O') && (tic_tac_toe[4] =='O') && (tic_tac_toe[5]=='O')||
(tic_tac_toe[6] == 'O') && (tic_tac_toe[7] =='O') && (tic_tac_toe[8]=='O')||
(tic_tac_toe[0] == 'O') && (tic_tac_toe[3] =='O') && (tic_tac_toe[6]=='O')||
(tic_tac_toe[1] == 'O') && (tic_tac_toe[4] =='O') && (tic_tac_toe[7]=='O')||
(tic_tac_toe[2] == 'O') && (tic_tac_toe[5] =='O') && (tic_tac_toe[8]=='O')||
(tic_tac_toe[0] == 'O') && (tic_tac_toe[4] =='O') && (tic_tac_toe[8]=='O')||
(tic_tac_toe[2] == 'O') && (tic_tac_toe[4] =='O') && (tic_tac_toe[6]=='O'))
{
winner = 'O';
printf("player %c is the winner\n", winner);
return (winner);
}
}
Shouldn't the get_a_winner be inside the loop so that it can be checked everytime a input is made? If i put it outside the do{} it will never check for a winner it'll just keep going.