-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap.c
More file actions
328 lines (306 loc) · 10.3 KB
/
map.c
File metadata and controls
328 lines (306 loc) · 10.3 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
#include "fonction.h"
void printMapRed(Cell ** map, int x, int y,int cible){ // Print the map : if a robot and a target are on the same cell, only the robot will be printed
printf("\033[H\033[2J"); // clear the screen
//printf("\x1b[44m"); //change the background color
for(int i = 0; i<y; i++){
for(int j = 0; j<x; j++){
if (map[i][j].robot>0){
if (map[i][j].robot==1){
printf("\U0001F916");
}else if (map[i][j].robot==2){
printf("\U0001F47E");
}else if (map[i][j].robot==3){
printf("\U0001F47D");
}else if (map[i][j].robot==4){
printf("\U0001F47B");
}
}else if (map[i][j].target==cible && cible>=10){
printf(RED_TEXT "%d" RESET_TEXT,map[i][j].target);
}else if (map[i][j].target==cible && cible>0){
printf(RED_TEXT "%d " RESET_TEXT,map[i][j].target);
}else if (map[i][j].target>=10){
printf("%d", map[i][j].target);
}else if (map[i][j].target>0){
printf("%d ", map[i][j].target);
}else if (map[i][j].wall){
printf("\u2588\u2588");
}else{
printf(" ");
}
}
printf("\n");
}
printf("\n");
}
void addBorderWalls(Cell **map, int x,int y){ // add the edge's walls
//printf("Starting edges placement...\n");
for(int i = 0;i<x;i++){
map[0][i].wall=1;
}
sleep(0.1);
//printf("|");
for(int i = 0;i<y;i++){
map[i][x-1].wall=1;
}
//printf("|");
for(int i = 0; i<x;i++){
map[y-1][i].wall=1;
}
//printf("|");
for(int i = 0;i<y;i++){
map[i][0].wall=1;
}
//printf("|\n");
//printf("Edges placement finished !\n");
}
Cell ** generateMap(int *x, int *y){ // Gen the blank map with the edges as walls
int width = rand() % 6 + 15; // def of the playground's width
int length = rand() % 6 + 15; // def of the playground's length
//printf("\033[H\033[2J Map coordinates generation");
*y = width*2+1;
*x = length*2+1;
//printf("Map coordinates set !\n Starting Cells creation...\n");
Cell ** map = malloc((*y)*sizeof(Cell*)); // Creation of the playground
if (map==NULL){ // allocation assertion
printf("Tab malloc failed, exiting the game\n");
exit(1);
}else{
for(int i = 0; i < *y; i++){ // Corrected loop for y
map[i] = malloc((*x) * sizeof(Cell));
if (map[i]==NULL){ // allocation assertion
free(map);
printf("Tab[i] malloc failed, exiting the game\n");
exit(2);
}
for (int j = 0; j < *x; j++) { // initialize all the cells' values at 0
map[i][j].robot = 0;
map[i][j].target = 0;
map[i][j].wall = 0;
}
//printf("|");
}
}
//printf("\n Cell generation done ! Adding border walls...\n");
addBorderWalls(map,*x,*y);
//printf("Border walls added ! Blank map done\n");
return map;
}
int verifyTargetPlacement(Cell ** map, int xT, int yT){ // Verify if a target can be put at this location based on the rules
//printf(" Starting target placement verification...");
if (map[yT-2][xT-2].target>0){
//printf("Bad ! Retrying\n");
return 1;
}
if (map[yT-2][xT].target>0){
//printf("Bad ! Retrying\n");
return 1;
}
if (map[yT-2][xT+2].target>0){
//printf("Bad ! Retrying\n");
return 1;
}
if (map[yT][xT-2].target>0){
//printf("Bad ! Retrying\n");
return 1;
}
if (map[yT][xT+2].target>0){
//printf("Bad ! Retrying\n");
return 1;
}
if (map[yT+2][xT-2].target>0){
//printf("Bad ! Retrying\n");
return 1;
}
if (map[yT+2][xT].target>0){
//printf("Bad ! Retrying\n");
return 1;
}
if (map[yT+2][xT+2].target>0){
//printf("Bad ! Retrying\n");
return 1;
}
if (map[yT][xT].target>0){
//printf("Bad ! Retrying\n");
return 1;
}
if (map[yT+2][xT-1].wall>0){
//printf("Bad ! Retrying\n");
return 1;
}
if (map[yT+2][xT+1].wall>0){
//printf("Bad ! Retrying\n");
return 1;
}
if (map[yT+1][xT-2].wall>0){
//printf("Bad ! Retrying\n");
return 1;
}
if (map[yT-1][xT-2].wall>0){
//printf("Bad ! Retrying\n");
return 1;
}
if (map[yT+1][xT+2].wall>0){
//printf("Bad ! Retrying\n");
return 1;
}
if (map[yT-1][xT+2].wall>0){
//printf("Bad ! Retrying\n");
return 1;
}
if (map[yT-2][xT-1].wall>0){
//printf("Bad ! Retrying\n");
return 1;
}
if (map[yT-2][xT+1].wall>0){
//printf("Bad ! Retrying\n");
return 1;
}
if (map[yT-1][xT].wall>0){
//printf("Bad ! Retrying\n");
return 1;
}
if (map[yT][xT+1].wall>0){
//printf("Bad ! Retrying\n");
return 1;
}
if (map[yT+1][xT].wall>0){
//printf("Bad ! Retrying\n");
return 1;
}
if (map[yT][xT-1].wall>0){
//printf("Bad ! Retrying\n");
return 1;
}
//printf("Checked ! Creating the target\n");
return 0;
}
void ** placeTargets(Cell ** map, int x, int y){ // place all the targets in the map
//printf("Starting target placement...\n");
int xT, yT;
int lMap = (x-1)/2; // calculate the "real" length of the map with all possibles robot-reachable cells (the cells where targets could be placed)
int wMap = (y-1)/2;
for(int i = 0; i<18; i++){
//printf("Placing %dst target\n", i);
do{
xT = (rand()%(lMap-2))+1; // x of the target without the edges of the map
yT = (rand()%(wMap-2))+1; // idem for y
xT = xT*2+1;
yT = yT*2+1;
}while(verifyTargetPlacement(map, xT, yT)); // Verify if the target isn't next to another target
map[yT][xT].target = i+1;
int typeWall = rand()%4; // What walls will be placed next to the target
if (typeWall==0){
map[yT-1][xT].wall = 1;
map[yT][xT+1].wall = 1;
map[yT-1][xT+1].wall = 1;
}
if (typeWall==1){
map[yT][xT-1].wall = 1;
map[yT-1][xT].wall = 1;
map[yT-1][xT-1].wall = 1;
}
if (typeWall==2){
map[yT+1][xT].wall = 1;
map[yT][xT-1].wall = 1;
map[yT+1][xT-1].wall = 1;
}
if (typeWall==3){
map[yT+1][xT].wall = 1;
map[yT][xT+1].wall = 1;
map[yT+1][xT+1].wall = 1;
}
}
//printf("Target placement finished !\n");
}
void placeAdditionalTerrainWalls(Cell ** map, int xMap, int yMap){
int xT, yT;
int lMap = (xMap-1)/2; // calculate the "real" length of the map with all possibles robot-reachable cells (the cells where targets could be placed)
int wMap = (yMap-1)/2;
int stop = 0; // to stop the program if it see that there is not enough place
int counter = 0;
for(int i = 0; i<50 && stop==0;i++){
counter = 0;
do{
xT = (rand()%(lMap-2))+1; // x of the wall without the edges of the map
yT = (rand()%(wMap-2))+1; // idem for y
xT = xT*2+1;
yT = yT*2+1;
if(counter>=15){ // stopping the program if there is not enough place
stop = 1;
}
counter++;
}while(verifyTargetPlacement(map, xT, yT)&&!stop); // Verify if the wall isn't next to another or something else
if (!stop){
int typeWall = rand()%4; // What walls will be placed
if (typeWall==0){
map[yT-1][xT].wall = 1;
map[yT][xT+1].wall = 1;
map[yT-1][xT+1].wall = 1;
}
if (typeWall==1){
map[yT][xT-1].wall = 1;
map[yT-1][xT].wall = 1;
map[yT-1][xT-1].wall = 1;
}
if (typeWall==2){
map[yT+1][xT].wall = 1;
map[yT][xT-1].wall = 1;
map[yT+1][xT-1].wall = 1;
}
if (typeWall==3){
map[yT+1][xT].wall = 1;
map[yT][xT+1].wall = 1;
map[yT+1][xT+1].wall = 1;
}
}
}
}
void placeWallsOnAnEdge(Cell ** map, int x, int y){ // Places 2 walls per edge
//printf("Border walls placement started...\n");
int wall1, wall2;
do{
wall1 = rand()%(x/2-2)*2+2;
wall2 = rand()%(x/2-2)*2+2;
}while(wall1==wall2);
map[1][wall1].wall=1;
map[1][wall2].wall=1;
//printf("|");
do{
wall1 = rand()%(y/2-2)*2+2;
wall2 = rand()%(y/2-2)*2+2;
}while(wall1==wall2);
map[wall1][1].wall=1;
map[wall2][1].wall=1;
//printf("|");
do{
wall1 = rand()%(y/2-2)*2+2;
wall2 = rand()%(y/2-2)*2+2;
}while(wall1==wall2);
map[wall1][x-2].wall=1;
map[wall2][x-2].wall=1;
//printf("|");
do{
wall1 = rand()%(x/2-2)*2+2;
wall2 = rand()%(x/2-2)*2+2;
}while(wall1==wall2);
map[y-2][wall1].wall=1;
map[y-2][wall2].wall=1;
//printf("|\nBorder walls placement finished !\n");
}
void placeRobots(Cell ** map, int x, int y){ // place robots
//printf("\nRobot placement started...\n");
int xR, yR;
int lMap = (x-1)/2; // These two lines calculate the number of possibles cells without the walls' cells (LengthMap and WidthMap)
int wMap = (y-1)/2;
for(int i = 0; i<4; i++){
do{
xR = (rand()%(lMap-2))+1; // x of the robot without the edges of the map
yR = (rand()%(wMap-2))+1; // idem for y
xR = xR*2+1;
yR = yR*2+1;
}while((map[yR][xR].target>0)||(map[yR][xR].robot>0)); // Verify the robot placement
map[yR][xR].robot = i+1;
//printf("|");
}
//printf("\nRobot placement finished !\n");
}