-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.c
59 lines (54 loc) · 1.37 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "tic_tac_toe.h"
char board[9]; //game board
int main(void)
{
char player_marker = '\0', computer_marker = '\0'; //markers
int turn = PLAYER, position = 9, moves = 0;
srand(time(NULL));
intialize(board);
show_instructions();
printf("\n\t\t\tEnter 'O' or 'X' to choose: ");
printf("\nChoose Marker : ");
while ((player_marker != 'X') && (player_marker != 'O'))
{
fflush(stdin);
scanf("%c", &player_marker);
}
if (player_marker == 'O')
computer_marker = 'X';
else
computer_marker = 'O';
while (!(game_over(board) || moves == 9))
{
if (turn == PLAYER)
{
while (!check_positon(board, position))
{
printf("\tChoose position : ");
fflush(stdin);
scanf("%d", &position);
}
place_marker(board, position, player_marker);
moves++;
system("CLS");
printf("\n\t\tPlayer has put a %c at %d\n", player_marker, position);
show_board(board);
winner(board, turn);
turn = COMPUTER;
}
else if (turn == COMPUTER)
{
while (!check_positon(board, position))
position = rand() % 9;
place_marker(board, position, computer_marker);
moves++;
printf("\n\t\tComputer has put a %c at %d\n", computer_marker, position);
show_board(board);
winner(board, turn);
turn = PLAYER;
}
}
if (!game_over(board) && moves == 9)
printf("\nGame Draw..!!\n");
return 0;
}