-
Notifications
You must be signed in to change notification settings - Fork 0
/
TicTacToe_MiniMax.c
252 lines (222 loc) · 4.5 KB
/
TicTacToe_MiniMax.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int board[3][3];
int check_win(int board[3][3]) {
int i;
for(i = 0; i < 3; i++) {
if(board[i][0] && board[i][1] == board[i][2] && board[i][2] == board[i][0]) {
return board[i][0];
}
if(board[0][i] && board[1][i] == board[2][i] && board[2][i] == board[0][i]) {
return board[0][i];
}
}
if(!board[1][1])
return 0;
if(board[1][1] == board[0][0] && board[2][2] == board[1][1]) {
return board[1][1];
}
if(board[1][1] == board[0][2] && board[1][1] == board[2][0]) {
return board[1][1];
}
return 0;
}
void reset_board() {
int i, j;
for(i = 0; i < 3; i++) {
for(j = 0; j < 3; j++) {
board[i][j] = 0;
}
}
}
void print_board() {
int i, j;
char *move = "X O";
printf(" ___ ___ ___ \n");
for(i = 0; i < 3; i++) {
for(j = 0; j < 3; j++) {
printf("| %c |", move[board[i][j] + 1]);
}
printf("\n|___||___||___|\n");
}
printf("\n");
}
void print_board_start() {
int i, j;
char *move = "X O";
printf(" ___ ___ ___ \n");
for(i = 0; i < 3; i++) {
for(j = 0; j < 3; j++) {
printf("| %d |", i*3 + j + 1);
}
printf("\n|___||___||___|\n");
}
printf("\n");
}
int board_full(int board[3][3]) {
int i, j;
for(i = 0; i < 3; i++) {
for(j = 0; j < 3; j++) {
if(board[i][j] == 0) {
return 0;
}
}
}
return 1;
}
int valid_move(int pos, int player) {
if(pos > 9 || pos < 1) {
return 0;
}
int play_i, play_j;
play_i = pos/3;
play_j = pos%3;
if(play_j == 0) {
play_i--;
play_j = 2;
}
else {
play_j--;
}
if(board[play_i][play_j] != 0) {
return 0;
}
else {
board[play_i][play_j] = player;
return player;
}
}
void player_move() {
int pos, valid = 1;
int ch, flag = 0; // for invalid characters in scanf
do {
if(!valid && !flag){
printf("Invalid move! Try Again.\n");
}
printf("\n\nEnter position you want to play: ");
if(scanf("%d", &pos) <= 0) {
valid = 1;
while ((ch = getchar()) != '\n' && ch != EOF);
printf("Weird character encountered!!!\n");
flag = 1;
continue;
}
flag = 0;
}while(!(valid = valid_move(pos, 1)));
}
void populate(int a[3][3], int b[3][3]) {
int i, j;
for(i = 0; i < 3; i++) {
for(j = 0; j < 3; j++) {
a[i][j] = b[i][j];
}
}
}
void computer_move() {
int pos = -26;
int i, j, best_i = 1, best_j = 1;
int points = 25, move_points = 0;
printf("\n\nZ3TA plays position: ");
for(i = 0; i < 3; i++) {
for(j = 0; j < 3; j++) {
if(!board[i][j]) {
board[i][j] = -1;
move_points = minimax(board, 1);
//printf("%d\n", move_points); the magic happens here!
if(points > move_points) {
points = move_points;
pos = i*3 + j + 1;
best_i = i;
best_j = j;
}
board[i][j] = 0;
}
}
}
board[best_i][best_j] = -1;
printf("%d\n", pos);
}
int minimax(int b[3][3], int player) {
int val = check_win(b);
int i, j;
int points = 25, move_points = 0;
if(player == 1) points = -points;
if(val || board_full(b)) {
return val;
}
for(i = 0; i < 3; i++) {
for(j = 0; j < 3; j++) {
if(!b[i][j]) {
if(player == -1) {
b[i][j] = -1;
move_points = minimax(b, 1);
if(points > move_points) {
points = move_points;
}
}
else {
b[i][j] = 1;
move_points = minimax(b, -1);
if(points < move_points) {
points = move_points;
}
}
b[i][j] = 0;
}
}
}
return points;
}
void play_game() {
char name[50];
printf("Hi!\n\nI am Z3TA.\n\nWhat's your name: ");
scanf("%s", name);
int play_first = 0;
printf("\n\nDo you want to play first? 1 - Yes, 0 - No: ");
scanf("%d", &play_first);
printf("\n\n\nX: Z3TA\nO: %s\n\n\n",name);
if(!play_first) {
while(!board_full(board) && !check_win(board)) {
computer_move();
printf("Z3TA just made a move...\n");
print_board();
if(board_full(board) || check_win(board)) {
break;
}
player_move();
printf("%s just made a move...\n", name);
print_board();
}
}
else {
while(!board_full(board) && !check_win(board)) {
player_move();
printf("%s just made a move...\n", name);
print_board();
if(board_full(board) || check_win(board)) {
break;
}
computer_move();
printf("Z3TA just made a move...\n");
print_board();
}
}
printf("\n\n------------------------------\n\n");
if(check_win(board) == 0) {
printf("It's a DRAW!\n");
}
else if(check_win(board) == 1) {
printf("%s WON!\n", name);
}
else {
printf("Z3TA WON!\n");
}
printf("\n------------------------------\n");
}
int main() {
reset_board();
print_board_start();
play_game();
return 0;
}