-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
130 lines (112 loc) · 2.83 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <omp.h>
#include "headers/fileHandler.h"
#include "headers/config_t.h"
#include "headers/types_t.h"
#include "headers/rules.h"
#include "headers/coord_t.h"
void printResult (object_t **eco, config_t conf);
void destroyLock (object_t **eco, config_t conf);
int main (int argc, char **argv) {
if (argc < 2) {
printf("Usage:\n%s <config_file>\n\n", argv[0]);
exit(1);
}
config_t conf;
object_t **eco;
coord_t p;
printf("Lendo arquivo");
readFile(argv[1], &conf, &eco);
#ifdef STEP
system("clear");
printf("-------GEN:%2d-------\n", 0);
printMatrix (eco, conf.L, conf.C, conf);
#endif
for (unsigned int t = 1; t <= conf.N_GEN; t++){
conf.GEN++;
#ifdef STEP
getchar();
system("clear");
printf("-------GEN:%2d-------\n", t);
#endif
// REGRAS DO COELHO
for (unsigned int i = 0; i < conf.L; i++){
for (unsigned int j = 0; j < conf.C; j++){
if (eco[i][j].animal.type == RABBIT){
p = (coord_t){.x = i, .y = j};
rabbit_rules (eco, conf, p);
}
}
}
//CONFLITOS DO COELHO
#ifdef PARALELO
#pragma omp parallel for //paralelo
#endif
for (unsigned int i = 0; i < conf.L; i++){
for (unsigned int j = 0; j < conf.C; j++){
eco[i][j].animal = choose_rabbit(eco[i][j]);
}
}
#ifdef STEP2
printMatrix (eco, conf.L, conf.C, conf);
getchar();
system("clear");
printf("-------GEN:%2d-------\n", t);
#endif
//REGRAS DA RAPOSA
for (unsigned int i = 0; i < conf.L; i++){
for (unsigned int j = 0; j < conf.C; j++){
if (eco[i][j].animal.type == FOX){
p = (coord_t){.x = i, .y = j};
fox_rules (eco, conf, p);
}
}
}
//CONFLITOS DA RAPOSA
#ifdef PARALELO
#pragma omp parallel for //paralelo
#endif
for (unsigned int i = 0; i < conf.L; i++){
for (unsigned int j = 0; j < conf.C; j++){
eco[i][j].animal = choose_fox(eco[i][j]);
}
}
printf("\rGeração %d/%d", t, conf.N_GEN);
fflush(stdout);
#ifdef STEP
printMatrix (eco, conf.L, conf.C, conf);
#endif
}
//printResult(eco, conf);
return 0;
}
void printResult (object_t **eco, config_t conf) {
int N = 0;
for (unsigned int i = 0; i < conf.L; i++)
for (unsigned int j = 0; j < conf.C; j++)
if (eco[i][j].animal.type != EMPTY){
N++;
}
printf("%u %u %u 0 %u %u %u\n",conf.GEN_PROC_COELHOS,
conf.GEN_PROC_RAPOSAS,
conf.GEN_COMIDA_RAPOSAS,
conf.L,
conf.C,
N);
for (unsigned int i = 0; i < conf.L; i++)
for (unsigned int j = 0; j < conf.C; j++)
if (eco[i][j].animal.type != EMPTY){
if (eco[i][j].animal.type == FOX) {
printf("RAPOSA %d %d\n", i, j);
}
else if (eco[i][j].animal.type == RABBIT) {
printf("COELHO %d %d\n", i, j);
}
else if (eco[i][j].animal.type == ROCK) {
printf("ROCHA %d %d\n", i, j);
}
}
}