-
Notifications
You must be signed in to change notification settings - Fork 0
/
frog.c
336 lines (280 loc) · 6.96 KB
/
frog.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
//======================================================
// frog.c
//
// COMP 3430 - Operating Systems - Assignment 2 Frogger
//
// Game Entity - Frog
//
// @author Andy Lun
// @date June 04, 2017
//======================================================
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "board.h"
#include "console.h"
#include "frog.h"
#include "linked_list.h"
#include "log.h"
#include "threads.h"
#define NUM_FROG_GRAPHICS 2
#define FROG_ENTITY_SIZE 2
#define INIT_FROG_ROWS BOARD_ROWS - FROG_ENTITY_SIZE - 1
#define INIT_FROG_COLS BOARD_COLS/2 - FROG_ENTITY_SIZE/2
#define MAX_FROG_LIVES 4
#define SLEEP_TICKS_FROG 100
//=====================================
// GRAPHICS
//=====================================
static char* FROG_GRAPHICS[NUM_FROG_GRAPHICS][FROG_ENTITY_SIZE + 1] = {
{"@@",
"<>"},
{"--",
"<>"}
};
//=====================================
// STRUCT
//=====================================
struct frog_t
{
int graphics_index;
int row;
int column;
int tier;
int lives;
};
int get_frog_graphics_index(const frog_t* frog) { return frog->graphics_index; }
void set_frog_graphics_index(frog_t* frog, int index) { frog->graphics_index = index; }
int get_frog_row(const frog_t* frog) { return frog->row; }
void set_frog_row(frog_t* frog, int row) { frog->row = row; }
int get_frog_column(const frog_t* frog) { return frog->column; }
void set_frog_column(frog_t* frog, int column) { frog->column = column; }
int get_frog_tier(const frog_t* frog) { return frog->tier; }
void set_frog_tier(frog_t* frog, int tier) { frog->tier = tier; }
int get_frog_lives(const frog_t* frog) { return frog->lives; }
void set_frog_lives(frog_t* frog, int lives) { frog->lives = lives; }
//=====================================
// FUNCTIONS
//=====================================
frog_t*
init_frog()
{
frog_t* frog = (frog_t*) malloc(sizeof(frog_t));
lock_mutex(&mutex_frog);
set_frog_graphics_index(frog, 0);
set_frog_row(frog, INIT_FROG_ROWS);
set_frog_column(frog, INIT_FROG_COLS);
set_frog_tier(frog, 0);
set_frog_lives(frog, MAX_FROG_LIVES);
unlock_mutex(&mutex_frog);
return frog;
}
void
reset_frog(frog_t* frog, bool dead_frog)
{
lock_mutex(&mutex_frog);
set_frog_row(frog, INIT_FROG_ROWS);
set_frog_column(frog, INIT_FROG_COLS);
set_frog_tier(frog, 0);
int curr_frog_lives = get_frog_lives(frog);
set_frog_lives(frog, (dead_frog) ? (curr_frog_lives - 1) : curr_frog_lives);
unlock_mutex(&mutex_frog);
move_frog(frog, 0, 0);
}
void
destroy_frog(frog_t* frog)
{
free(frog);
}
int
get_frog_jump_distance(const frog_t* frog, int direction)
{
int distance = 0;
switch(get_frog_tier(frog))
{
default:
distance = 4;
break;
case MAX_LOG_TIERS:
if (UP == direction)
{
distance = 3;
}
else if (DOWN == direction)
{
distance = 4;
}
break;
}
return distance * direction;
}
bool
is_frog_within_bounds(int row, int column)
{
bool within_bounds = false;
if (SCR_LEFT <= column && SCR_LEFT + BOARD_COLS - FROG_ENTITY_SIZE >= column &&
SCR_TOP <= row && SCR_TOP + BOARD_ROWS - FROG_ENTITY_SIZE >= row)
{
within_bounds = true;
}
return within_bounds;
}
void
draw_frog(int row, int column)
{
lock_mutex(&mutex_frog);
char** frog_tile = FROG_GRAPHICS[get_frog_graphics_index(frog)];
unlock_mutex(&mutex_frog);
lock_mutex(&mutex_console);
consoleDrawImage(row, column, frog_tile, FROG_ENTITY_SIZE);
unlock_mutex(&mutex_console);
}
void
move_frog(frog_t* frog, int row_offset, int column_offset)
{
lock_mutex(&mutex_frog);
char** frog_tile = FROG_GRAPHICS[get_frog_graphics_index(frog)];
int prev_row = get_frog_row(frog);
int prev_column = get_frog_column(frog);
int next_row = prev_row + row_offset;
int next_column = prev_column + column_offset;
bool redraw = false;
if (is_frog_within_bounds(next_row, next_column))
{
set_frog_row(frog, next_row);
set_frog_column(frog, next_column);
if (prev_row != next_row)
{
int direction = (row_offset < 0) ? 1 : -1;
set_frog_tier(frog, (get_frog_tier(frog) + direction));
}
redraw = (MAX_LOG_TIERS + 1 != get_frog_tier(frog));
}
unlock_mutex(&mutex_frog);
lock_mutex(&mutex_console);
consoleClearImage(prev_row, prev_column, FROG_ENTITY_SIZE, FROG_ENTITY_SIZE);
if (redraw)
{
consoleDrawImage(next_row, next_column, frog_tile, FROG_ENTITY_SIZE);
}
else
{
consoleDrawImage(prev_row, prev_column, frog_tile, FROG_ENTITY_SIZE);
}
unlock_mutex(&mutex_console);
}
void
switch_frog_graphics(frog_t* frog)
{
lock_mutex(&mutex_frog);
set_frog_graphics_index(frog, ((get_frog_graphics_index(frog) + 1) % NUM_FROG_GRAPHICS));
char** frog_tile = FROG_GRAPHICS[get_frog_graphics_index(frog)];
int row = get_frog_row(frog);
int column = get_frog_column(frog);
unlock_mutex(&mutex_frog);
lock_mutex(&mutex_console);
consoleClearImage(row, column, FROG_ENTITY_SIZE, FROG_ENTITY_SIZE);
consoleDrawImage(row, column, frog_tile, FROG_ENTITY_SIZE);
unlock_mutex(&mutex_console);
}
void
print_frog_lives(const frog_t* frog)
{
lock_mutex(&mutex_frog);
int lives = get_frog_lives(frog);
unlock_mutex(&mutex_frog);
char char_lives[2];
snprintf(char_lives, 2, "%d", lives);
lock_mutex(&mutex_console);
putString (char_lives, SCR_TOP, 42, 5);
unlock_mutex(&mutex_console);
}
bool
get_input(int* movement_buffer)
{
char input;
fd_set readset;
FD_ZERO(&readset);
FD_SET(STDIN_FILENO, &readset);
struct timespec timeout = getTimeout(1);
bool quit = false;
memset(movement_buffer, 0, sizeof(int) * 2);
if (-1 == pselect(FD_SETSIZE, &readset, NULL, NULL, &timeout, NULL))
{
perror("select");
quit = true;
}
else
{
if (-1 == read(STDIN_FILENO, &input, 1))
{
perror("read");
quit = true;
}
else
{
switch(input)
{
case 'w':
movement_buffer[0] = UP;
break;
case 'a':
movement_buffer[1] = LEFT;
break;
case 's':
movement_buffer[0] = DOWN;
break;
case 'd':
movement_buffer[1] = RIGHT;
break;
case 'q':
quit = true;
break;
}
}
}
return quit;
}
//=====================================
// THREAD FUNCTIONS
//=====================================
void*
animate_frog(void* frog)
{
while (!end_of_game)
{
switch_frog_graphics(frog);
sleep_thread(SLEEP_TICKS_FROG);
}
pthread_exit(NULL);
}
void*
jump_frog(void* frog)
{
int* movement_buffer;
int row;
int column;
bool quit = false;
movement_buffer = (int*) malloc(sizeof(int) * 2);
while (!quit && !end_of_game)
{
if (!(quit = get_input(movement_buffer)) &&
(0 != (row = movement_buffer[0]) || 0 != (column = movement_buffer[1])))
{
move_frog(frog, get_frog_jump_distance(frog, row), column);
if (!add_frog(get_frog_tier(frog), get_frog_column(frog)) && 0 != get_frog_tier(frog))
{
bool dead_frog = true;
if (MAX_LOG_TIERS + 1 == get_frog_tier(frog))
{
dead_frog = !jump_into_slot(get_frog_column(frog));
}
reset_frog(frog, dead_frog);
}
}
}
trigger_condition();
pthread_exit(NULL);
}