-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
342 lines (308 loc) · 10 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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "colors.h"
#include "game.h"
#include "solver.h"
#define INPUT_OK 0
#define NO_INPUT 1
#define TOO_LONG 2
#define PVP 1
#define PVC 2
typedef bool GameOutcome;
#define GAME_OVER true
#define GAME_NEXT false
// TODO: rename
typedef struct TurnState {
TurnOutcome turn_outcome;
GameOutcome game_outcome;
} TurnState;
// Get a line from the user with a prompt.
// shamelessly stolen from
// https://stackoverflow.com/questions/4023895/how-do-i-read-a-string-entered-by-the-user-in-c/4023921#4023921
static int get_line(char* prmpt, char* buff, size_t sz) {
int ch, extra;
// Get line with buffer overrun protection.
if (prmpt != NULL) {
printf("%s", prmpt);
fflush(stdout);
}
if (fgets(buff, sz, stdin) == NULL)
return NO_INPUT;
// If it was too long, there'll be no newline. In that case, we flush
// to end of line so that excess doesn't affect the next call.
if (buff[strlen(buff) - 1] != '\n') {
extra = 0;
while (((ch = getchar()) != '\n') && (ch != EOF))
extra = 1;
return (extra == 1) ? TOO_LONG : INPUT_OK;
}
// Otherwise remove newline and give string back to caller.
buff[strlen(buff) - 1] = '\0';
return INPUT_OK;
}
// TODO: make an option to leave (maybe)
/*
Prompts a player to choose a pit to play.
*/
uint32_t prompt_user(Board* board, Player player_id) {
char buffer[3]; // one digit, \n and \0
char query[39];
sprintf(query, "Player %d, choose pit to play (1-6): ", player_id + 1);
for (;;) {
int err = get_line(query, buffer, 4);
// int rand_input = (arc4random() % 6) + 1;
// printf("%d\n", rand_input);
// sprintf(buffer, "%d\n", rand_input);
// int err = 0;
if (!err) {
int user_input;
// includes EOF and failure to match (0)
if (sscanf(buffer, "%d", &user_input) > 0) {
if (user_input >= 1 && user_input <= 6) {
uint32_t converted = convert_index(user_input, player_id);
if (*get_pit(board, converted) > 0)
return converted;
goto empty;
}
}
goto invalid;
}
invalid:
printf("Invalid input, try again\n");
continue;
empty:
printf("The chosen pit is empty\n");
}
}
bool is_win(Board* board) {
uint32_t p1_pits_sum = sum(board->p1_pits);
uint32_t p2_pits_sum = sum(board->p2_pits);
return (p1_pits_sum == 0) || (p2_pits_sum == 0);
}
TurnState player_turn(Board* board,
Player current_player,
uint8_t* p1_pits_sum,
uint8_t* p2_pits_sum) {
TurnOutcome turn_outcome =
make_a_turn(board, prompt_user(board, current_player), current_player);
*p1_pits_sum = sum(board->p1_pits);
*p2_pits_sum = sum(board->p2_pits);
TurnState turn_state;
turn_state.turn_outcome = turn_outcome;
turn_state.game_outcome = GAME_NEXT; // until changed otherwise
display_board(board);
if (turn_outcome == COMPLETE) {
printf("Player %d has finished their turn!\n", current_player + 1);
if (is_win(board)) {
printf("Player %d's pits are empty, game over\n", current_player + 1);
turn_state.game_outcome = GAME_OVER;
}
} else if (turn_outcome == REPEAT) {
printf("Player %d landed their last bead into their home", current_player + 1);
if (is_win(board)) {
printf(", but their pits are empty, the game is over\n");
turn_state.game_outcome = GAME_OVER;
} else {
printf("! They get an extra turn.\n");
}
} else if (turn_outcome == INVALID) {
printf("Something has gone TERRIBLY wrong!!!!!!\n");
exit(EXIT_FAILURE);
}
return turn_state;
}
TurnState computer_turn(Board* board,
Player current_player,
uint8_t* p1_pits_sum,
uint8_t* p2_pits_sum,
uint32_t diff_level) {
printf("Processing....\n");
n_freed = 0; // for stats
StateNode* tree;
tree = create_statenode(*board, current_player, diff_level);
OptimalSolution* opt_sol = find_optimal_solution(tree, current_player);
// should be enough
char buf[300] = {0};
write_strategy(opt_sol->strategy, opt_sol->idx, buf);
printf("Computers solution: %s\n", buf);
memcpy(board, &(opt_sol->statenode.board_state), sizeof(Board));
free_optimal_solution(opt_sol);
free_statenodes(tree);
*p1_pits_sum = sum(board->p1_pits);
*p2_pits_sum = sum(board->p2_pits);
display_board(board);
//printf("n_freed:%llu\n", n_freed);
printf("Computer has finished its turn");
TurnState turn_state;
// the computer will never finish with a repeat
turn_state.turn_outcome = COMPLETE;
turn_state.game_outcome = GAME_NEXT;
if (is_win(board)) {
printf(", and the game is over.\n");
turn_state.game_outcome = GAME_OVER;
} else {
printf("!\n");
}
return turn_state;
}
int print_and_return_results(Board* board, uint8_t p1_pits_sum, uint8_t p2_pits_sum, bool is_computer) {
// The score is personal home + other player's pits
uint8_t p1_score = board->p1_home + p2_pits_sum;
uint8_t p2_score = board->p2_home + p1_pits_sum;
// QUICK HACK! FIXME
char p2_name[10];
if (is_computer) {
memcpy(p2_name, "Computer", 10);
} else {
memcpy(p2_name, "Player 2", 10);
}
printf("\n---RESULTS---\n");
printf(
"Player 1 has scored %d points (%d in home + %2d of %s's "
"pits)\n",
p1_score, board->p1_home, p2_pits_sum, p2_name);
printf(
"%s has scored %d points (%d in home + %2d of player 1's "
"pits)\n",
p2_name, p2_score, board->p2_home, p1_pits_sum);
if (p1_score > p2_score) {
printf("Player 1 wins! Thanks for playing the game.\n");
return P1;
} else if (p2_score > p1_score) {
printf("%s wins! Thanks for playing the game.\n", p2_name);
return P2;
} else {
printf("Draw! Thanks for playing the game.\n");
return DRAW;
}
}
// TODO: make it nicer, integrate with game_loop?
/*
Main game loop (PvC). Returns the id of the winner `P1` or `P2`, or `DRAW`.
*/
int alt_game_loop(Board* board, uint32_t diff_level) {
Player current_player = P1;
display_board(board);
uint8_t p1_pits_sum;
uint8_t p2_pits_sum;
TurnState game_result;
for (;;) {
if (current_player == P1) {
game_result =
player_turn(board, current_player, &p1_pits_sum, &p2_pits_sum);
} else {
game_result = computer_turn(board, current_player, &p1_pits_sum,
&p2_pits_sum, diff_level);
}
if (game_result.game_outcome == GAME_OVER) {
break;
} else {
if (game_result.turn_outcome == COMPLETE) {
current_player = !current_player;
}
continue;
}
}
return print_and_return_results(board, p1_pits_sum, p2_pits_sum, true);
}
// TODO: refactor to make it solver friendly (make another function?)
/*
Main game loop (PvP). Returns the id of the winner `P1` or `P2`, or `DRAW`.
*/
int game_loop(Board* board) {
Player current_player = P1;
display_board(board);
uint8_t p1_pits_sum;
uint8_t p2_pits_sum;
TurnState game_result;
for (;;) {
game_result = player_turn(board, current_player, &p1_pits_sum, &p2_pits_sum);
if (game_result.game_outcome == GAME_OVER) {
break;
} else {
if (game_result.turn_outcome == COMPLETE) {
current_player = !current_player;
}
continue;
}
}
return print_and_return_results(board, p1_pits_sum, p2_pits_sum, false);
}
int welcoming_prompt() {
printf("---Welcome to Kalaha!---\n");
printf("Choose mode:\n");
printf(" 1) Two players\n");
printf(" 2) Single player vs computer\n");
for (;;) {
char buffer[3]; // one digit, \n and \0
char query[6];
sprintf(query, ">>> ");
int err = get_line(query, buffer, 4);
if (err) {
printf("Invalid input, try again!\n");
continue;
}
int user_input;
// includes EOF and failure to match (0)
if (sscanf(buffer, "%d", &user_input) <= 0) {
printf("Invalid input, try again!\n");
continue;
}
if (user_input != 1 && user_input != 2) {
printf("Invalid input, try again!\n");
continue;
}
return user_input;
}
}
uint32_t choose_difficulty() {
printf("Choose difficulty level\n");
for (;;) {
char buffer[6];
char query[6];
sprintf(query, ">>> ");
int err = get_line(query, buffer, 4);
if (err) {
printf("Invalid input, try again!\n");
continue;
}
int user_input;
// includes EOF and failure to match (0)
if (sscanf(buffer, "%d", &user_input) <= 0) {
printf("Invalid input, try again!\n");
continue;
}
if (user_input <= 0) {
printf("Invalid input, try again!\n");
continue;
}
return user_input;
}
}
int main() {
Board _init_board = {
{6, 6, 6, 6, 6, 6}, // player2's pits
0, // player2's home
{6, 6, 6, 6, 6, 6}, // player1's pits
0 // player1's home
};
Board* board = &_init_board;
srand(time(NULL) + 1);
int res = welcoming_prompt();
if (res == PVP) {
printf("Let's begin!\n\n");
game_loop(board);
return 0;
}
if (res == PVC) {
uint32_t diff_level = choose_difficulty();
printf("Let's begin!\n\n");
alt_game_loop(board, diff_level);
}
return 0;
}