-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.h
276 lines (247 loc) · 8.19 KB
/
game.h
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
/**
* @file game.h
* @brief Basic Game Functions.
* @details See @ref index for further details.
* @copyright University of Bordeaux. All rights reserved, 2020.
**/
#ifndef __GAME_H__
#define __GAME_H__
#include <stdbool.h>
/**
* @brief Standard unsigned integer type.
**/
typedef unsigned int uint;
/**
* @brief Size of the default game grid.
**/
#define DEFAULT_SIZE 8
/**
* @brief Different kinds of square used in the game.
**/
typedef enum {
EMPTY = 0, /**< empty square */
TREE = 1, /**< tree square */
TENT = 2, /**< tent square */
GRASS = 3, /**< grass square */
} square;
/**
* @brief Different kinds of move used in the game.
**/
enum {
ILLEGAL = 0, /**< illegal move */
REGULAR = 1, /**< regular move */
LOSING = 2, /**< losing move */
};
/**
* @brief The structure pointer that stores the game state.
**/
typedef struct game_s *game;
/**
* @brief The structure constant pointer that stores the game state.
* @details That means that it is not possible to modify the game using this
* pointer.
**/
typedef const struct game_s *cgame;
/**
* @brief Creates a new game with default size and initializes it.
* @param squares an array describing the initial square values (row-major
* storage)
* @param nb_tents_row an array with the expected number of tents in each row
* @param nb_tents_col an array with the expected number of tents in each column
* @pre @p squares must be an initialized array of default size squared.
* @pre @p nb_tents_row must be an initialized array of default size.
* @pre @p nb_tents_col must be an initialized array of default size.
* @return the created game
**/
game game_new(square *squares, uint *nb_tents_row, uint *nb_tents_col);
/**
* @brief Creates a new empty game with defaut size.
* @details All squares are initialized with empty squares, and the expected
* numbers of tents for each row/column are set to zero.
* @return the created game
**/
game game_new_empty(void);
/**
* @brief Duplicates a game.
* @param g the game to copy
* @return the copy of the game
* @pre @p g must be a valid pointer toward a game structure.
**/
game game_copy(cgame g);
/**
* @brief Tests if two games are equal.
* @param g1 the first game
* @param g2 the second game
* @return true if the two games are equal, false otherwise
* @pre @p g1 must be a valid pointer toward a game structure.
* @pre @p g2 must be a valid pointer toward a game structure.
**/
bool game_equal(cgame g1, cgame g2);
/**
* @brief Deletes the game and frees the allocated memory.
* @param g the game to delete
* @pre @p g must be a valid pointer toward a game structure.
**/
void game_delete(game g);
/**
* @brief Sets the value of a given square.
* @details This function is useful for initializing the squares of an empty
* game.
* @param g the game
* @param i row index
* @param j column index
* @param s the square value
* @pre @p g must be a valid pointer toward a game structure.
* @pre @p i < game width
* @pre @p j < game height
* @pre @p s must be either EMPTY, GRASS, TENT or TREE.
**/
void game_set_square(game g, uint i, uint j, square s);
/**
* @brief Gets the value of a given square.
* @param g the game
* @param i row index
* @param j column index
* @pre @p g must be a valid pointer toward a game structure.
* @pre @p i < game width
* @pre @p j < game height
* @return the square value
**/
square game_get_square(cgame g, uint i, uint j);
/**
* @brief Sets the expected number of tents in a given row.
* @param g the game
* @param i row index
* @param nb_tents the expected number of tents on this row
* @pre @p g must be a valid pointer toward a game structure.
* @pre @p i < game width
**/
void game_set_expected_nb_tents_row(game g, uint i, uint nb_tents);
/**
* @brief Sets the expected number of tents in a given column.
* @param g the game
* @param j column index
* @param nb_tents the expected number of tents on this column
* @pre @p g must be a valid pointer toward a game structure.
* @pre @p j < game height
**/
void game_set_expected_nb_tents_col(game g, uint j, uint nb_tents);
/**
* @brief Gets the expected number of tents in a given row.
* @param g the game
* @param i row index
* @return the expected number of tents on this row
* @pre @p g must be a valid pointer toward a game structure.
* @pre @p i < game width
**/
uint game_get_expected_nb_tents_row(cgame g, uint i);
/**
* @brief Gets the expected number of tents in a given column.
* @param g the game
* @param j column index
* @return the expected number of tents on this column
* @pre @p g must be a valid pointer toward a game structure.
* @pre @p j < game height
**/
uint game_get_expected_nb_tents_col(cgame g, uint j);
/**
* @brief Gets the expected number of tents on the whole grid.
* @param g the game
* @return the expected number of tents on this game
* @pre @p g must be a valid pointer toward a game structure.
**/
uint game_get_expected_nb_tents_all(cgame g);
/**
* @brief Gets the current number of tents in a given row.
* @param g the game
* @param i row index
* @return the current number of tents on this row
* @pre @p g must be a valid pointer toward a game structure.
* @pre @p i < game width
**/
uint game_get_current_nb_tents_row(cgame g, uint i);
/**
* @brief Gets the current number of tents in a given column.
* @param g the game
* @param j column index
* @return the expected number of tents on this column
* @pre @p g must be a valid pointer toward a game structure.
* @pre @p j < game height
**/
uint game_get_current_nb_tents_col(cgame g, uint j);
/**
* @brief Gets the current number of tents on the whole grid.
* @param g the game
* @return the current number of tents on this game
* @pre @p g must be a valid pointer toward a game structure.
**/
uint game_get_current_nb_tents_all(cgame g);
/**
* @brief Plays a move in a given square.
* @details This function allows to play both a regular or a losing move, but
* not an illegal move (see @ref index).
* @param g the game
* @param i row index
* @param j column index
* @param s the square value
* @pre @p g must be a valid pointer toward a game structure.
* @pre @p i < game width
* @pre @p j < game height
* @pre @p s must be either EMPTY, GRASS or TENT (but not TREE).
* @pre The game square at position (i,j) must not be TREE.
**/
void game_play_move(game g, uint i, uint j, square s);
/**
* @brief Checks if a given move in a square is regular
* @details This function checks that playing a move in a square is a regular
* move (see @ref index).
* @param g the game
* @param i row index
* @param j column index
* @param s the square value
* @return either REGULAR, LOSING or ILLEGAL depending on the move
* @pre @p g must be a valid pointer toward a game structure.
* @pre @p i < game width
* @pre @p j < game height
* @pre @p s must be either EMPTY, GRASS, TENT or TREE.
**/
int game_check_move(cgame g, uint i, uint j, square s);
/**
* @brief Checks if the game is won.
* @param g the game
* @details This function checks that all the game rules (decribed on @ref
* index) are satisfied, and more specifically that all the tents are correctly
* placed. The remaining squares are either empty square or grass square.
* @return true if the game ended successfully, false otherwise
* @pre @p g must be a valid pointer toward a game structure.
**/
bool game_is_over(cgame g);
/**
* @brief Fills a row with grass.
* @details This function only puts grass on empty squares, without modifying
* the other squares.
* @param g the game
* @param i row index
* @pre @p g must be a valid pointer toward a game structure.
* @pre @p i < game width
*/
void game_fill_grass_row(game g, uint i);
/**
* @brief Fills a column with grass.
* @details This function only puts grass on empty squares, without modifying
* the other squares.
* @param g the game
* @param j column index
* @pre @p g must be a valid pointer toward a game structure.
* @pre @p j < game height
*/
void game_fill_grass_col(game g, uint j);
/**
* @brief Restarts a game.
* @details All the game is reset to its initial state. In particular, all the
* squares except trees are reset to empty state.
* @param g the game
* @pre @p g must be a valid pointer toward a game structure.
**/
void game_restart(game g);
#endif // __GAME_H__