-
Notifications
You must be signed in to change notification settings - Fork 12
/
state.c
211 lines (177 loc) · 5.01 KB
/
state.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
#include "state.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "snake_utils.h"
/* Helper function definitions */
static void set_board_at(game_state_t* state, unsigned int row, unsigned int col, char ch);
static bool is_tail(char c);
static bool is_head(char c);
static bool is_snake(char c);
static char body_to_tail(char c);
static char head_to_body(char c);
static unsigned int get_next_row(unsigned int cur_row, char c);
static unsigned int get_next_col(unsigned int cur_col, char c);
static void find_head(game_state_t* state, unsigned int snum);
static char next_square(game_state_t* state, unsigned int snum);
static void update_tail(game_state_t* state, unsigned int snum);
static void update_head(game_state_t* state, unsigned int snum);
/* Task 1 */
game_state_t* create_default_state() {
// TODO: Implement this function.
return NULL;
}
/* Task 2 */
void free_state(game_state_t* state) {
// TODO: Implement this function.
return;
}
/* Task 3 */
void print_board(game_state_t* state, FILE* fp) {
// TODO: Implement this function.
return;
}
/*
Saves the current state into filename. Does not modify the state object.
(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 */
/*
Helper function to get a character from the board
(already implemented for you).
*/
char get_board_at(game_state_t* state, unsigned int row, unsigned int col) {
return state->board[row][col];
}
/*
Helper function to set a character on the board
(already implemented for you).
*/
static void set_board_at(game_state_t* state, unsigned int row, unsigned int col, char ch) {
state->board[row][col] = ch;
}
/*
Returns true if c is part of the snake's tail.
The snake consists of these characters: "wasd"
Returns false otherwise.
*/
static bool is_tail(char c) {
// TODO: Implement this function.
return true;
}
/*
Returns true if c is part of the snake's head.
The snake consists of these characters: "WASDx"
Returns false otherwise.
*/
static bool is_head(char c) {
// TODO: Implement this function.
return true;
}
/*
Returns true if c is part of the snake.
The snake consists of these characters: "wasd^<v>WASDx"
*/
static bool is_snake(char c) {
// TODO: Implement this function.
return true;
}
/*
Converts a character in the snake's body ("^<v>")
to the matching character representing the snake's
tail ("wasd").
*/
static char body_to_tail(char c) {
// TODO: Implement this function.
return '?';
}
/*
Converts a character in the snake's head ("WASD")
to the matching character representing the snake's
body ("^<v>").
*/
static char head_to_body(char c) {
// TODO: Implement this function.
return '?';
}
/*
Returns cur_row + 1 if c is 'v' or 's' or 'S'.
Returns cur_row - 1 if c is '^' or 'w' or 'W'.
Returns cur_row otherwise.
*/
static unsigned int get_next_row(unsigned int cur_row, char c) {
// TODO: Implement this function.
return cur_row;
}
/*
Returns cur_col + 1 if c is '>' or 'd' or 'D'.
Returns cur_col - 1 if c is '<' or 'a' or 'A'.
Returns cur_col otherwise.
*/
static unsigned int get_next_col(unsigned int cur_col, char c) {
// TODO: Implement this function.
return cur_col;
}
/*
Task 4.2
Helper function for update_state. Return the character in the cell the snake is moving into.
This function should not modify anything.
*/
static char next_square(game_state_t* state, unsigned int snum) {
// TODO: Implement this function.
return '?';
}
/*
Task 4.3
Helper function for update_state. Update the head...
...on the board: add a character where the snake is moving
...in the snake struct: update the row and col of the head
Note that this function ignores food, walls, and snake bodies when moving the head.
*/
static void update_head(game_state_t* state, unsigned int snum) {
// TODO: Implement this function.
return;
}
/*
Task 4.4
Helper function for update_state. Update the tail...
...on the board: blank out the current tail, and change the new
tail from a body character (^<v>) into a tail character (wasd)
...in the snake struct: update the row and col of the tail
*/
static void update_tail(game_state_t* state, unsigned int snum) {
// TODO: Implement this function.
return;
}
/* Task 4.5 */
void update_state(game_state_t* state, int (*add_food)(game_state_t* state)) {
// TODO: Implement this function.
return;
}
/* Task 5 */
game_state_t* load_board(char* filename) {
// TODO: Implement this function.
return NULL;
}
/*
Task 6.1
Helper function for initialize_snakes.
Given a snake struct with the tail row and col filled in,
trace through the board to find the head row and col, and
fill in the head row and col in the struct.
*/
static void find_head(game_state_t* state, unsigned int snum) {
// TODO: Implement this function.
return;
}
/* Task 6.2 */
game_state_t* initialize_snakes(game_state_t* state) {
// TODO: Implement this function.
return NULL;
}