-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
267 lines (255 loc) · 6.5 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
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
int human = -1;
int robot = 1;
int empty = 0;
int size = 9;
// max(a, b) returns the bigger number
int max(a, b) {
if (a > b) return a; else return b;
}
// min(a, b) returns the smaller number
int min(a, b) {
if (a < b) return a; else return b;
}
// full(board) determines if all spaces on the board
// are occupied and returns true, otherwise false
bool full(int board[9]) {
for (int i = 0; i < size; i++) {
if (board[i] == 0) { // if any of the spaces are empty, it is not full
return false;
}
}
return true;
}
// win(board, player) determines if the game is at terminal
// state in favor of the player specified
bool win(int board[9], int player) {
if ((board[0] == player &&
board[3] == player &&
board[6] == player) ||
(board[1] == player &&
board[4] == player &&
board[7] == player) ||
(board[2] == player &&
board[5] == player &&
board[8] == player) ||
(board[0] == player &&
board[1] == player &&
board[2] == player) ||
(board[3] == player &&
board[4] == player &&
board[5] == player) ||
(board[6] == player &&
board[7] == player &&
board[8] == player) ||
(board[0] == player &&
board[4] == player &&
board[8] == player) ||
(board[2] == player &&
board[4] == player &&
board[6] == player)) { // 8 possible win combinations
return true;
} else {
return false;
}
}
// minimax(board, scoreboard, player) recursively updates
// scoreboard
// effects: mutates scoreboard
int minimax(int board[9], int player) {
// if human wins
if (win(board, human)) {
return -10;
}
// if robot wins
if (win(board, robot)) {
return 10;
}
// if no spaces left and no wins
if (full(board)) {
return 0;
}
// if player is robot, maximize score
if (player == robot) {
int bestscore = -100;
// traverse through the board
for (int i = 0; i < size; i++) {
// if there is an empty space
if (board[i] == empty) {
// try the space
board[i] = robot;
// recursively determine score at current space and aim to maximize
bestscore = max(bestscore, minimax(board, human));
// reset the board
board[i] = empty;
}
}
return bestscore;
} else { // if player is human, minimize score
int bestscore = 100;
// traverse through the board
for (int i = 0; i < size; i++) {
// if there is an empty space
if (board[i] == empty) {
// try the space
board[i] = human;
// recursively determine score at current space and aim to minimize
bestscore = min(bestscore, minimax(board, robot));
// reset the board
board[i] = empty;
}
}
return bestscore;
}
}
// move(board, scoreboard) executes the move which is most
// favorable for the robot
// effects: mutates board
// mutates scoreboard
void move(int board[9]) {
int index = -100;
int bestscore = -100;
// check that board is not full
if (!full(board)) {
// traverse
for (int i = 0; i < size; i++) {
// if space is empty
if (board[i] == empty) {
// pretend robot is on this tile
board[i] = robot;
// determine score
int score = minimax(board, human);
// done pretending
board[i] = empty;
// update bestscore and index
if (score > bestscore) {
index = i;
bestscore = score;
}
}
}
board[index] = robot;
}
}
// print(board) logs the board to the console
void print(int board[9]) {
printf("\n\n\n\n\n\n TIC TAC TOE\n\n\n");
int column = 0;
for (int i = 0; i < size; i++) {
if (column == 0) {
printf(" ");
}
if (board[i] == human) {
//printf("\u25EF");
printf("\x001b[1mO\x1B[0m");
} else if (board[i] == robot) {
//printf("\u2573");
printf("\x001b[1mX\x1B[0m");
} else {
printf("\x001b[31m%d\x1B[0m", i + 1);
}
if (column == 2) {
printf("\n");
column = 0;
} else {
printf(" ");
column++;
}
}
printf("\n\n");
}
// update(board) updates the board with user's input
// effects: mutates board
void update(int board[9]) {
char c = getchar();
getchar();
int ind = c - '0';
if (board[ind - 1] == empty) {
board[ind - 1] = human;
}
}
// restart() restarts the game if input is Y
bool restart(void) {
char c = getchar();
getchar();
if (c == 'Y' || c == 'y') {
return true;
} else {
return false;
}
}
// game(board) consists of the graphical user interface which prompts the user
void game(int board[9]) {
while (1) {
int count = 0;
for (int i = 0; i < size; i++) {
if (board[i] != 0) {
count++;
}
}
if (count < 8) {
system("clear");
print(board);
printf(" Type a number and press enter.\n If you try to overwrite a box,\n your chance will be passed on.\n\n\n");
printf(" ");
update(board);
move(board);
}
system("clear");
print(board);
if (count == 8) {
for (int i = 0; i < size; i++) {
if (board[i] == 0) {
board[i] = human;
continue;
}
}
}
system("clear");
print(board);
if (win(board, human)) {
printf(" You win!\n\n");
printf(" Would you like to replay? Y/N\n\n");
if (restart()) {
for (int i = 0; i < size; i++) {
board[i] = 0;
}
continue;
} else {
system("clear");
break;
}
} else if (win(board, robot)) {
printf(" You lose!\n\n");
printf(" Would you like to replay? Y/N\n\n");
if (restart()) {
for (int i = 0; i < size; i++) {
board[i] = 0;
}
continue;
} else {
system("clear");
break;
}
} else if (full(board)) {
printf(" Tie!\n\n");
printf(" Would you like to replay? Y/N\n\n");
if (restart()) {
for (int i = 0; i < size; i++) {
board[i] = 0;
}
continue;
} else {
system("clear");
break;
}
}
}
}
int main(void) {
int ogboard[9] = {0,0,0,0,0,0,0,0,0};
game(ogboard);
return 0;
}