This repository was archived by the owner on May 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrid.h
More file actions
286 lines (247 loc) · 7.43 KB
/
grid.h
File metadata and controls
286 lines (247 loc) · 7.43 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
/**
* @file grid.h
* @author TEAM PINE
* @brief: exports functionality for the grid module.
* The grid module holds map data and offers
* interface functions for checking and updating the data as necessary.
* @version 0.1
* @date 2021-05-25
*
* @copyright Copyright (c) 2021
*
*/
#ifndef __GRID_H
#define __GRID_H
/* standard libs */
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <ctype.h>
#include <string.h>
#include <float.h>
#include <math.h>
#include "file.h" /* file operations */
#include "message.h" /* message operations */
typedef struct grid {
char** g;
int rows;
int cols;
} grid_t;
typedef struct player player_t;
typedef struct game gamestate_t;
/**
* @brief: function to initialize a grid instance to hold map data.
* This function allocates memory, which the caller
* must later free by callind grid_delete()
*
* Inputs:
* @param mapfile: FILE pointer to the source file containing
* the map data. It's expected to be open for reading.
*
* Returns:
* @return grid_t*: pointer to a grid instance containing the map data.
*/
grid_t* grid_init(FILE* mapfile);
/**
* @brief: function to initialize a grid for a new player.
* Rather than loading a player from FILE,
* this function takes in a pointer to a master grid
* and copies needed information from that grid instance.
*
* Inputs:
* @param masterGrid
*
* Returns:
* @return grid_t*: pointer to grid struct
*/
grid_t* grid_initForPlayer(grid_t* masterGrid);
bool grid_isPlayerVisible(gamestate_t* gamestate, grid_t* Grid, player_t* player, player_t* player2);
char* grid_toStringForPlayer(gamestate_t* state, player_t* current_player);
void grid_movePlayer(gamestate_t* gameState, player_t* player, int x, int y);
/**
* @brief: function to delete a grid instance
* and free all allocated memory.
* The grid struct should have been created
* via a call to either grid_init()
* or grid_initForPlayer()
*
* Inputs:
* @param grid: the grid instance
*
* Returns: None.
*/
void grid_delete(grid_t* grid);
/**
* @brief: function to get the number of rows in a grid instance.
*
* Inputs:
* @param grid: pointer to a grid instance
*
* Returns:
* @return int: number of rows.
* NOTE: we return -1 as a flag for a NULL grid.
* The caller may check that conditions
* in the return value to detect errors.
*/
int grid_getRows(grid_t* grid);
/**
* @brief: function to get the number of columns in a grid instance.
*
* Inputs:
* @param grid: pointer to a grid instance
*
* Returns:
* @return int: number of columns.
* NOTE: we return -1 as a flag for a NULL grid.
* The caller may check that conditions
* in the return value to detect errors.
*/
int grid_getColumns(grid_t* grid);
/**
* @brief: function to get the raw representation
* of the map from a grid struct.
*
* Inputs:
* @param grid: pointer to a grid struct.
* @return char**: an array of sentences (2D char array)
* NOTE: we do not allocate memory for the return value.
* The caller may NOT free the pointer returned herein.
*/
char** grid_getGrid(grid_t* grid);
/**
* @brief: function to check if a given point in the grid
* is a player.
*
* Inputs:
* @param grid: pointer to a grid struct holding map data.
* @param x: x position. of point to check in the map.
* @param y: y position of point to check in the map.
*
* Returns:
* @return true: given point is a wall.
* @return false: given point is NOT a wall.
*/
bool grid_isWall(grid_t* grid, int x, int y);
/**
* @brief: function to check if a given point in the grid
* is a player.
*
* Inputs:
* @param grid: pointer to a grid struct holding map data.
* @param x: x position. of point to check in the map.
* @param y: y position of point to check in the map.
*
* Returns:
* @return true: there's a player at the given point.
* @return false: there's no player at the given point.
*/
bool grid_isPlayer(grid_t* grid, int x, int y);
/**
* @brief: function to check if a given point in the grid
* contains gold.
*
* Inputs:
* @param grid: pointer to a grid struct holding map data.
* @param x: x position. of point to check in the map.
* @param y: y position of point to check in the map.
*
* Returns:
* @return true: given point contains gold.
* @return false: given point does NOT contain gold.
*/
bool grid_isGold(grid_t* grid, int x, int y);
/**
* @brief: function to check if a given point in the grid
* is a passage.
*
* Inputs:
* @param grid: pointer to a grid struct holdint map data.
* @param x: x position of point to check in the map.
* @param y: y position of point to check in the map.
*
* Returns:
* @return true: given point is a passage.
* @return false: given point is NOT a passage.
*/
bool grid_isPassage(grid_t* grid, int x, int y);
/**
* @brief: function to check if a given point in the grid
* is a playable space.
*
* Inputs:
* @param grid: pointer to a grid struct holdint map data.
* @param x: x position. of point to check in the map.
* @param y: y position of point to check in the map.
*
* Returns:
* @return true: given point is a wall.
* @return false: given point is NOT a wall.
*/
bool grid_isSpace(grid_t* grid, int x, int y);
/**
* @brief: function to check if a player can move in some
* direction given a key.
*
* Inputs:
* @param grid: pointer to a grid struct representing the map.
* @param player: pointer to a player trying to move.
* @param k: action character that triggered the call.
*
* Returns:
* @return true: the next point in given direction is a valid move.
* @return false: the next point in given direction NOT a valid move.
*/
bool grid_canMove(grid_t* master, player_t* player, char k);
void grid_calculateVisibility(grid_t* Grid, player_t* player);
/**
* @brief: function to convert master grid to string.
* This function takes in the grid and the gamestate
* for the current instance of the game.
* This is important because information from the gamestate
* is needed to locate and place other players in the
* stringified representation of the master grid.
*
* Inputs:
* @param masterGrid: pointer to the grid instance
* representing the master grid for the current session of the game.
* @param gamestate: pointer to the gamestate instance
* for the current session of the game.
*
* Returns:
* @return char*: a string representation of the master grid,
* with all the player information filled in.
*
* NOTE: This function allocates memory for the returned string
* via calls to another function. The caller of this function
* must later call free() on the returned pointer.
*
*/
char* grid_masterGridToString(grid_t* masterGrid, gamestate_t* state);
/**
* @brief: function to generate an identical copy of a grid instance.
*
* Inputs:
* @param original_grid
*
* Returns:
* @return grid_t*: a pointer to a grid struct
* identical to the one that was passed in as an argument.
*
* NOTE: this function allocates memory for the copy of the grid
* through calls to other functions. The caller of this function
* must later free that pointer by calling grid_delete().
*/
grid_t* grid_copy(grid_t* original_grid);
/**
* @brief: function to generate a string representation
* of a grid instance.
*
* Inputs:
* @param state: pointer to gamestate for the current game instance
* @param grid: pointer to the grid to be stringified
*
* Returns:
* @return char*: a string representation of the grid.
*/
char* grid_toString(gamestate_t* state, grid_t* grid);
#endif /* __GRID_H */