-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.c
More file actions
334 lines (317 loc) · 9.47 KB
/
game.c
File metadata and controls
334 lines (317 loc) · 9.47 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
#include "game.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "libs/gets.h"
#include "libs/compares.h"
int init_field(Game *game, Field *field)
{
field->points = (bool **) calloc(game->height, sizeof(bool *));
if (field->points == NULL)
{
return MEMORY_ALLOCATION_ERROR;
}
for (size_t i = 0; i < game->height; ++i)
{
field->points[i] = (bool *) calloc(game->width, sizeof(bool));
if (field->points[i] == NULL)
{
return MEMORY_ALLOCATION_ERROR;
}
}
return SUCCESS;
}
void free_field(Game *game, Field *field)
{
if (field != NULL)
{
if (field->points != NULL)
{
for (size_t j = 0; j < game->height; ++j)
{
if (field->points[j] != NULL)
{
free(field->points[j]);
}
}
free(field->points);
}
free(field);
}
}
int create_new_point(Game *game, Field *field, Point *point)
{
if (point->x >= game->width || point->y >= game->height)
{
return COORDINATE_ERROR;
}
field->points[point->y][point->x] = true;
return SUCCESS;
}
unsigned short int count_neighbours(Game *game, Field *field, Point *point)
{
Point left_top = {(size_t) (((long int) point->x - 1 < 0) * (game->width - 1)) + (size_t) (((long int) point->x - 1 >= 0) * (point->x - 1)),
(size_t) (((long int) point->y - 1 < 0) * (game->height - 1)) + (size_t) (((long int) point->y - 1 >= 0) * (point->y - 1))};
Point left = {(size_t) (((long int) point->x - 1 < 0) * (game->width - 1)) + (size_t) (((long int) point->x - 1 >= 0) * (point->x - 1)),
point->y};
Point left_bottom = {(size_t) (((long int) point->x - 1 < 0) * (game->width - 1)) + (size_t) (((long int) point->x - 1 >= 0) * (point->x - 1)),
(point->y + 1) % game->height};
Point right_top = {(point->x + 1) % game->width,
(size_t) (((long int) point->y - 1 < 0) * (game->height - 1)) + (size_t) (((long int) point->y - 1 >= 0) * (point->y - 1))};
Point right = {(point->x + 1) % game->width,
point->y};
Point right_bottom = {(point->x + 1) % game->width,
(point->y + 1) % game->height};
Point top = {point->x, (size_t) (((long int) point->y - 1 < 0) * (game->height - 1)) + (size_t) (((long int) point->y - 1 >= 0) * (point->y - 1))};
Point bottom = {point->x, (point->y + 1) % game->height};
const size_t len = 8;
Point *neighbours[] = {&left_top, &left, &left_bottom, &right_top, &right, &right_bottom, &top, &bottom};
unsigned short int count = 0;
for (size_t i = 0; i < len; ++i)
{
if (field->points[neighbours[i]->y][neighbours[i]->x] == true)
{
++count;
}
}
return count;
}
void generate_new_points(Game *game, Field *old_field)
{
for (size_t i = 0; i < game->height; ++i)
{
for (size_t j = 0; j < game->width; ++j)
{
Point point = {j, i};
unsigned short int count = count_neighbours(game, old_field, &point);
if (count == 3)
{
game->active_field->points[i][j] = true;
} else if (count < 2 || count > 3)
{
game->active_field->points[i][j] = false;
} else {
game->active_field->points[i][j] = old_field->points[i][j];
}
}
}
}
int to_next_gen(Game *game) // Доработать
{
Field *old_field = NULL;
if (game->active_field != NULL)
{
push_CyclicStack(game->history_field, &(game->active_field), &old_field);
if (is_full_CyclicStack(game->history_field))
{
free_field(game, old_field);
}
old_field = game->active_field;
}
game->active_field = (Field *) malloc(sizeof(Field));
if (game->active_field == NULL)
{
return MEMORY_ALLOCATION_ERROR;
}
if (init_field(game, game->active_field) == MEMORY_ALLOCATION_ERROR)
{
return MEMORY_ALLOCATION_ERROR;
}
if (old_field != NULL)
{
generate_new_points(game, old_field);
}
return SUCCESS;
}
void to_prev_gen(Game *game)
{
if (!is_empty_CyclicStack(game->history_field))
{
free_field(game, game->active_field);
pop_CyclicStack(game->history_field, &(game->active_field));
}
}
void free_game(Game *game)
{
while (!is_empty_CyclicStack(game->history_field))
{
Field *field;
pop_CyclicStack(game->history_field, &field);
free_field(game, field);
}
free_field(game, game->active_field);
free_CyclicStack(game->history_field);
}
int start_event(Game *game, int event)
{
switch (event)
{
case TO_NEXT_GEN:
if (to_next_gen(game) == MEMORY_ALLOCATION_ERROR) {
return MEMORY_ALLOCATION_ERROR;
}
break;
case TO_PREV_GEN:
to_prev_gen(game);
break;
default:
{
return NON_EXISTENT_EVENT;
break;
}
}
return SUCCESS;
}
int create_history(Game *game)
{
if (init_CyclicStack(&(game->history_field), game->capacity, sizeof(Field *)) == MEMORY_ALLOCATION_ERROR)
{
return MEMORY_ALLOCATION_ERROR;
}
return SUCCESS;
}
void render(Game *game)
{
for (size_t i = 0; i < game->height; ++i)
{
printf("+");
for (size_t j = 0; j < game->width; ++j)
{
printf("-+");
}
printf("\n|");
for (size_t j = 0; j < game->width; ++j)
{
if (game->active_field->points[i][j] == true)
{
printf("▉|");
} else
{
printf(" |");
}
}
printf("\n");
}
printf("+");
for (size_t j = 0; j < game->width; ++j)
{
printf("-+");
}
printf("\n");
}
int input_points(Game *game, Field *field)
{
long long int count_points;
if (GetLLInt(&count_points, "Введите кол-во живых точек: ", llint_non_negative) == EOF)
{
return EOF;
}
for (long long int i = 0; i < count_points; ++i)
{
long long int x, y;
printf("Введите координаты живойх точки: \n");
if (GetLLInt(&x, "\tx: ", llint_non_negative) == EOF)
{
return EOF;
}
if (GetLLInt(&y, "\ty: ", llint_non_negative) == EOF)
{
return EOF;
}
Point point = {(size_t)x, (size_t)y};
if (create_new_point(game, field, &point) == COORDINATE_ERROR)
{
printf("[WARNING] Ошибка ввода координат точки!\n");
printf("Попробуйте еще раз!\n");
--i;
}
}
return SUCCESS;
}
void print_menu()
{
printf("Комманды которые вы можете использовать:\n");
printf("\t>) Перейти к следующему поколению\n");
printf("\t<) Перейти к предыдущему поколению\n");
printf("\tq) Закончить игру\n");
}
int command_menu()
{
int result;
do {
print_menu();
printf("Введите команду: ");
char command, symbol = getchar();
command = symbol;
while (symbol != '\n' && symbol != EOF)
{
symbol = getchar();
}
if (command == EOF || symbol == EOF)
{
return EOF;
}
switch (command)
{
case '>':
result = TO_NEXT_GEN;
break;
case '<':
result = TO_PREV_GEN;
break;
case 'q':
result = EXIT;
break;
default:
result = INVALID_COMMAND;
printf("[WARNING] Такой команды не существует!\n");
printf("Попробуйте еще раз!\n");
break;
}
} while (result == INVALID_COMMAND);
return result;
}
int start_game(size_t width, size_t height, size_t capacity)
{
Game game = {width, height, capacity, NULL, NULL};
if (create_history(&game) == MEMORY_ALLOCATION_ERROR)
{
return MEMORY_ALLOCATION_ERROR;
}
if (to_next_gen(&game) == MEMORY_ALLOCATION_ERROR)
{
free_CyclicStack(game.history_field);
return MEMORY_ALLOCATION_ERROR;
}
if (input_points(&game, game.active_field) == EOF)
{
free_game(&game);
return EOF;
}
int command;
if (getchar() == EOF)
{
return EOF;
}
do
{
system("clear");
render(&game);
command = command_menu();
if (command == EOF)
{
free_game(&game);
return EOF;
}
if (command != EXIT)
{
int result = start_event(&game, command);
if (result != SUCCESS)
{
return result;
}
}
} while (command != EXIT);
free_game(&game);
return SUCCESS;
}