-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIAruim.c
1233 lines (1036 loc) · 27.6 KB
/
IAruim.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
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#include<math.h>
//variavel global usada. O ID e um valor passado pelo programa principal que vai identificar se o jogador e o player 1 ou player 2
int id;
//ID do jogo
int ident;
int modificacao;
int move[5];
char range_symbol[3];
char e_range_symbol[3];
int plantedBomb = 0; //If it planted a bomb it looks into running a away from it.
//esta estrutura guarda as posicoes em i e j num tabuleiro
typedef struct pos
{
int i;
int j;
}pos;
typedef struct info_bomba
{
int i;
int j;
int range;
}info_bomba;
pos posIam;
pos oldPos; //a Posicao anterior do usuario
int jogarbomba; //Verifica se vai jogar bomba nesta posicao ou nao;
int willexplode; //Indica se ira jogar bomba nesta posicao ou nao;
//no maximmo pode ter 5 bombas(TMB)
info_bomba bombas[5];// guarda as posicoes da bomba;
int qtd_bombas = 0;
int qtd_bombas_max = 2;
int range = 2; //Alcance das bombas;
info_bomba bombas_enemy[5];
int qtd_bombas_enemy = 0;
int qtd_bombas_max_enemy = 2;
int range_enemy = 2;
// nome das nossas bombas
char bomb[3];
// nome das bombas do inimigo
char enemyBomb[3];
pos matinhos[100];
int qtdMatinhos = 0;
int rodada = 1;
pos bonus_range[100];
int qtdBonusRange = 0;
pos bonus_bombas[100];
int qtdBonusBomba = 0;
//vetores de deslocamento. {parado, sobe, esquerda, desce, direita}
int dx[] = {0,-1,0,1,0};
int dy[] = {0,0,-1,0,1};
FILE *fp = NULL; //objeto que possui metodos para escrevermos nos objetos;
//estrutura do tabuleiro. Como e sabido, os dois primeiros caracteres guardam as informacoes sobre o jogadore e os dois ultimos
//sobre a presenca de bombas, bonus, etc
//logo, usa-se duas strings
typedef struct tabela
{
char str1[3],str2[3];
}tabela;
//mapa a ser lido.
tabela tab[11][13];
//mapa secundario
tabela tab2[11][13];
// tabela com os pesos das posições do mapa
int tabPeso[11][13];
// Nome do nosso player
char s[3];
// Nome do Player inimigo
char enemyS[3];
//funcao que realiza a leitura do mapa. O mapa e passado como entrada padrao pelo programa principal, portanto, nesses casos, usem scanf normalmente.
//usem fscanf para trabalhar com arquivos criados por voces.
void leitura()
{
int i, j;
for(i = 0; i<11; i++)
{
for(j = 0; j<13; j++)
{
char temp[5];
scanf("%s", temp);
tab[i][j].str1[0] = temp[0];
tab[i][j].str1[1] = temp[1];
tab[i][j].str2[0] = temp[2];
tab[i][j].str2[1] = temp[3];
}
}
}
// verifica se o mapa secundario eh igual ao mapa principal
void verificarMapa2()
{
int i, j;
for(i = 0; i<11; i++)
{
for(j = 0; j<13; j++)
{
//verifica se a primeira string eh igual
if(strcmp(tab2[i][j].str1, tab[i][j].str1) != 0)
{
//se for diferente verifica se a segunda string é diferente de F1
//CASO
//tab - MMMM
//tab2 - --F1
//(pq a gente quer representar que o matinho explodiu)
if(strcmp(tab2[i][j].str2, range_symbol) != 0){
strcpy(tab2[i][j].str1, tab[i][j].str1);
}
//explodir o matinho
}else if(strcmp(tab2[i][j].str2, range_symbol) == 0)
{
strcpy(tab2[i][j].str1, "--");
}
//verificar a segunda string
if(strcmp(tab2[i][j].str2, tab[i][j].str2) != 0)
{
//verifica se no mapa secundario tem o char 'F' o qual é normal que exista
if(strcmp(tab2[i][j].str2, range_symbol) == 0) continue;
strcpy(tab2[i][j].str2, tab[i][j].str2);
}
}
}
}
// cria mapa com pesesos para cada posição
void criarMapaPeso(){
/*
-------------------
| POSIÇÃO | PESO |
-------------------
| Pnosso | 0 |
-------------------
| pInimigo | 0 |
-------------------
| -- | 1 |
-------------------
| MM | 3 |
-------------------
| XX | 999 |
-------------------
| Fnosso | 5 |
-------------------
| Finimigo | 98 |
-------------------
| Bnosso | 999 |
-------------------
| Binimigo | 999 |
-------------------
*/
int i, j;
for (i = 0; i < 11; ++i){
for (j = 0; j < 13; ++j){
if(strcmp(tab2[i][j].str1, s) == 0) tabPeso[i][j] = 2;
else if(strcmp(tab2[i][j].str1, enemyS) == 0) tabPeso[i][j] = 0;
else if(strcmp(tab2[i][j].str2, "--") == 0) tabPeso[i][j] = 1;
else if(strcmp(tab2[i][j].str2, "MM") == 0) tabPeso[i][j] = 3;
else if(strcmp(tab2[i][j].str2, "XX") == 0) tabPeso[i][j] = 999;
else if(strcmp(tab2[i][j].str2, "F1") == 0) tabPeso[i][j] = 5;
else if(strcmp(tab2[i][j].str2, "F2") == 0) tabPeso[i][j] = 98;
else if(strcmp(tab2[i][j].str2, bomb) == 0) tabPeso[i][j] = 999;
else if(strcmp(tab2[i][j].str2, enemyBomb) == 0) tabPeso[i][j] = 999;
}
}
}
// cria um .txt com os pesos do mapa
void escreverMapaPeso(){
fp = fopen("mapaPeso.txt","w+");
int i, j;
for(i = 0; i < 11; i++){
for(j = 0; j < 13; j++){
// imprime só o utimo valor do peso para ficar uma matriz arrumada
fprintf(fp,"%d ", tabPeso[i][j]%10);
}
fprintf(fp, "\n");
}
fclose(fp);
}
//funcao retorna a posicao corrente de determinado jogador, cuja string (P1 ou P2) e passada como parametro.
pos cur_pos(char* player)
{
pos posi;
int i,j;
for(i = 0; i < 11; i++){
for(j = 0; j < 13; j++)
{
if(strcmp(tab[i][j].str1, player) == 0 || strcmp(tab[i][j].str1, "B3") == 0 )
{
posi.i = i;
posi.j = j;
return posi;
}
}
}
}
/*
Returns the distance between two objects;
*/
double distance(int x1,int x2,int y1,int y2){
double dist = sqrt((y2-y1)^2 + (x2-x1)^2);
return dist;
}
//funcao que checa se o movimento e valido em determinada posicao
int check(int x, int y)
{
if(x>=0 && x<11 && y>=0 && y<13 && (strcmp(tab[x][y].str2,"--")==0 || strcmp(tab[x][y].str2,"+F")==0 || strcmp(tab[x][y].str2,"+B")==0) && strcmp(tab[x][y].str2,"MM") != 0 && strcmp(tab[x][y].str2,"XX") != 0)
return 1;
else
return 0;
}
int checkPos(int x, int y){
if(x >= 0 && x < 11 && y >=0 && y <13){
return 1;
}else
return 0;
}
int manhattanDistance(int playerX,int enemyX,int playerY, int enemyY){
int distanceX = abs(playerX - enemyX);
int distanceY = abs(playerY - enemyY);
return distanceX+distanceY;
}
//Caso seja uma posicao 100% segura (ou seja, na diagonal) da minha bomba inicial, então é uma boa posição para ir.
void checkSafety(int x, int y){
if(qtd_bombas > 0){
if(x-1 != bombas[0].i && y != bombas[0].j){
move[1] += 3;
}
if(x != bombas[0].i && y-1 != bombas[0].j){
move[2] += 3;
}
if(x+1 != bombas[0].i && y != bombas[0].j){
move[3] += 3;
}
if(x != bombas[0].i && y+1 != bombas[0].j){
move[4] += 3;
}
}
}
// coloca os FF no mapa2
void bombaColocouMapa2(int x, int y){
strcpy(tab2[x][y].str2, bomb);
int i, j, matoAntes, paredeAntes;
for(j = 1; j<5; j++){
matoAntes = 0;
paredeAntes = 0;
for(i = 1; i<=range; i++){
// verifica se teve mato em alguma posição anterios
if(checkPos(x+(dx[j]*i), y+(dy[j]*i)) && strcmp(tab2[x+(dx[j]*i)][y+(dy[j]*i)].str2, "MM") == 0)
matoAntes = 1;
// verifica se teve parede em alguma posição anterior
else if(checkPos(x+(dx[j]*i), y+(dy[j]*i)) && strcmp(tab2[x+(dx[j]*i)][y+(dy[j]*i)].str2, "XX") == 0)
paredeAntes = 1;
// coloca FF no mapa se não tive passado anteriormente por uma parece ou mato
if(check(x+(dx[j]*i), y+(dy[j]*i)) && !matoAntes && !paredeAntes)
strcpy(tab2[x+(dx[j]*i)][y+(dy[j]*i)].str2, range_symbol);
}
}
}
//funcao se determina se devo soltar uma bomba ou nao
int soltarbomba(int x, int y,int enemyX,int enemyY){
if(qtd_bombas < 1){
int impossibleWays = 0;
if(!check(x,y+1)){
impossibleWays++;
}
if(!check(x+1,y)){
impossibleWays++;
}
if(!check(x-1,y)){
impossibleWays++;
}
if(!check(x,y-1)){
impossibleWays++;
}
if(impossibleWays == 3 || ( x == enemyX && (distance(x,enemyX,y,enemyY) < 4))|| (y == enemyY && (distance(x,enemyX,y,enemyY) < 4))){ //If all my surroundings are gone then it is better to get rid off it.
bombas[qtd_bombas].i = x;
bombas[qtd_bombas].j = y;
bombas[qtd_bombas].range = range;
qtd_bombas++;
bombaColocouMapa2(x, y);
return 1;
}
if(checkPos(x,y+1)){
/*Checo se à minha direita existe mato/
y < enemyY is because if I am the left of him, going is probably going to make me closer to him
*/
if(strcmp(tab[x][y+1].str2,"MM") == 0 && (y < enemyY)){
bombas[qtd_bombas].i = x;
bombas[qtd_bombas].j = y;
bombas[qtd_bombas].range = range;
qtd_bombas++;
bombaColocouMapa2(x, y);
return 1;
}
}
if(checkPos(x+1,y)){
/*Checo se abaixo de mim existe mato/
x < enemyX is because if I am the up to him, going is probably going to make me closer to him
*/
if(strcmp(tab[x+1][y].str2,"MM") == 0 && (x < enemyX)){
bombas[qtd_bombas].i = x;
bombas[qtd_bombas].j = y;
bombas[qtd_bombas].range = range;
qtd_bombas++;
bombaColocouMapa2(x, y);
return 1;
}
}
if(checkPos(x,y-1)){
/*Checo se à minha direita existe mato/
y < enemyY is because if I am the left of him, going is probably going to make me closer to him
*/
if(strcmp(tab[x][y-1].str2,"MM") == 0 && (y > enemyY)){
bombas[qtd_bombas].i = x;
bombas[qtd_bombas].j = y;
bombas[qtd_bombas].range = range;
qtd_bombas++;
bombaColocouMapa2(x, y);
return 1;
}
}
if(checkPos(x-1,y)){
/*Checo se à minha direita existe mato/
y < enemyY is because if I am the left of him, going is probably going to make me closer to him
*/
if(strcmp(tab[x-1][y].str2,"MM") == 0 && (x > enemyX)){
bombas[qtd_bombas].i = x;
bombas[qtd_bombas].j = y;
bombas[qtd_bombas].range = range;
qtd_bombas++;
bombaColocouMapa2(x, y);
return 1;
}
}
}
return 0;
}
//funcao: determina se caso voce exploda a ultima bomba, voce continue vivo ou qual lugar possui a maior distancia em relacao a bomba;
void bealive(int x, int y, int jogarbomba){
if(qtd_bombas > 0){
if(jogarbomba == 0){
if(check(x-1,y)){
if(x < bombas[0].i){
move[1] += 3;
}
}
if(check(x,y-1)){
if((y < bombas[0].j)){
move[2] += 3;
}
}
if(check(x+1,y)){
if(x > bombas[0].i){
move[3] += 3;
}
}
if(check(x,y+1)){
if(y > bombas[0].j){
move[4] += 3;
}
}
}else{ //jogarbomba == 1
if(check(x-1,y)){
if((distance(x-1,bombas[0].i,y,bombas[0].j) > distance(x,bombas[0].i,y,bombas[0].j)) && bombas[0].i != x-1){
move[1] += 1;
if(distance(x-1,bombas[0].i,y,bombas[0].j) < 4){
bealive(x-1,y,jogarbomba);
}
}
}
if(check(x,y-1)){
if((distance(x,bombas[0].i,y-1,bombas[0].j) > distance(x,bombas[0].i,y,bombas[0].j)) &&
bombas[0].j != y-1){
move[2] += 1;
if(distance(x,bombas[0].i,y-1,bombas[0].j) < 4){
bealive(x,y-1,jogarbomba);
}
}
}
if(check(x+1,y)){
if((distance(x+1,bombas[0].i,y,bombas[0].j) > distance(x,bombas[0].i,y,bombas[0].j))
&& bombas[0].i != x+1){
move[3] +=1;
if(distance(x+1,bombas[0].i,y,bombas[0].j) < 4){
bealive(x+1,y,jogarbomba);
}
}
}
if(check(x,y+1)){
if((distance(x,bombas[0].i,y+1,bombas[0].j) > distance(x,bombas[0].i,y,bombas[0].j)) &&
bombas[0].j != y+1){
move[4] += 1;
if(distance(x,bombas[0].i,y+1,bombas[0].j) < 4){
bealive(x,y+1,jogarbomba);
}
}
}
}
}
}
//Arquivo de teste para escrever os dados que possuo;
void escreverValores(){
fp = fopen("valores.txt","a");
int i = 0;
for(; i < 5; i++){
fprintf(fp," %d ", move[i]);
}
fprintf(fp,"\n");
fclose(fp);
}
//Escreve os dados das bombas atualizadas no arquivo;
void escreverBombas(){
fp = fopen("bombas.txt","w+");
int i = 0;
//nossas bombas
fprintf(fp, "%d %d %d\n",qtd_bombas,qtd_bombas_max,range); //tem que guardar o range atual
for(; i < qtd_bombas; i++){
fprintf(fp,"%d %d %d ", bombas[i].range, bombas[i].i,bombas[i].j);
}
//bombas do inimigo
fprintf(fp, "\n%d %d %d\n", qtd_bombas_enemy, qtd_bombas_max_enemy, range_enemy);
for(i = 0; i < qtd_bombas_enemy; i++){
fprintf(fp, "%d %d %d ", bombas_enemy[i].range, bombas_enemy[i].i, bombas_enemy[i].j);
}
fclose(fp);
}
//Le as coordenadas atuais das bombas;
void lerBombas(){
fp = fopen("bombas.txt","r+");
if(fp != NULL){
int i = 0;
//nossas bombas
fscanf(fp," %d %d %d", &qtd_bombas, &qtd_bombas_max, &range);
for(; i < qtd_bombas; i++){
fscanf(fp,"%d %d %d ", &bombas[i].range, &bombas[i].i, &bombas[i].j);
}
//bombas do inimigo
fscanf(fp, " %d %d %d", &qtd_bombas_enemy, &qtd_bombas_max_enemy, &range_enemy);
for(i = 0; i < qtd_bombas_enemy; i++){
fscanf(fp, "%d %d %d ", &bombas_enemy[i].range, &bombas_enemy[i].i, &bombas_enemy[i].j);
}
fclose(fp);
}
}
void modifybombs(){
int i = 0;
for(;i < qtd_bombas;i++){
bombas[i] = bombas[i+1];
}
}
// metodo para retirar B# e FF da bomba que explodiu
void bombaExplodiuMapa2()
{
int i = bombas[0].i;
int j = bombas[0].j;
int range = bombas[0].range;
strcpy(tab2[i][j].str2, "--");
int k, l;
for(l = 1; l<5; l++)
{
for(k = 1; k<=range; k++)
{
if(check(i+(dx[l]*k), j+(dy[l]*k)))
{
strcpy(tab2[i+(dx[l]*k)][j+(dy[l]*k)].str1, "--");
strcpy(tab2[i+(dx[l]*k)][j+(dy[l]*k)].str2, "--");
}
}
}
}
//x is the player position in the x-axis
//y is the player position in the y-axis
int explodirbomba(int x,int y,int where){
int retorno = 0;
if(qtd_bombas > 0){
int i = 0;
int j = 0;//size of the map
switch(where){
case 0:
if(check(x,y)){ //Parece desnecessário , contudo, estou utilizando para fins de teste.
if(strcmp(tab2[x][y].str2,range_symbol) != 0 && strcmp(tab2[x][y].str2,bomb) != 0){
retorno = 1;
}
}
break;
case 1:
if(check(x-1,y)){
if(strcmp(tab2[x-1][y].str2,range_symbol) != 0 && strcmp(tab2[x-1][y].str2,bomb) != 0){
retorno = 1;
}
}
break;
case 2:
if(check(x,y-1)){
if(strcmp(tab2[x][y-1].str2,range_symbol) != 0 && strcmp(tab2[x][y-1].str2,bomb) != 0){
retorno = 1;
}
}
break;
case 3:
if(check(x+1,y)){
if(strcmp(tab2[x+1][y].str2,range_symbol) != 0 && strcmp(tab2[x+1][y].str2,bomb) != 0){
retorno = 1;
}
}
break;
case 4:
if(check(x,y+1)){
if(strcmp(tab2[x][y+1].str2,range_symbol) != 0 && strcmp(tab2[x][y+1].str2,bomb) != 0){
retorno = 1;
}
}
break;
}
if(retorno == 1){
qtd_bombas--;
bombaExplodiuMapa2();
modifybombs();
if(qtd_bombas > 0) bombaExplodiuMapa2(bombas[0].i,bombas[0].j); //Caso a quantidade de bombas ainda seja maior do que 0, teremos de colocar os novos F's no mapa pois agora bomba[1] pasosu a ser bomba[0].
}
}
return retorno;
}
void lerPosicao(){
fp = fopen("posicao.txt","r+");
if(fp != NULL){
fscanf(fp,"%d %d ",&oldPos.i, &oldPos.j);
fclose(fp);
}
}
void escreverPosicao(int x , int y){
fp = fopen("posicao.txt","w+");
fprintf(fp," %d %d ",x,y);
fclose(fp);
}
void checkWays(int playerX,int playerY){
if(check(playerX,playerY) == 0){ //parado
move[0] -= 1;
}
if(check(playerX-1,playerY) == 0){ //sobe
move[1] -= 1;
}
if(check(playerX,playerY-1) == 0){ //esquerda
move[2] -= 1;
}
if(check(playerX+1,playerY) == 0){ //desce
move[3] -= 1;
}
if(check(playerX,playerY+1) == 0){ //direta
move[4] -= 1;
}
}
/*
Funcao feita para que ele nao pense em retornar a uma posicao antiga(desnecessario)
*/
void avoidOldPosition(int x, int y,int jogarbomba){
if(jogarbomba == 0){ //Sem soltar bomba
if(oldPos.i != x-1 && check(x-1,y)){
move[1] += 1;
}
if(oldPos.j != y-1 && check(x,y-1)){
move[2] += 1;
}
if(oldPos.i != x+1 && check(x+1,y)){
move[3] += 1;
}
if(oldPos.j != y+1 && check(x,y+1)){
move[4] += 1;
}
}
}
/*
O lado que conseguir a maior quantidade de "pontos" sera o lado escolhido
*/
int MAIOR(int *vec)
{
int i, maior = 0;
for(i = 0; i<5; i++)
{
if(vec[i]>vec[maior])
maior = i;
}
return maior;
}
/*
Metodo responsavel pela medida de distancia;
*/
void getcloser(int enemyX,int enemyY, int playerX, int playerY){
if(check(playerX-1,playerY)){ //sobe
if(playerX > enemyX){
move[1] += 3; //It holds more weight
}
if(manhattanDistance(playerX -1, enemyX,playerY,enemyY) < manhattanDistance(playerX,enemyX,playerY,enemyY)){
move[1] += 1;
}
}
if(check(playerX,playerY-1)){ //esquerda
if(playerY > enemyY ){
move[2] += 3; //It holds more weight
}
if(manhattanDistance(playerX, enemyX,playerY-1,enemyY) < manhattanDistance(playerX,enemyX,playerY,enemyY)){
move[2] += 1;
}
}
if(check(playerX+1,playerY)){ //desce
if(playerX < enemyX ){ // This bombas[0].i /or j != playerX or Y is because it's starting a loop
move[3] += 3;
}
if(manhattanDistance(playerX +1, enemyX,playerY,enemyY) < manhattanDistance(playerX,enemyX,playerY,enemyY)){
move[3] += 1;
}
}
if(check(playerX,playerY+1)){ //direita
if(playerY < enemyY && bombas[0].j != playerY){
move[4] += 3;
}
if(manhattanDistance(playerX, enemyX,playerY+1,enemyY) < manhattanDistance(playerX,enemyX,playerY,enemyY)){
move[4] += 1;
}
}
}
//It returns if in that direction there is a path with the minimum size required.
/*
Parameters(x,y) : Position of our player (x,y)
Size: The size of the path we want
sizeFound: The size of the path
Paramerters(oldx,oldy) : To avoid going back to the previous tile or position (avoiding a huge loop), it's default is the inital position x and y
*/
int findSize(int x, int y, int size,int sizeFound,int oldx,int oldy){
int ways = 0;
if(size == sizeFound){
return sizeFound;
}
if(check(x-1,y) && posIam.i != x-1 && (sizeFound < size) && oldx != x-1){
ways++;
sizeFound += findSize(x-1,y,size,sizeFound+1,x,y);
}
if(check(x,y-1) && posIam.j != y-1 && (sizeFound < size) && oldy != y-1){
sizeFound += findSize(x,y-1,size,sizeFound+1,x,y);
}
if(check(x+1,y) && posIam.i != x+1 && (sizeFound < size) && oldx != x+1){
ways++;
sizeFound += findSize(x+1,y,size,sizeFound+1,x,y);
}
if(check(x,y+1) && posIam.j != y+1 && (sizeFound < size) && oldy != y-1){
ways++;
sizeFound += findSize(x,y+1,size,sizeFound+1,x,y);
}
if(ways == 0 && sizeFound < size){
return 0;
}
if(sizeFound >= size){
return 1;
}else{
return 0;
}
}
//verifica se as bombas ainda estão no lugar ou se foram explodidas
//verifiquem se esta certo
void verificar_bombas(){
int i, j;
for(i = 0; i < qtd_bombas_enemy; i++){
if(strcmp(tab[bombas_enemy[i].i][bombas_enemy[i].j].str2, enemyBomb) != 0){
//se for o ultimo do array
if(i == qtd_bombas_enemy-1){
qtd_bombas_enemy--;
}else{
//igual ao modifyBombs
for(j = i+1;j < qtd_bombas_enemy; j++){
bombas_enemy[i-1] = bombas_enemy[i];
}
qtd_bombas_enemy--;
}
}
}
}
//recebe a pos do inimigo e verifica se ha bombas por perto
void bombas_inimigo(int x, int y){
if(checkPos(x+1,y)){
if(strcmp(tab[x+1][y].str2,enemyBomb) == 0){
if(verificar_bombas_inimigo(x+1,y)){
bombas_enemy[qtd_bombas_enemy].i = x+1;
bombas_enemy[qtd_bombas_enemy].j = y;
bombas_enemy[qtd_bombas_enemy].range = range_enemy;
qtd_bombas_enemy++;
}
}
}
if(checkPos(x,y+1)){
if(strcmp(tab[x][y+1].str2,enemyBomb) == 0){
if(verificar_bombas_inimigo(x,y+1)){
bombas_enemy[qtd_bombas_enemy].i = x;
bombas_enemy[qtd_bombas_enemy].j = y+1;
bombas_enemy[qtd_bombas_enemy].range = range_enemy;
qtd_bombas_enemy++;
}
}
}
if(checkPos(x-1,y)){
if(strcmp(tab[x-1][y].str2,enemyBomb) == 0){
if(verificar_bombas_inimigo(x-1,y)){
bombas_enemy[qtd_bombas_enemy].i = x-1;
bombas_enemy[qtd_bombas_enemy].j = y;
bombas_enemy[qtd_bombas_enemy].range = range_enemy;
qtd_bombas_enemy++;
}
}
}
if(checkPos(x,y-1)){
if(strcmp(tab[x][y-1].str2,enemyBomb) == 0){
if(verificar_bombas_inimigo(x,y-1)){
bombas_enemy[qtd_bombas_enemy].i = x;
bombas_enemy[qtd_bombas_enemy].j = y-1;
bombas_enemy[qtd_bombas_enemy].range = range_enemy;
qtd_bombas_enemy++;
}
}
}
if(checkPos(x,y)){
if(strcmp(tab[x][y].str2,enemyBomb) == 0){
if(verificar_bombas_inimigo(x,y)){
bombas_enemy[qtd_bombas_enemy].i = x;
bombas_enemy[qtd_bombas_enemy].j = y;
bombas_enemy[qtd_bombas_enemy].range = range_enemy;
qtd_bombas_enemy++;
}
}
}
}
int verificar_bombas_inimigo(int x, int y){
int i;
for(i = 0; i < qtd_bombas_enemy; i++){
if(bombas_enemy[i].i == x && bombas_enemy[i].j == y){
return 0;
}
}
return 1;
}
void verificarBonus()
{
int i;
// Bonus Range
for(i = 0; i < qtdBonusRange; i++)
{
//se o bonus nao estiver mais la
if(strcmp(tab[bonus_range[i].i][bonus_range[i].j].str2, "+F") != 0)
{
if(strcmp(tab[bonus_range[i].i][bonus_range[i].j].str1, s) == 0) range++;
else range_enemy++;
//caso seja o ultimo bonus do array
if(i == qtdBonusRange-1) qtdBonusRange--;
//caso NAO seja o ultimo bonus do array
else
{
bonus_range[i] = bonus_range[qtdBonusRange-1];
qtdBonusRange--;
}
}
}
// Bonus Bomba
for(i = 0; i < qtdBonusBomba; i++)
{
//se o bonus nao estiver mais la
if(strcmp(tab[bonus_bombas[i].i][bonus_bombas[i].j].str2, "+B") != 0)
{
if(strcmp(tab[bonus_bombas[i].i][bonus_bombas[i].j].str1, s) == 0) qtd_bombas_max++;
else qtd_bombas_max_enemy++;
//caso seja o ultimo bonus do array
if(i == qtdBonusBomba-1) qtdBonusBomba--;
//caso NAO seja o ultimo bonus do array
else
{
bonus_bombas[i] = bonus_bombas[qtdBonusBomba-1];
qtdBonusBomba--;
}
}
}
}
void escreverMapa2(){
/* Coloca as bombas nas posições */
int l, k = 0;
for(; l < qtd_bombas;l++){
bombaColocouMapa2(bombas[l].i,bombas[l].j);
}
for(; k < qtd_bombas_enemy;k++){
bombaColocouMapa2(bombas_enemy[k].i,bombas_enemy[k].j);
}
fp = fopen("mapa2.txt","w+");
int i, j;
for(i = 0; i < 11; i++){
for(j = 0; j < 13; j++){
fprintf(fp,"%s%s ", tab2[i][j].str1, tab2[i][j].str2);
}
fprintf(fp, "\n");
}
fclose(fp);
}
//Le as coordenadas atuais das bombas;
void lerMapa2(){
fp = fopen("mapa2.txt","r+");
if(fp != NULL){
int i, j;
for(i = 0; i<11; i++)
{
for(j = 0; j<13; j++)
{
char temp[5];
fscanf(fp,"%s", temp);
tab2[i][j].str1[0] = temp[0];
tab2[i][j].str1[1] = temp[1];
tab2[i][j].str2[0] = temp[2];
tab2[i][j].str2[1] = temp[3];
}
}
fclose(fp);
}
}
void debug(int x, int y,int where){
fp = fopen("debug.txt","a+");
/* checar os vetores de movimento */
// int i = 0;
// for(; i < 5;i++){
// fprintf(fp, "move[%d] = %d ", i,move[i]);
// }
// fprintf(fp,"\n");
fprintf(fp,"x: %d , y : %d , tab[x,y] = %s , where = %d , range_symbol = %s , bomb = %s",x,y,tab2[x][y].str2,where,range_symbol,bomb);
fprintf(fp,"\n");
int i, j;
for(i = 0; i < 11; i++){
for(j = 0; j < 13; j++){
fprintf(fp,"%s%s ", tab2[i][j].str1, tab2[i][j].str2);
}
fprintf(fp, "\n");
}
fclose(fp);
}
void criarMapa2(){
int i, j;
if(rodada == 1){
for (i = 0; i < 11; i++)
{
for (j = 0; j < 13; j++)
{
strcpy(tab2[i][j].str1, tab[i][j].str1);
strcpy(tab2[i][j].str2, tab[i][j].str2);