-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtictactoe.c
185 lines (160 loc) · 3.71 KB
/
tictactoe.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include <stdio.h>
#include <time.h>
void clean_stdin(void) {
int c;
do {
c = getc(stdin);
} while (c != '\n' && c != EOF);
}
void display(int *board) {
for (int i = 0; i < 9; i++) {
if (!(i % 3)) {
printf("\n");
}
switch (board[i]) {
case 1:
printf("X ");
break;
case 2:
printf("O ");
break;
default:
printf(". ");
}
}
printf("\n");
}
int detect(int *board) { // -1 Draw, 0 in play, 1 X win, 2 O win
for (int offset = 0; offset < 9; offset += 3) { // Row win detect
int win = board[offset];
if (win) {
if (win == board[offset+1] && win == board[offset+2]) {
return win;
}
}
}
for (int offset = 0; offset < 3; offset++) { // Col win detect
int win = board[offset];
if (win) {
if (win == board[offset+3] && win == board[offset+6]) {
return win;
}
}
}
int win = board[4];
if (win) { // Diagonal win detect
if (win == board[0] && win == board[8]) {
return win;
}
if (win == board[2] && win == board[6]) {
return win;
}
}
for (int i = 0; i < 9; i++) {
if (!board[i]) {
return 0;
}
}
return -1;
}
void AIplay(int *board, int me) {
for (int i = 0; i < 9; i++) { // Win on this move
if (!board[i]) {
board[i] = me;
if (detect(board) == me) {
return;
}
board[i] = 0;
}
}
int op = me & 1;
op++;
for (int i = 0; i < 9; i++) { // Block a win next move
if (!board[i]) {
board[i] = op;
if (detect(board) == op) {
board[i] = me;
return;
}
board[i] = 0;
}
}
if (!board[4]) {
board[4] = me;
return;
}
int rand = time(NULL) % 5;
rand <<= 1;
int i = rand;
do {
if (!board[i]) {
board[i] = me;
return;
}
i += 2;
i %= 10;
} while (i != rand);
for (i = 0; i < 9; i++) {
if (!board[i]) {
board[i] = me;
return;
}
}
}
void humanplay(int *board, int player) {
while (1) {
printf("Use keypad to indicate location >>> ");
int ui = getc(stdin);
clean_stdin();
ui -= '0';
if (ui >= 1 && ui <= 9) {
ui--;
int location = 2 - (ui / 3);
location *= 3;
location += ui % 3;
if (!board[location]) {
board[location] = player;
return;
}
}
printf("Bad input: enter 1<=x<=9, 7 = top left, 3 = bottom right, blank space.\n");
}
}
int main() {
int board[9];
int player = 1;
int result = 0;
int computer = 2;
for (int i = 0; i < 9; i++) {
board[i] = 0;
}
printf("Computer goes first? y/n >>> ");
int ui = getc(stdin);
clean_stdin();
if (ui == 'y' || ui == 'Y') {
computer = 1;
}
display(board);
while (!result) {
if (player == computer) {
AIplay(board, player);
} else {
humanplay(board, player);
}
display(board);
result = detect(board);
player &= 1;
player++;
}
switch (result) {
case 1:
printf("\nWinner: X\n");
break;
case 2:
printf("\nWinner: O\n");
break;
case -1:
printf("\nDraw.\n");
}
return 0;
}