-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.c
286 lines (273 loc) · 8.43 KB
/
Game.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
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "Game.h"
#include "Player.h"
#include "GameTree.h"
// Initiales a Game and returns a pointer to it
// This returns NULL if it fails.
Game* init_game(int gridx, int gridy, int** grid, int p1[2], int p2[2]) {
// This first section makes sure the arguments are legal
if (p1[0] >= gridx || p1[0] < 0) {
printf("p1[0] is %d and is not in grid!\n", p1[0]);
return NULL;
}
if (p1[1] >= gridy || p1[1] < 0) {
printf("p1[1] is %d and is not in grid!\n", p1[1]);
return NULL;
}
if (p2[0] >= gridx || p2[0] < 0) {
printf("p2[0] is %d and is not in grid!\n", p2[0]);
return NULL;
}
if (p2[1] >= gridx || p2[1] < 0) {
printf("p2[1] is %d and is not in grid!\n", p2[1]);
return NULL;
}
Game* game = malloc(sizeof(Game));
if(!game) {
printf("Out of memory or something like that.\n");
return NULL;
}
game->gridx = gridx;
game->gridy = gridy;
game->is_over = 0;
game->p1lost = 0;
game->p2lost = 0;
game->grid = malloc(gridx * sizeof(int*));
if(!(game->grid)) {
free(game);
printf("Out of memory.\n");
return NULL;
}
for (int i = 0; i<gridx; i++) {
game->grid[i] = malloc(gridy * sizeof(int));
if(!(game->grid[i])) {
for (int j = 0; j<i; j++) {
free(game->grid[j]);
}
free(game->grid);
free(game);
printf("Out of memory.\n");
return NULL;
}
}
if(grid) {
for (int i = 0; i<gridx; i++) {
for (int j = 0; j<gridy; j++) {
game->grid[i][j] = grid[i][j];
}
}
} else {
for (int i = 0; i<gridx; i++) {
for (int j = 0; j<gridy; j++) {
game->grid[i][j] = 0;
}
}
}
game->pos = malloc(2 * sizeof(int*));
if(!game->pos) {
free(game);
printf("Out of memory.\n");
return NULL;
}
for (int i = 0; i<2; i++) {
game->pos[i] = malloc(2 * sizeof(int));
if(!(game->pos[i])) {
for (int j = 0; j<i; j++) {
free(game->pos[j]);
}
for (int j = 0; j<gridx; j++) {
free(game->grid[j]);
}
free(game->grid);
free(game->pos);
free(game);
printf("Out of memory.\n");
return NULL;
}
}
for (int i = 0; i<2; i++) {
game->pos[0][i] = p1[i];
game->pos[1][i] = p2[i];
}
return game;
}
// Frees allocated memory when creating a Game
void destroy_game(Game* game) {
if (!game) return;
if (game->p1) destroy_player(game->p1);
if (game->p2) destroy_player(game->p2);
for (int i = 0; i<game->gridx; i++) {
free(game->grid[i]);
}
free(game->grid);
for (int i = 0; i<2; i++) {
free(game->pos[i]);
}
free(game->pos);
free(game);
}
// This plays game until the end
// This needs to be redone because i will change Player API to consider the time
// limit
int play_game(Game* game, int (*compute_p1)(Player*, int, int),
int (*compute_p2)(Player*, int, int), double** times){
Player* p1 = game->p1;
Player* p2 = game->p2;
int last1, last2, temp, num_moves = 0;
struct timespec start, current;
last1 = last2 = -1;
Tree* tree = game->p1->tree;
while (!(game->is_over)) {
clock_gettime(CLOCK_REALTIME, &start);
temp = compute_p1(p1, last1, last2);
clock_gettime(CLOCK_REALTIME, ¤t);
times[0][num_moves] = elapsed_time(&start, ¤t) /1000000000.0;
clock_gettime(CLOCK_REALTIME, &start);
last2 = compute_p2(p2, last2, last1);
clock_gettime(CLOCK_REALTIME, ¤t);
times[1][num_moves] = elapsed_time(&start, ¤t) /1000000000.0;
last1 = temp;
make_moves(game, last1, last2);
//print_grid(game->grid, game->gridx, game->gridy, game->pos);
num_moves++;
}
return num_moves;
}
// Prints the grid.
// This does not check if the game is over, it might not work as intended if it is
// Bottom left corner is x = y = 0, x is horizontal, y is vertical
// This is solely for debugging, I have no intention for the game to be playable
void print_grid(int** grid, int gridx, int gridy, int** pos) {
for (int j = gridy-1; j>=0; j--) {
for (int i = 0; i<gridx; i++) {
if (pos[0][0]==i && pos[0][1]==j) {
printf("p1 ");
if (pos[1][0]==i && pos[1][1]==j) {
printf("p2 ");
} else {
printf(" ");
}
continue;
}
if (pos[1][0]==i && pos[1][1]==j) {
printf("p2 ");
continue;
}
printf("%d ", grid[i][j]);
}
printf("\n");
}
printf("\n");
}
// Creates players for game.
// d1 and d2 are the initial directions
void add_players(Game* game, int d1, int d2, double (*movep1)(), double (*movep2)()) {
game->p1 = init_player(game->grid, game->gridx, game->gridy, game->pos, 0,
d1, d2, movep1);
if(!game->p1) {
printf("Could not create p1.\n");
return;
}
game->p2 = init_player(game->grid, game->gridx, game->gridy, game->pos, 1,
d1, d2, movep2);
if(!game->p2) {
printf("Could not create p2.\n");
return;
}
}
// Makes move m1 for player 1 and move m2 for player 2
// Possible moves are 0 (go left), 1 (go up), 2 (go right) and 3 (go down)
// Checks if game is over
void make_moves(Game* game, int m1, int m2) {
if (game->is_over) {
printf("Game is over, no need to make moves!\n");
}
if(m1>3 || m1<0 || make_move(game->grid,
game->gridx, game->gridy, game->pos, 0, m1)) {
game->is_over = 1;
game->p1lost = 1;
}
if(m2>3 || m2<0 || make_move(game->grid,
game->gridx, game->gridy, game->pos, 1, m2)) {
game->is_over = 1;
game->p2lost = 1;
}
if (!game->p1lost && game->grid[game->pos[0][0]][game->pos[0][1]] == 1) {
game->is_over = 1;
game->p1lost = 1;
}
if (!game->p2lost && game->grid[game->pos[1][0]][game->pos[1][1]] == 1) {
game->is_over = 1;
game->p2lost = 1;
}
if ((game->pos[0][0] == game->pos[1][0])
&& (game->pos[0][1] == game->pos[1][1])) {
game->is_over = 1;
game->p1lost = 1;
game->p2lost = 1;
}
}
// Makes move for player on grid
// Change the value of grid, then we move the player.
// Checks for out of bounds moves
int make_move(int** grid, int gridx, int gridy, int** pos, int id, int move) {
grid[pos[id][0]][pos[id][1]] = 1;
if (move == 0) {
pos[id][0]--;
if (pos[id][0]<0) return 1;
} else if (move == 1) {
pos[id][1]++;
if (pos[id][1]>=gridy) return 1;
}
else if (move == 2) {
pos[id][0]++;
if (pos[id][0]>=gridx) return 1;
}
else if (move == 3) {
pos[id][1]--;
if (pos[id][1]<0) return 1;
}
return 0;
}
// Checks if game specified by the parameters is over
int game_over(int** grid, int gridx, int gridy, int** pos) {
if(pos[0][0] < 0
|| pos[0][1] < 0
|| pos[0][0] >= gridx
|| pos[0][1] >= gridy
|| pos[1][0] < 0
|| pos[1][1] < 0
|| pos[1][0] >= gridx
|| pos[1][1] >= gridy
|| (pos[0][0] == pos[1][0] && pos[0][1] == pos[1][1])
|| grid[pos[0][0]][pos[0][1]]
|| grid[pos[1][0]][pos[1][1]]) {
return 1;
}
return 0;
}
// Returns 0 if p1 lost, 1 if p2 lost and 2 of both lost
// This assumes the game is over. This will return a valid value even if it is
// not.
int game_loser(int** grid, int gridx, int gridy, int** pos) {
if (pos[0][0] < 0
|| pos[0][1] < 0
|| pos[0][0] >= gridx
|| pos[0][1] >= gridy
|| grid[pos[0][0]][pos[0][1]]) {
if (pos[1][0] < 0
|| pos[1][1] < 0
|| pos[1][0] >= gridx
|| pos[1][1] >= gridy
|| grid[pos[1][0]][pos[1][1]]) return 2;
return 0;
}
if (pos[1][0] < 0
|| pos[1][1] < 0
|| pos[1][0] >= gridx
|| pos[1][1] >= gridy
|| grid[pos[1][0]][pos[1][1]]) return 1;
return 2;
}