-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflood_lib.c
272 lines (256 loc) · 5.81 KB
/
flood_lib.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
#include "const.h"
#include "flood_type.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/sysinfo.h>
#include <unistd.h>
int get_covert(int owned[MAX_SIZE_X][MAX_SIZE_Y]) {
int cpt=0;
int i,j;
for (i=0;i<MAX_SIZE_X;i++)
{
for (j=0;j<MAX_SIZE_Y;j++)
{
cpt+=owned[i][j]==1;
}
}
return cpt;
}
int init_parameters(struct parameters *p) {
p->min_col = 0;
p->max_col = 9;
p->begin_x = 0;
p->begin_y = 0;
p->max_paths_check = 100;
p->display_color_number = 0;
p->display_stars = 0;
p->display_stats = 0;
p->surface = 0;
p->max_cpu = get_nprocs();
//printf("count of cpu = %d\n", p->max_cpu);
}
int parse_parameters(int argc, char *argv[], struct parameters *p, int path[MAX_PATHS]) { // use ->truc instead of &val.truc
init_parameters(p);
int i;
int ok = 1;
i = 0;
i = 1;
while ((i<argc) && (ok = 1)) {
ok=0;
if (!strcmp(&argv[i][0], "-bx")) {
i++;
p->begin_x = (int) strtol(&argv[i][0], NULL, 10);
ok=1;
}
if (!strcmp(&argv[i][0], "-by")) {
i++;
p->begin_y = (int) strtol(&argv[i][0], NULL, 10);
ok=1;
}
if (!strcmp(&argv[i][0], "-mp")) {
i++;
p->max_paths_check = (int) strtol(&argv[i][0], NULL, 10);
ok=1;
}
if (!strcmp(&argv[i][0], "-dn")) {
p->display_color_number = 1;
ok=1;
}
if (!strcmp(&argv[i][0], "-ds")) {
p->display_stars = 1;
ok=1;
}
if (!strcmp(&argv[i][0], "-dst")) {
p->display_stats = 1;
ok=1;
}
if (!strcmp(&argv[i][0], "-path")) {
i++;
strcpy(p->path, &argv[i][0]);
ok=1;
}
if (!strcmp(&argv[i][0], "-cpu")) {
i++;
p->max_cpu = (int) strtol(&argv[i][0], NULL, 10);
ok=1;
}
if (ok == 0) {
printf("Unknown parameter %s\n", &argv[i][0]);
}
i++;
};
return ok;
};
int test_parameters(struct parameters *p) {
int ok = 1;
if (p->begin_x > p->size_x) { fprintf(stderr, "Error: horizontal starting position is greater than horizontal size\n"); ok=0; };
if (p->begin_y > p->size_y) { fprintf(stderr, "Error: vertical starting position is greater than vertical size\n"); ok=0; };
if (p->size_x > MAX_SIZE_X) { fprintf(stderr, "Error: horizontal size greater than %d\n", MAX_SIZE_X); ok=0; };
if (p->size_y > MAX_SIZE_Y) { fprintf(stderr, "Error: vertical size greater than %d\n", MAX_SIZE_Y); ok=0; };
if (p->max_paths_check > MAX_PATHS) { fprintf(stderr, "Error: maximum number of paths checks is size greater than %d\n", MAX_PATHS); ok=0; };
//TODO : some programs can't use some parameters, check that based on the program run
return ok;
}
int color_print(int x, int y, int col, struct parameters *p) {
//printf("painting %d %d in %d\n", x, y, col);
//return 0;
int bg,fg;
bg=col;
if ((bg==2)||(bg==3)||(bg==6)||(bg==7))
{
fg=0;
}
else
{
fg=7;
}
printf("\033[%dm\033[%dm", bg+40, fg+30); // we change colors
printf("\033[%d;%dH", y+1, x*2+1); // we move the cursor
if (p->display_stars) { printf("**"); } else { printf(" "); }
return 0;
}
int update_owned(int board[MAX_SIZE_X][MAX_SIZE_Y], int owned[MAX_SIZE_X][MAX_SIZE_Y], int color, struct parameters *p) {
// doesn't update the color of the board
int i, j, updated;
updated=1;
while (updated) {
updated=0;
for(i=0;i<p->size_x;i++) {
for(j=0;j<p->size_y;j++) {
if (owned[i][j]) {
if (i>0) {
if (!(owned[i-1][j])) {
if (board[i-1][j]==color) { owned[i-1][j]=1; updated=1; }
}
}
if (j>0) {
if (!(owned[i][j-1])) {
if (board[i][j-1]==color) { owned[i][j-1]=1; updated=1; }
}
}
if (i<p->size_x-1) {
if (!(owned[i+1][j])) {
if (board[i+1][j]==color) { owned[i+1][j]=1; }
}
}
if (j<p->size_y-1) {
if (!(owned[i][j+1])) {
if (board[i][j+1]==color) { owned[i][j+1]=1; }
}
}
}
}
}
}
}
int init_board_from_stdin(int board[MAX_SIZE_X][MAX_SIZE_Y], struct parameters *p) {
FILE *in = stdin;
int bufsize;
char *buffer;
int i=0,j=0, col;
buffer=malloc(1);
p->size_x = 0; p->size_y = 0;
p->min_col = 9; p->max_col = 0;
while (1) {
bufsize = fread(buffer, 1, 1, in);
if (bufsize==0)
{
break;
}
if (buffer[0]=='\n')
{
if (i > MAX_SIZE_Y) {
fprintf(stdout, "board too high\n");
return 1;
}
i+=1;
if (p->size_x==0)
{
p->size_x=j;
}
j=0;
p->size_y+=1;
}
else
{
if (j > MAX_SIZE_X) {
fprintf(stdout, "Board too wide\n");
return 1;
}
col=(int) strtol(buffer, NULL, 10);
if (col > 7) {
fprintf(stdout, "Too much colors\n");
return 1;
}
if (col < p->min_col) { p->min_col=col; }
if (col > p->max_col) { p->max_col=col; }
board[j][i]=col;
j+=1;
}
}
p->surface = p->size_x * p->size_y;
return 1;
}
int init_owned(int owned[MAX_SIZE_X][MAX_SIZE_Y], struct parameters *p) {
int i, j;
for (i=0; i < p->size_x; i++) {
for (j=0; i < p->size_y; i++) {
owned[i][j] = 0;
}
}
owned[p->begin_x][p->begin_y]=1;
}
int print_board(int board[MAX_SIZE_X][MAX_SIZE_Y], int owned[MAX_SIZE_X][MAX_SIZE_Y], struct parameters *p) {
int i, j;
int bg, fg;
FILE *out=stdout;
for (i=0;i<p->size_y;i++)
{
for (j=0;j<p->size_x;j++)
{
bg=board[j][i];
if ((bg==2)||(bg==3)||(bg==6)||(bg==7))
{
fg=0;
}
else
{
fg=7;
}
fprintf(out,"\033[%dm\033[%dm", fg+30, bg+40);
if (owned[j][i]==1) {
if (p->display_stars) {
fprintf(out,"**");
}
else
{
fprintf(out," ");
}
}
else
{
if (p->display_color_number) {
fprintf(out," %d", bg); // has to be an option
}
else
{
fprintf(out, " ");
}
}
fprintf(out,"\033[0m");
}
fprintf(out,"\n");
}
return 0;
}
int print_path(int path[MAX_PATH], int path_length) // only for debug
{
int i;
for (i=0;i<path_length;i++)
{
if (path[i]!=-1) { printf("%d", path[i]); }
}
//printf("\n");
return 0;
}