Something got jacked up.
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();
int computer_move=get_computer_move(tic_tac_toe);
if (choice == 'X')
tic_tac_toe[computer_move] = 'X';
else
tic_tac_toe[computer_move] = 'O';
do
{
int i =user_move(tic_tac_toe);
tic_tac_toe[i-1]= choice;
print_board(tic_tac_toe);
winner =get_a_winner(tic_tac_toe);
int computer_move=get_computer_move(tic_tac_toe);
if (choice == 'X')
tic_tac_toe[computer_move] = 'X';
else
tic_tac_toe[computer_move] = 'O';
// tic_tac_toe[computer_move] = 'O';
print_board(tic_tac_toe);
}while(winner != 'X' && winner != 'O');
printf("end of game\n");
return EXIT_SUCCESS;
}
void clear_board(char*tic_tac_toe)
{
int i;
for (i = 0; i < 9; i++)
{
tic_tac_toe[i] = ' ';
}
}
void print_board(char *tic_tac_toe)
{
printf(" %c | %c | %c\n", tic_tac_toe[0], tic_tac_toe[1], tic_tac_toe[2]);
printf("----+---+----\n");
printf(" %c | %c | %c\n", tic_tac_toe[3], tic_tac_toe[4], tic_tac_toe[5]);
printf("----+---+----\n");
printf(" %c | %c | %c\n", tic_tac_toe[6], tic_tac_toe[7], tic_tac_toe[8]);
}
char get_user_character()
{
char choice;
printf("Play with X or play with O\n");
scanf("%c", &choice);
return (choice);
}
char user_move(char *tic_tac_toe)
{
int i;
printf("Enter what box number that you\n");
printf("would like to place you move in>\n");
scanf("%d", &i);
if (i < 0 || i>9)
{
printf("invalid move\n");
}
else if (tic_tac_toe[i] != ' ')
{
printf("make another move\n");
return (i);
}
}
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);
}
else
{
printf("no winner yet\n");
}
}
int get_computer_move(char *tic_tac_toe)
{
int computer_move;
for (computer_move=0; computer_move<9; computer_move++)
if(tic_tac_toe[computer_move] == ' ')
return (computer_move);
}
output
Code:
| |
----+---+----
| |
----+---+----
| |
Play with X or play with O
X
Enter what box number that you
would like to place you move in>
5
X | |
----+---+----
| |
----+---+----
| |
no winner yet
X | X |
----+---+----
| |
----+---+----
| |