/
marcoyolo.c
110 lines (87 loc) · 2.32 KB
/
marcoyolo.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
#include "marcoyolo.h"
int main (int argc, char** argv) {
struct s_map *map;
if (argc>1) {
printf("Too much parameters.\nUsage : %s\n", argv[0]);
return 1;
}
map=malloc((MAP_SIZE_X*MAP_SIZE_Y*2+1)*sizeof(int));
if (!map) {
printf("Error : Cannot allocate memory\n");
return 2;
}
init(map);
display(map);
browse(map);
return 0;
}
int init(struct s_map *map) {
int x,y;
srand ( time(NULL) );
for (x=0;x<MAP_SIZE_X;x++) {
for (y=0;y<MAP_SIZE_Y;y++) {
map->ground[x][y]=rand()%2;
map->color[x][y]=COLOR_NULL;
}
}
map->next_color=30;
return 0;
}
int display(struct s_map *map) {
int x,y;
char color[16];
char ground;
struct timespec pause;
pause.tv_sec = 0;
pause.tv_nsec = T_PAUSE;
printf("\e[1;1H\e[2J");
for (y=0;y<MAP_SIZE_Y;y++) {
for (x=0;x<MAP_SIZE_X;x++) {
if ( map->ground[x][y] == 0 )
ground='~';
else
ground='o';
sprintf(color,"\x1B[%im",map->color[x][y]);
printf("%s%c",color,ground);
}
printf("\n");
}
if (nanosleep(&pause,NULL) < 0) {
printf("Error : syscall nanosleep failed.\n");
exit(1);
}
return 0;
}
int browse(struct s_map *map) {
int x,y;
for (y=0;y<MAP_SIZE_Y;y++) {
for (x=0;x<MAP_SIZE_X;x++) {
if ( (map->color[x][y]==COLOR_NULL) && (map->ground[x][y] != 0) )
spread(map,x,y,map->ground[x][y],nextcolor(map));
}
display(map);
}
return 0;
}
int nextcolor(struct s_map *map) {
map->next_color=(map->next_color-29)%7+30;
return map->next_color;
}
int spread(struct s_map *map,int x, int y, int ground, int color) {
if ( (x<0) || (x>=MAP_SIZE_X) || (y<0) || (y>=MAP_SIZE_Y) )
return 0;
if (map->color[x][y]!=COLOR_NULL)
return 0;
if ( map->ground[x][y] != ground )
return 0;
map->color[x][y]=color;
spread(map,x-1,y,ground,color);
spread(map,x+1,y,ground,color);
spread(map,x,y-1,ground,color);
spread(map,x,y+1,ground,color);
spread(map,x-1,y-1,ground,color);
spread(map,x-1,y+1,ground,color);
spread(map,x+1,y-1,ground,color);
spread(map,x+1,y+1,ground,color);
return 0;
}