-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFormiga.java
452 lines (380 loc) · 14.9 KB
/
Formiga.java
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
import java.util.*;
import java.lang.*;
public class Formiga{
/* Constantes de informação da formiga */
public static final int MAX_VIDA = 5;
/* Possiveis decisoes: caracteristicas ("aminoacidos") do DNA */
public static final int SEGUE_AGUA = 0;
public static final int SEGUE_PAREDE = 1;
public static final int SEGUE_FORMIGAS = 2;
public static final int SEGUE_VAZIO = 3;
public static final int FICA_PARADO = 4;
public static final int PROCURA_ALEATORIAMENTE = 5;
/* Possiveis decisoes logicas */
public static final int PARADO = 0;
public static final int NORTE = 1;
public static final int LESTE = 2;
public static final int SUL = 3;
public static final int OESTE = 4;
/* Variáveis que definem a classe formiga */
private int x;
private int y;
private int vida;
private int visao_norte;
private int visao_leste;
private int visao_sul;
private int visao_oeste;
private int decisao;
//Construtor
public Formiga(int x, int y){
this.x = x;
this.y = y;
this.vida = MAX_VIDA;
}
public int getX() {return this.x;}
public int getY() {return this.y;}
public int getVida() {return this.vida;}
public int getNorte() {return this.visao_norte;}
public int getLeste() {return this.visao_leste;}
public int getSul() {return this.visao_sul;}
public int getOeste() {return this.visao_oeste;}
public void setVida(int vida) {this.vida = vida;}
/* O metodo setVisao analisa a matriz de pixeis para determinar o que cada formiga ve
em seus arredores */
public void setVisao(int[][] matriz) {
int norte = -1, sul = -1, leste = -1, oeste = -1;
int x = this.getX();
int y = this.getY();
switch(matriz[x][y-1]) // Norte
{
case Formigueiro.PAREDE:
norte = Formigueiro.PAREDE;
break;
case Formigueiro.AGUA:
norte = Formigueiro.AGUA;
break;
case Formigueiro.FORMIGA:
norte = Formigueiro.FORMIGA;
break;
case Formigueiro.VAZIO:
norte = Formigueiro.VAZIO;
break;
}
switch(matriz[x+1][y]) // Leste
{
case Formigueiro.PAREDE:
leste = Formigueiro.PAREDE;
break;
case Formigueiro.AGUA:
leste = Formigueiro.AGUA;
break;
case Formigueiro.FORMIGA:
leste = Formigueiro.FORMIGA;
break;
case Formigueiro.VAZIO:
leste = Formigueiro.VAZIO;
break;
}
switch(matriz[x][y+1]) // Sul
{
case Formigueiro.PAREDE:
sul = Formigueiro.PAREDE;
break;
case Formigueiro.AGUA:
sul = Formigueiro.AGUA;
break;
case Formigueiro.FORMIGA:
sul = Formigueiro.FORMIGA;
break;
case Formigueiro.VAZIO:
sul = Formigueiro.VAZIO;
break;
}
switch(matriz[x-1][y]) // Oeste
{
case Formigueiro.PAREDE:
oeste = Formigueiro.PAREDE;
break;
case Formigueiro.AGUA:
oeste = Formigueiro.AGUA;
break;
case Formigueiro.FORMIGA:
oeste = Formigueiro.FORMIGA;
break;
case Formigueiro.VAZIO:
oeste = Formigueiro.VAZIO;
break;
}
this.visao_norte = norte;
this.visao_oeste = oeste;
this.visao_sul = sul;
this.visao_leste = leste;
//System.out.println(" > Campo de visao");
//System.out.println(" -> Visao Norte: " + this.visao_norte);
//System.out.println(" -> Visao Oeste: " + this.visao_oeste);
//System.out.println(" -> Visao Sul: " + this.visao_sul);
//System.out.println(" -> Visao Leste: " + this.visao_leste);
}
/* A partir do DNA do formigueiro e da visao dela, uma decisao e tomada. Os estados dependem
do que a formiga ve. Se ela ve agua, formiga e vazio, um aminoacido do DNA sera usado para tomar
a decisao. Se ela ver apenas vazio, outra decisao. E assim por diante */
public void getDecisao(int[] DNA) {
int estado;
int agua, formigas, vazio, parede;
int total = 0;
int potenc = 1;
if( (visao_norte == Formigueiro.AGUA) || ( visao_oeste == Formigueiro.AGUA) || (visao_sul == Formigueiro.AGUA) || (visao_leste == Formigueiro.AGUA) )
agua = 1;
else agua = 0;
if( (visao_norte == Formigueiro.FORMIGA) || ( visao_oeste == Formigueiro.FORMIGA) || (visao_sul == Formigueiro.FORMIGA) || (visao_leste == Formigueiro.FORMIGA) )
formigas = 1;
else formigas = 0;
if( (visao_norte == Formigueiro.VAZIO) || ( visao_oeste == Formigueiro.VAZIO) || (visao_sul == Formigueiro.VAZIO) || (visao_leste == Formigueiro.VAZIO) )
vazio = 1;
else vazio = 0;
if( (visao_norte == Formigueiro.PAREDE) || ( visao_oeste == Formigueiro.PAREDE) || (visao_sul == Formigueiro.PAREDE) || (visao_leste == Formigueiro.PAREDE) )
parede = 1;
else parede = 0;
estado = agua*1000 + formigas*100 + vazio*10 + parede*1;
//System.out.println(" > Estado: " + estado);
while(estado > 0)
{
total += estado % 10 * potenc;
estado = estado / 10;
potenc = potenc * 2;
}
this.decisao = DNA[total];
//System.out.println(" > Estado(num): " + total);
//System.out.println(" > Decisão: " + this.decisao);
}
/* O metodo setMov pega a decisao da formiga e atualiza a matriz que define o formigueiro */
public void setMov(int[][] matriz) {
//System.out.println(" > Vida antes do move: " + this.vida);
int aux;
int move = PARADO;
int count_aux = 0;
boolean flag[] = {false,false,false,false};
Random gerador = new Random();
switch(decisao)
{
case SEGUE_AGUA:
if( visao_norte == Formigueiro.AGUA ) {
count_aux++;
flag[0] = true;
}
if( visao_leste == Formigueiro.AGUA ) {
count_aux++;
flag[1] = true;
}
if( visao_oeste == Formigueiro.AGUA ) {
count_aux++;
flag[2] = true;
}
if( visao_sul == Formigueiro.AGUA ) {
count_aux++;
flag[3] = true;
}
if(count_aux == 0) move = gerador.nextInt(5);
else if(count_aux == 1) {
if( flag[0] == true ) move = NORTE;
else if( flag[1] == true ) move = LESTE;
else if( flag[2] == true ) move = SUL;
else if( flag[3] == true ) move = OESTE;
}
else {
int i_aux;
do{
i_aux = gerador.nextInt(4);
} while(flag[i_aux] == false);
if( i_aux == 0 ) move = NORTE;
else if( i_aux == 1 ) move = LESTE;
else if( i_aux == 2 ) move = SUL;
else if( i_aux == 3 ) move = OESTE;
}
break;
case SEGUE_PAREDE:
if( visao_norte == Formigueiro.PAREDE ) {
count_aux++;
flag[0] = true;
}
if( visao_leste == Formigueiro.PAREDE ) {
count_aux++;
flag[1] = true;
}
if( visao_oeste == Formigueiro.PAREDE ) {
count_aux++;
flag[2] = true;
}
if( visao_sul == Formigueiro.PAREDE ) {
count_aux++;
flag[3] = true;
}
if(count_aux == 0) move = gerador.nextInt(5);
else if(count_aux == 1) {
if( flag[0] == true ) move = NORTE;
else if( flag[1] == true ) move = LESTE;
else if( flag[2] == true ) move = SUL;
else if( flag[3] == true ) move = OESTE;
}
else {
int i_aux;
do{
i_aux = gerador.nextInt(4);
} while(flag[i_aux] == false);
if( i_aux == 0 ) move = NORTE;
else if( i_aux == 1 ) move = LESTE;
else if( i_aux == 2 ) move = SUL;
else if( i_aux == 3 ) move = OESTE;
}
break;
case SEGUE_FORMIGAS:
if( visao_norte == Formigueiro.FORMIGA ) {
count_aux++;
flag[0] = true;
}
if( visao_leste == Formigueiro.FORMIGA ) {
count_aux++;
flag[1] = true;
}
if( visao_oeste == Formigueiro.FORMIGA ) {
count_aux++;
flag[2] = true;
}
if( visao_sul == Formigueiro.FORMIGA ) {
count_aux++;
flag[3] = true;
}
if(count_aux == 0) move = gerador.nextInt(5);
else if(count_aux == 1) {
if( flag[0] == true ) move = NORTE;
else if( flag[1] == true ) move = LESTE;
else if( flag[2] == true ) move = SUL;
else if( flag[3] == true ) move = OESTE;
}
else {
int i_aux;
do{
i_aux = gerador.nextInt(4);
} while(flag[i_aux] == false);
if( i_aux == 0 ) move = NORTE;
else if( i_aux == 1 ) move = LESTE;
else if( i_aux == 2 ) move = SUL;
else if( i_aux == 3 ) move = OESTE;
}
break;
case SEGUE_VAZIO:
if( visao_norte == Formigueiro.VAZIO ) {
count_aux++;
flag[0] = true;
}
if( visao_leste == Formigueiro.VAZIO ) {
count_aux++;
flag[1] = true;
}
if( visao_oeste == Formigueiro.VAZIO ) {
count_aux++;
flag[2] = true;
}
if( visao_sul == Formigueiro.VAZIO ) {
count_aux++;
flag[3] = true;
}
if(count_aux == 0) move = gerador.nextInt(5);
else if(count_aux == 1) {
if( flag[0] == true ) move = NORTE;
else if( flag[1] == true ) move = LESTE;
else if( flag[2] == true ) move = SUL;
else if( flag[3] == true ) move = OESTE;
}
else {
int i_aux;
do{
i_aux = gerador.nextInt(4);
} while(flag[i_aux] == false);
if( i_aux == 0 ) move = NORTE;
else if( i_aux == 1 ) move = LESTE;
else if( i_aux == 2 ) move = SUL;
else if( i_aux == 3 ) move = OESTE;
}
break;
case FICA_PARADO:
move = PARADO;
break;
case PROCURA_ALEATORIAMENTE:
move = gerador.nextInt(5);
break;
}
int x = this.x;
int y = this.y;
int vida = this.vida;
int m, n;
switch(move)
{
case NORTE:
if( (matriz[x][y-1] == Formigueiro.PAREDE) || (matriz[x][y-1] == Formigueiro.FORMIGA) ) move = PARADO;
break;
case LESTE:
if( (matriz[x+1][y] == Formigueiro.PAREDE) || (matriz[x+1][y] == Formigueiro.FORMIGA) ) move = PARADO;
break;
case SUL:
if( (matriz[x][y+1] == Formigueiro.PAREDE) || (matriz[x][y+1] == Formigueiro.FORMIGA) ) move = PARADO;
break;
case OESTE:
if( (matriz[x-1][y] == Formigueiro.PAREDE) || (matriz[x-1][y] == Formigueiro.FORMIGA) ) move = PARADO;
break;
}
switch(move)
{
case PARADO:
break;
case NORTE:
if( (this.x > 0.5 * Formigueiro.L_CENARIO) && (this.x < 0.7 * Formigueiro.L_CENARIO) )
matriz[x][y] = Formigueiro.AGUA;
else
matriz[x][y] = Formigueiro.VAZIO;
this.x = x;
this.y = y-1;
matriz[x][y-1] = Formigueiro.FORMIGA;
break;
case LESTE:
if( (this.x > 0.5 * Formigueiro.L_CENARIO) && (this.x < 0.7 * Formigueiro.L_CENARIO) )
matriz[x][y] = Formigueiro.AGUA;
else
matriz[x][y] = Formigueiro.VAZIO;
this.x = x+1;
this.y = y;
matriz[x+1][y] = Formigueiro.FORMIGA;
break;
case SUL:
if( (this.x > 0.5 * Formigueiro.L_CENARIO) && (this.x < 0.7 * Formigueiro.L_CENARIO) )
matriz[x][y] = Formigueiro.AGUA;
else
matriz[x][y] = Formigueiro.VAZIO;
this.x = x;
this.y = y + 1;
matriz[x][y+1] = Formigueiro.FORMIGA;
break;
case OESTE:
if( (this.x > 0.5 * Formigueiro.L_CENARIO) && (this.x < 0.7 * Formigueiro.L_CENARIO) )
matriz[x][y] = Formigueiro.AGUA;
else
matriz[x][y] = Formigueiro.VAZIO;
this.x = x - 1;
this.y = y;
matriz[x-1][y] = Formigueiro.FORMIGA;
break;
}
/* A vida da formiga e atualizada. Caso ela esteja rodeada de agua (4 lados), ela perde um ponto
de vida */
int count_neighbor = 0;
if( (this.x > 0.5 * Formigueiro.L_CENARIO) && (this.x < 0.7 * Formigueiro.L_CENARIO) ) {
if(this.visao_norte == Formigueiro.AGUA) count_neighbor++;
if(this.visao_leste == Formigueiro.AGUA) count_neighbor++;
if(this.visao_sul == Formigueiro.AGUA) count_neighbor++;
if(this.visao_oeste == Formigueiro.AGUA) count_neighbor++;
if(count_neighbor > 3) this.vida = this.vida - 1;
}
//System.out.println(" > Nova posicao: (" + this.x + " " + this.y + ")");
//System.out.println(" > Nova vida: " + this.vida);
}
}