-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalphatoe.c
66 lines (57 loc) · 1.34 KB
/
alphatoe.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
60
61
62
63
64
65
66
/*
* Fixed version of alphatoe.c
* Credit: Samuel Guilbeault
*/
#include "ttt.h"
#include <stdio.h>
#include <ctype.h>
#define MAX_INPUT 512
int main(int argc, char **argv)
{
char human;
char input[MAX_INPUT];
init_boards();
init_board(START_BOARD);
join_graph(START_BOARD);
compute_score();
printf("X or O? ");
fgets(input, MAX_INPUT, stdin);
human = input[0];
int board = 0;
while (isalpha(htable[board].turn)) // while no winner
{
printf("%d\n", board);
if (human == htable[board].turn)
{
int next_board;
do
{
char movec;
int movei;
print_node(htable[board]);
printf("Your move [0-8]: ");
fgets(input, MAX_INPUT, stdin);
movec = input[0];
movei = (int)(movec - '0');
next_board = htable[board].move[movei];
if (next_board == -1)
{
printf("Illegal move, try again!\n");
}
} while (next_board == -1);
board = next_board;
}
else // computers turn
{
board = htable[board].move[best_move(board)];
} // computers turn
} // while loop
print_node(htable[board]);
printf("Game over, Player 1!\n");
if (htable[board].winner == human)
printf("You won!\n");
else if (htable[board].winner == '-')
printf("The game was a tie!\n");
else
printf("You lost!\n");
}