Repository navigation
Expand file tree
/
Copy pathstate.c
More file actions
393 lines (337 loc) · 9.87 KB
/
Copy pathstate.c
File metadata and controls
393 lines (337 loc) · 9.87 KB
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "snake_utils.h"
#include "state.h"
/* Helper function definitions */
static char get_board_at(game_state_t* state, int x, int y);
static void set_board_at(game_state_t* state, int x, int y, char ch);
static bool is_tail(char c);
static bool is_snake(char c);
static char body_to_tail(char c);
static int incr_x(char c);
static int incr_y(char c);
static void find_head(game_state_t* state, int snum);
static char next_square(game_state_t* state, int snum);
static void update_tail(game_state_t* state, int snum);
static void update_head(game_state_t* state, int snum);
/* Helper function to get a character from the board (already implemented for you). */
static char get_board_at(game_state_t* state, int x, int y) {
return state->board[y][x];
}
/* Helper function to set a character on the board (already implemented for you). */
static void set_board_at(game_state_t* state, int x, int y, char ch) {
state->board[y][x] = ch;
}
/* Task 1 */
game_state_t* create_default_state() {
// TODO: Implement this function.
snake_t* default_snake;
default_snake = (snake_t*)malloc(sizeof(snake_t)+1);
default_snake->tail_x = 4;
default_snake->tail_y = 4;
default_snake->head_y = 4; //y axis, row number
default_snake->head_x = 5; //x axis, column number
default_snake->live = true;
game_state_t* default_state;
default_state = (game_state_t*)malloc(sizeof(game_state_t)+1);
//default_state->num_rows = 18;
default_state->x_size = 14;
default_state->y_size = 10;
default_state->num_snakes = 1;
default_state->snakes = default_snake;
char** board_arr = (char**)malloc(sizeof(char*)*15+1);
int row = 14;
int col = 10;
int i, j;
for (i = 0; i < row; i++)
board_arr[i] = (char*)malloc(sizeof(char)*14+1);
for (i = 0; i < col; i++)
for (j = 0; j < row; j++)
if (j == 0 || i == 0 || j == 13 || i == 9) {
board_arr[i][j] = '#';
}
else if (j == 9 && i == 2) {
board_arr[i][j] = '*';
}
else if (j == default_snake->tail_x && i == default_snake->tail_y) {
board_arr[i][j] = 'd';
}
else if (j == default_snake->head_x && i == default_snake->head_y) {
board_arr[i][j] = '>';
}
else {
board_arr[i][j] = ' ';
}
default_state->board = board_arr;
return default_state;
}
/* Task 2 */
void free_state(game_state_t* state) {
// TODO: Implement this function.
free(state->snakes);
int i;
for (i = 0; i < state->y_size; i++)
free(state->board[i]);
free(state->board);
free(state);
return;
}
/* Task 3 */
void print_board(game_state_t* state, FILE* fp) {
// TODO: Implement this function.
char** board_arr = state->board;
int i;
int y;
y = state->y_size;
for (i = 0; i < y; i++)
fprintf(fp,"%s\n", board_arr[i]);
return;
}
/* Saves the current state into filename (already implemented for you). */
void save_board(game_state_t* state, char* filename) {
FILE* f = fopen(filename, "w");
print_board(state, f);
fclose(f);
}
/* Task 4.1 */
static bool is_tail(char c) {
// TODO: Implement this function.
if (c == 'w' || c == 's' || c == 'a' || c== 'd') {
return true;
}
return false;
}
static bool is_snake(char c) {
// TODO: Implement this function.
if (c == 'w' || c == 's' || c == 'a' || c== 'd' || c=='^' || c=='<' || c=='>' || c=='v' ) {
return true;
}
return false;
}
static bool is_snake_body(char c) {
// TODO: Implement this function.
if (c=='^' || c=='<' || c=='>' || c=='v' ) {
return true;
}
return false;
}
static char body_to_tail(char c) {
// TODO: Implement this function.
if (c == '^'){
return 'w';
} else if ( c == '<') {
return 'a';
} else if ( c == 'v') {
return 's';
} else if (c == '>') {
return 'd';
}
return '?';
}
static int incr_x(char c) {
// TODO: Implement this function.
if (c == '>'|| c == 'd') {
return 1;
} else if (c == '<'|| c == 'a') {
return -1;
}
return 0;
}
static int incr_y(char c) {
// TODO: Implement this function.
if (c == 'v'|| c == 's') {
return 1;
} else if (c == '^'|| c == 'w') {
return -1;
}
return 0;
}
/* Task 4.2 */
static char next_square(game_state_t* state, int snum) {
// TODO: Implement this function.
int tail_loc_row;
int tail_loc_col;
tail_loc_col = state->snakes[snum].head_x;
tail_loc_row = state->snakes[snum].head_y;
int x = incr_x(state->board[tail_loc_row][tail_loc_col]);
int y = incr_y(state->board[tail_loc_row][tail_loc_col]);
tail_loc_col += x;
tail_loc_row += y;
return state->board[tail_loc_row][tail_loc_col];
}
/* Task 4.3 */
static void update_head(game_state_t* state, int snum) {
// TODO: Implement this function.
//row number and column number of the head of the snake
int head_loc_row;
int head_loc_col;
head_loc_col = state->snakes[snum].head_x;
head_loc_row = state->snakes[snum].head_y;
//find the head direction of the snake (^<v>)
char head_char = state->board[head_loc_row][head_loc_col];
//update the head of the snake
int new_head_loc_row = head_loc_row + incr_y(state->board[head_loc_row][head_loc_col]);
int new_head_loc_col = head_loc_col + incr_x(state->board[head_loc_row][head_loc_col]);
//update head info to the snake struct
state->snakes[snum].head_x = new_head_loc_col;
state->snakes[snum].head_y = new_head_loc_row;
//update head char
state->board[new_head_loc_row][new_head_loc_col] = head_char;
return;
}
/* Task 4.4 */
static void update_tail(game_state_t* state, int snum) {
// TODO: Implement this function.
//row number and column number of the tail of the snake
int tail_loc_row;
int tail_loc_col;
tail_loc_col = state->snakes[snum].tail_x;
tail_loc_row = state->snakes[snum].tail_y;
//update the tail of the snake
int new_tail_loc_row = tail_loc_row + incr_y(state->board[tail_loc_row][tail_loc_col]);
int new_tail_loc_col = tail_loc_col + incr_x(state->board[tail_loc_row][tail_loc_col]);
//setting the old location to be empty
state->board[tail_loc_row][tail_loc_col] = ' ';
//body to tail
state->board[new_tail_loc_row][new_tail_loc_col] = body_to_tail(state->board[new_tail_loc_row][new_tail_loc_col]);
//update tail info to the snake struct
state->snakes[snum].tail_x = new_tail_loc_col;
state->snakes[snum].tail_y = new_tail_loc_row;
return;
}
/* Task 4.5 */
void update_state(game_state_t* state, int (*add_food)(game_state_t* state)) {
// TODO: Implement this function.
int num = state->num_snakes;
for (int i = 0; i < num; i++) {
char next_s = next_square(state, i);
// printf("%c\n", next_s);
if (next_s == '#' || is_snake(next_s)) {
int x_pos = state->snakes[i].head_x;
int y_pos = state->snakes[i].head_y;
state->board[y_pos][x_pos] = 'x';
state->snakes->live = false;
} else if (next_s == '*') {
update_head(state, i);
add_food(state);
} else if (next_s == ' ') {
update_head(state, i);
update_tail(state, i);
}
}
return;
}
/* Task 5 */
game_state_t* load_board(char* filename) {
// TODO: Implement this function.
game_state_t* state_ptr = (game_state_t*)malloc(sizeof(game_state_t)+1);
FILE *fp = fopen(filename,"r");
char c = fgetc(fp);;
int row = 0;
while (c!='\n') {
c = fgetc(fp);
row++;
}
rewind(fp);
int col = 0;
while (c!= EOF) {
c = fgetc(fp);
if (c == '\n') {
col++;
}
}
state_ptr->x_size = row;
state_ptr->y_size = col;
state_ptr->board = (char**)malloc(sizeof(char*)*(col));
for (int i = 0; i < col; i++) {
state_ptr->board[i] = (char*)malloc(sizeof(char)*(row));
}
rewind(fp);
for (int c = 0; c < col; c++) {
for (int r = 0; r < row+1; r++) {
char l = fgetc(fp);
if (l == '\0') {
printf("%c", 'n');
}
if (l == '\n') {
state_ptr->board[c][r] = '\0';
} else {
state_ptr->board[c][r] = l;
}
}
}
return state_ptr;
}
/* Task 6.1 */
static void find_head(game_state_t* state, int snum) {
// TODO: Implement this function.
int counter = -1;
int tail_loc_row;
int tail_loc_col;
int i;
int j;
for (i = 0; i < state->y_size; i++) {
if (counter == snum) {
break;
}
for (j = 0; j < state->x_size; j++){
if (is_tail(state->board[i][j])) {
counter++;
if (counter == snum) {
tail_loc_row = i;
tail_loc_col = j;
break;
}
}
}
}
while (is_snake(state->board[tail_loc_row][tail_loc_col])) {
int x = incr_x(state->board[tail_loc_row][tail_loc_col]);
int y = incr_y(state->board[tail_loc_row][tail_loc_col]);
if (!is_snake(state->board[tail_loc_row + y][tail_loc_col + x])) {
break;
}
tail_loc_col += x;
tail_loc_row += y;
}
state->snakes[snum].head_x = tail_loc_col;
state->snakes[snum].head_y = tail_loc_row;
}
/* Task 6.2 */
game_state_t* initialize_snakes(game_state_t* state) {
// TODO: Implement this function.
int total_snake_num = 0;
int y = state->y_size;
int x = state->x_size;
//printf("%d\n",state->x_size);
for (int r = 0; r < y; r++) {
for (int c = 0; c < x; c++) {
if (is_tail(state->board[r][c])) {
total_snake_num++;
}
}
}
state->num_snakes = total_snake_num;
state->snakes = (snake_t*)malloc(sizeof(snake_t)*(total_snake_num));
state->snakes->live = true;
int i = 0;
for (int r = 0; r < state->y_size; r++) {
for (int c = 0; c < state->x_size; c++) {
if (is_tail(state->board[r][c])) {
state->snakes[i].tail_x = c;
state->snakes[i].tail_y = r;
find_head(state,i);
i++;
if (i == total_snake_num) {
break;
}
}
}
if (i == total_snake_num) {
break;
}
}
return state;
}