-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.c
354 lines (297 loc) · 7.39 KB
/
game.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
343
344
345
346
347
348
349
350
351
352
353
354
#include <stdlib.h>
#include <time.h>
#include <err.h>
#include "game.h"
#include "gamedefs.h"
#include "player.h"
#include "display.h"
#include "rand_util.h"
#ifdef debug
#include "stringbuilder.h"
#include <stdio.h>
#endif
game create_game(player p)
{
if(p)
{
game g = calloc(1, sizeof(game_t));
if(!g) exit(1);
g->p = create_player(p->name);
g->p->score = (p->score) ? p->score : 0;
g = game_reset(g);
return g;
}
return 0;
}
void destroy_game(game g)
{
if(g)
{
if(g->p) destroy_player(g->p);
free(g);
}
}
game game_reset(game g)
{
if(g)
{
// Delete old board
if(g->board)
{
for(int row = 0; row < g->board_height; row++) free(g->board[row]); // Free each row;
free(g->board);
}
// Update the terminal size
size_t* term_size = get_screen_size();
g->board_height = term_size[1] - 6;
if((g->board_height - 1) % 2) g->board_height--;
g->board_width = term_size[0];
if((g->board_width - 1) % 2) g->board_width--;
free(term_size);
// Resize the board_size
g->board = generate_maze(g->board_width, g->board_height);
g->turn = 0; // Reset the move count;
time(&g->start_time);
}
g->board[1][1] = PLAYER; // Starts the game off
return g;
}
board_space** generate_maze(unsigned width, unsigned height)
{
// Resize the board_size
board_space** board = malloc(sizeof(board_space*) * height);
if(!board) err(1, "Memory error");
for(int row = 0; row < height; row++)
{
board[row] = malloc(sizeof(board_space) * width);
if(!board[row]) err(1, "Memory error");
}
#ifdef debug
stringbuilder dboard = create_stringbuilder();
#endif
// Reset the board
for(int row = 0; row < height; row++)
for(int col = 0; col < width; col++)
board[row][col] = EMPTY;
// Generate the pylons
for(int row = 0; row < height; row++)
{
#ifdef debug
sb_append_ch(dboard, "[");
#endif
for(int col = 0; col < width; col++)
{
if(!row || row == height - 1)
{
if(!col)
board[row][col] = (!row) ? CORNER_RIGHT : CORNER_U_RIGHT;
else if(col == width - 1)
board[row][col] = (!row) ? CORNER_LEFT : CORNER_U_LEFT;
else
board[row][col] = HORIZONTAL;
}
else if(!col || col == width - 1) board[row][col] = VERTICAL;
else if(!(row % 2) && !(col % 2))
{
// This is done later, no reason to set it to CENTER twice
// board[row][col] = CENTER;
int side = randint(0, 3); // Decide which side to build the wall on
switch(side)
{
case 0:
//if(row == 2) board[0][col] = T_DOWN;
board[row - 1][col] = UNCONNECTED;
#ifdef debug
sb_append_ch(dboard, " 0");
#endif
break;
case 1:
//if(col == width - 3) board[row][width - 1] = T_LEFT;
board[row][col + 1] = UNCONNECTED;
#ifdef debug
sb_append_ch(dboard, " 1");
#endif
break;
case 2:
//if(row == height - 3) board[height - 1][col] = T_UP;
board[row + 1][col] = UNCONNECTED;
#ifdef debug
sb_append_ch(dboard, " 2");
#endif
break;
case 3:
//if(row == 2) board[0][col] = T_RIGHT;
board[row][col - 1] = UNCONNECTED;
#ifdef debug
sb_append_ch(dboard, " 3");
#endif
break;
default:
#ifdef debug
sb_append_ch(dboard, " U");
#endif
break;
}
board[row][col] = UNCONNECTED;
// board[row][col] = find_wall_type(board, row, col, height - 1, width - 1);
}
}
#ifdef debug
sb_append_ch(dboard, " ]\n");
#endif
}
#ifdef debug
char* dres = sb_concat(dboard);
destroy_stringbuilder(dboard);
printf("%s", dres);
free(dres);
#endif
for(int row = 0; row < height; row++)
for(int col = 0; col < width; col++)
if(space_is_wall(board[row][col])) board[row][col] = find_wall_type(board, row, col, height - 1, width - 1);
board[1][1] = START;
board[height - 2][width - 2] = FINISH;
return board;
}
unsigned* find_player(game g)
{
if(g)
{
unsigned* result = calloc(2, sizeof(unsigned));
if(!result) err(1, "Memory Error");
for(int row = 0; row < g->board_height; row++)
for(int col = 0; col < g->board_width; col++)
if(g->board[row][col] == PLAYER)
{
result[0] = row;
result[1] = col;
break;
}
return result;
}
return 0;
}
unsigned char game_is_win(game g)
{
if(g)
{
unsigned* position = find_player(g);
if(position[0] && position[1] && position[0] == (g->board_height - 2) && position[1] == (g->board_width - 2))
{
free(position);
return 1;
}
free(position);
}
return 0;
}
game take_turn(game g, move_direction direction)
{
unsigned* position = find_player(g);
// unsigned char valid = 0;
switch(direction)
{
case UP:
if(position[0] > 1 && !space_is_wall(g->board[position[0] - 1][position[1]]))
{
// g->board[position[0]][position[1]] = EMPTY;
g->board[position[0] - 1][position[1]] = PLAYER;
switch(g->p->previous_move)
{
case DOWN:
case UP:
g->board[position[0]][position[1]] = PVERTICAL;
break;
case LEFT:
g->board[position[0]][position[1]] = PCORNER_U_RIGHT;
break;
case RIGHT:
g->board[position[0]][position[1]] = PCORNER_U_LEFT;
break;
}
g->p->previous_move = direction;
}
break;
case LEFT:
if(position[1] > 1 && !space_is_wall(g->board[position[0]][position[1] - 1]))
{
// g->board[position[0]][position[1]] = EMPTY;
g->board[position[0]][position[1] - 1] = PLAYER;
switch(g->p->previous_move)
{
case DOWN:
g->board[position[0]][position[1]] = PCORNER_U_LEFT;
break;
case UP:
g->board[position[0]][position[1]] = PCORNER_LEFT;
break;
case RIGHT:
case LEFT:
g->board[position[0]][position[1]] = PHORIZONTAL;
break;
}
g->p->previous_move = direction;
}
break;
case RIGHT:
if(position[1] < g->board_width - 1 && !space_is_wall(g->board[position[0]][position[1] + 1]))
{
// g->board[position[0]][position[1]] = EMPTY;
g->board[position[0]][position[1] + 1] = PLAYER;
switch(g->p->previous_move)
{
case DOWN:
g->board[position[0]][position[1]] = PCORNER_U_RIGHT;
break;
case UP:
g->board[position[0]][position[1]] = PCORNER_RIGHT;
break;
case RIGHT:
case LEFT:
g->board[position[0]][position[1]] = PHORIZONTAL;
break;
}
g->p->previous_move = direction;
}
g->p->good_move_count++; // Player made a good move
break;
case DOWN:
if(position[0] < g->board_height - 1 && !space_is_wall(g->board[position[0] + 1][position[1]]))
{
// g->board[position[0]][position[1]] = EMPTY;
g->board[position[0] + 1][position[1]] = PLAYER;
switch(g->p->previous_move)
{
case DOWN:
case UP:
g->board[position[0]][position[1]] = PVERTICAL;
break;
case LEFT:
g->board[position[0]][position[1]] = PCORNER_RIGHT;
break;
case RIGHT:
g->board[position[0]][position[1]] = PCORNER_LEFT;
break;
}
g->p->previous_move = direction;
}
g->p->good_move_count++; // Player made a good move
break;
default:
err(5, "Invalid move direction");
}
if(position[0] == 1 && position[1] == 1) g->board[1][1] = START;
g->turn++;
return g;
}
game score_round(game g)
{
if(g)
{
size_t diagonal = g->board_width + g->board_height;
diagonal *= 1.10;
time_t time_diff = time(0) - g->start_time;
g->p->score += abs((((double)diagonal) / (((double)g->p->good_move_count) / (double)diagonal)) / (double)time_diff);
}
return g;
}