Advertisement
Guest User

Untitled

a guest
Jun 30th, 2015
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 32.38 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. #include<time.h>
  5.  
  6. #define CIMA 1
  7. #define BAIXO 3
  8. #define DIREITA 4
  9. #define ESQUERDA 2
  10.  
  11. //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
  12. int id;
  13. //ID do jogo
  14. int ident;
  15.  
  16. int p1b=0, p1f=0, p2b=0, p2f=0; //info de bonus
  17. int iP = -1, jP = -1; //posição de um objetivo encontrado na BFSafe
  18. int iB = -1, jB = -1; //posição de um objetivo encontrado na BonusFS
  19. int iF = -1, jF = -1; //posição de um objetivo encontrado na P2FS
  20. int iS = -1, jS = -1; //posição de um objetivo encontrado na P2FSB
  21.  
  22. //esta estrutura guarda as posicoes em i e j num tabuleiro
  23. typedef struct pos {
  24. int i;
  25. int j;
  26. } pos;
  27.  
  28.  
  29. //vetores de deslocamento. {parado, sobe, esquerda, desce, direita}
  30. int dx[] = {0,-1,0,1,0};
  31. int dy[] = {0,0,-1,0,1};
  32. //baixo, cima, direita, esquerda
  33.  
  34. typedef struct tabela {
  35. char str1[3],str2[3];
  36. } tabela;
  37.  
  38. //mapa a ser lido.
  39. tabela tab[11][13], mapAnt[11][13], rexpmap[11][13], simulexpmap[11][13];
  40. int visited[11][13];
  41. pos parent[11][13];
  42. int bonusDist = 0;
  43. int distP2 = 0;
  44. int distP2B = 0;
  45. int uBombI, uBombJ;
  46. pos curP1, curP2;
  47. char P1[3], P2[3];
  48. char B1[3], B2[3];
  49.  
  50. //PRINTA MAPA ANTERIOR (ATUAL DA RODADA) NO ARQUIVO
  51. void salvarMapAnt () {
  52. FILE *matrix;
  53. matrix = fopen ("DDPmapAnt.txt", "w");
  54. if (matrix == NULL) return;
  55. int i,j;
  56. for(i=0; i<11; i++) {
  57. for(j=0; j<13; j++) fprintf (matrix, "%s%s ", tab[i][j].str1, tab[i][j].str2);
  58. fprintf (matrix, "\n");
  59. }
  60. fclose (matrix);
  61. }
  62.  
  63. //PRINTA MAPA DE EXPLOSÃO (não é necessário, só pra teste)
  64. void salvarExpMap () {
  65. FILE *matrix;
  66. matrix = fopen ("DDPexpMap.txt", "a");
  67. if (matrix == NULL) return;
  68. int i,j;
  69. for(i=0; i<11; i++) {
  70. for(j=0; j<13; j++) fprintf (matrix, "%s%s ", rexpmap[i][j].str1, rexpmap[i][j].str2);
  71. fprintf (matrix, "\n");
  72. }
  73. fprintf (matrix, "\n");
  74. fclose (matrix);
  75. }
  76. void salvarSimulExpMap () {
  77. FILE *matrix;
  78. matrix = fopen ("DDPsimulexpMap.txt", "a");
  79. if (matrix == NULL) return;
  80. int i,j;
  81. for(i=0; i<11; i++) {
  82. for(j=0; j<13; j++) fprintf (matrix, "%s%s ", simulexpmap[i][j].str1, simulexpmap[i][j].str2);
  83. fprintf (matrix, "\n");
  84. }
  85. fprintf (matrix, "\n");
  86. fclose (matrix);
  87. }
  88.  
  89. //LÊ ARQUIVO DO MAPA ANTERIOR
  90. void lerMapAnt () {
  91. FILE *matrix;
  92. matrix = fopen ("DDPmapAnt.txt", "r");
  93. if (matrix == NULL) return;
  94. int i,j;
  95. for(i=0; i<11; i++) {
  96. for(j=0; j<13; j++) {
  97. char temp[5];
  98. fscanf(matrix, "%s", temp);
  99. mapAnt[i][j].str1[0] = temp[0];
  100. mapAnt[i][j].str1[1] = temp[1];
  101. mapAnt[i][j].str2[0] = temp[2];
  102. mapAnt[i][j].str2[1] = temp[3];
  103. }
  104. }
  105. fclose (matrix);
  106. }
  107.  
  108. //PEGA MAPA ATUAL
  109. void leitura() {
  110. int i, j;
  111. for(i = 0; i<11; i++) {
  112. for(j = 0; j<13; j++) {
  113. char temp[5];
  114. scanf("%s", temp);
  115. tab[i][j].str1[0] = temp[0];
  116. tab[i][j].str1[1] = temp[1];
  117. tab[i][j].str2[0] = temp[2];
  118. tab[i][j].str2[1] = temp[3];
  119. }
  120. }
  121. }
  122.  
  123. //funcao retorna a posicao corrente de determinado jogador, cuja string (P1 ou P2) e passada como parametro.
  124. pos* cur_pos() {
  125. if (strcmp("P1", P1) == 0) {
  126. strcpy(P2, "P2");
  127. strcpy(B2, "B2");
  128. strcpy(B1, "B1");
  129. }
  130. else {
  131. strcpy(P2, "P1");
  132. strcpy(B2, "B1");
  133. strcpy(B1, "B2");
  134. }
  135. int i,j;
  136. for(i = 0; i < 11; i++)
  137. for(j = 0; j < 13; j++) {
  138. if(strcmp(tab[i][j].str1, P1) == 0) {
  139. curP1.i = i;
  140. curP1.j = j;
  141. }
  142. if(strcmp(tab[i][j].str1, P2) == 0) {
  143. curP2.i = i;
  144. curP2.j = j;
  145. }
  146. if(strcmp(tab[i][j].str2, "B3") == 0 || strcmp(tab[i][j].str1, "P3") == 0) {
  147. curP1.i = i;
  148. curP1.j = j;
  149. curP2.i = i;
  150. curP2.j = j;
  151. }
  152. }
  153. }
  154.  
  155.  
  156. //funcao que checa se o movimento e valido em determinada posicao
  157. int check(int x, int y) {
  158. 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))
  159. return 1;
  160. else
  161. return 0;
  162. }
  163.  
  164. //funcao que checa se o movimento e valido no mapa de explosão
  165. int safe(int x, int y) {
  166. if(x>=0 && x<11 && y>=0 && y<13 && (strcmp(rexpmap[x][y].str2,"--")==0 || strcmp(rexpmap[x][y].str2,"+F")==0 || strcmp(rexpmap[x][y].str2,"+B")==0))
  167. return 1;
  168. else
  169. return 0;
  170. }
  171.  
  172. //funcao que checa se o movimento e valido no mapa de simulação
  173. int safesim(int x, int y) {
  174. if(x>=0 && x<11 && y>=0 && y<13 && (strcmp(simulexpmap[x][y].str2,"--")==0 || strcmp(simulexpmap[x][y].str2,"+F")==0 || strcmp(simulexpmap[x][y].str2,"+B")==0))
  175. return 1;
  176. else
  177. return 0;
  178. }
  179.  
  180. //COMPARA MAPA ANTERIOR COM ATUAL E FAZ CONTAGEM DE BONUS
  181. // +1B +1F +2B +2F
  182. void bonusCount () {
  183. FILE *matrix;
  184. matrix = fopen("DDPBonusCount.txt", "r");
  185. fscanf(matrix, "%d %d %d %d", &p1b, &p1f, &p2b, &p2f);
  186. fclose(matrix);
  187.  
  188. if (strcmp(mapAnt[curP1.i][curP1.j].str2, "+B")==0) p1b++;
  189. else if (strcmp(mapAnt[curP1.i][curP1.j].str2, "+F")==0) p1f++;
  190. if (strcmp(mapAnt[curP2.i][curP2.j].str2, "+B")==0) p2b++;
  191. else if (strcmp(mapAnt[curP2.i][curP2.j].str2, "+F")==0) p2f++;
  192.  
  193. matrix = fopen("DDPBonusCount.txt", "w");
  194. fprintf(matrix, "%d %d %d %d", p1b, p1f, p2b, p2f);
  195. fclose(matrix);
  196. }
  197. //ATUALIZA NUMERO DE BOMBAS NO ARQUIVO
  198. void bombCount(int bombs) {
  199. FILE *matrix;
  200. matrix = fopen("DDPBombCount.txt", "w");
  201. fprintf(matrix, "%d", bombs);
  202. fclose(matrix);
  203. }
  204.  
  205. //RETORNA NUMERO DE BOMBAS
  206. int getBombCount() {
  207. int bombs = 0;
  208. FILE *matrix;
  209. matrix = fopen("DDPBombCount.txt", "r");
  210. fscanf(matrix, "%d", &bombs);
  211. fclose(matrix);
  212. return bombs;
  213. }
  214.  
  215. //VERIFICA SE JOGADOR ESTÁ PERTO DE DESTRUTÍVEL
  216. int pertoDestr() {
  217. int k;
  218. for (k=1; k<5; k++) {
  219. int i = curP1.i + dx[k];
  220. int j = curP1.j + dy[k];
  221. if (i<0 || i>=11 || j<0 || j>=13) continue;
  222. if(strcmp(tab[i][j].str1, "MM")==0) return 1;
  223. }
  224. return 0;
  225. }
  226.  
  227. //FAZ O MAPA DE EXPLOSÃO
  228. void wexpmap(tabela expmap[11][13]) {
  229. int i, j, k, m, n;
  230. for(i = 0; i < 11; i++)
  231. for(j = 0; j < 13; j++) {
  232. strcpy(expmap[i][j].str1, tab[i][j].str1);
  233. strcpy(expmap[i][j].str2, tab[i][j].str2);
  234. }
  235. int alcanceP1 = 2 + p1f;
  236. int alcanceP2 = 2 + p2f;
  237. for(i = 0; i < 11; i++)
  238. for(j = 0; j < 13; j++) {
  239. if(strcmp(expmap[i][j].str2, B1) != 0 && strcmp(expmap[i][j].str2, B2) != 0) continue;
  240. if(strcmp(expmap[i][j].str2, B1) == 0) { //BOMBA DO JOGADOR 1
  241. strcpy(expmap[i][j].str1, "FF");
  242. strcpy(expmap[i][j].str2, "FF");
  243. //cima
  244. m = i;
  245. n = j;
  246. for (k=0; k<alcanceP1; k++) {
  247. m += dx[CIMA];
  248. n += dy[CIMA];
  249. if (m<0 || m>=11 || n<0 || n>=13) break;
  250. if (strcmp(expmap[m][n].str2, B1)==0 || strcmp(expmap[m][n].str2, B2)==0) break;
  251. if (strcmp(expmap[m][n].str1, "XX")!=0 && strcmp(expmap[m][n].str2, "DD")!=0) {
  252. if (strcmp(expmap[m][n].str1, "MM")==0) {
  253. strcpy(expmap[m][n].str1, "FF");
  254. strcpy(expmap[m][n].str2, "DD");
  255. break;
  256. } else {
  257. strcpy(expmap[m][n].str1, "FF");
  258. strcpy(expmap[m][n].str2, "FF");
  259. }
  260. } else break;
  261. }
  262. //baixo
  263. m = i;
  264. n = j;
  265. for (k=0; k<alcanceP1; k++) {
  266. m += dx[BAIXO];
  267. n += dy[BAIXO];
  268. if (m<0 || m>=11 || n<0 || n>=13) break;
  269. if (strcmp(expmap[m][n].str2, B1)==0 || strcmp(expmap[m][n].str2, B2)==0) break;
  270. if (strcmp(expmap[m][n].str1, "XX")!=0 && strcmp(expmap[m][n].str2, "DD")!=0) {
  271. if (strcmp(expmap[m][n].str1, "MM")==0) {
  272. strcpy(expmap[m][n].str1, "FF");
  273. strcpy(expmap[m][n].str2, "DD");
  274. break;
  275. } else {
  276. strcpy(expmap[m][n].str1, "FF");
  277. strcpy(expmap[m][n].str2, "FF");
  278. }
  279. } else break;
  280. }
  281. //direita
  282. m = i;
  283. n = j;
  284. for (k=0; k<alcanceP1; k++) {
  285. m += dx[DIREITA];
  286. n += dy[DIREITA];
  287. if (m<0 || m>=11 || n<0 || n>=13) break;
  288. if (strcmp(expmap[m][n].str2, B1)==0 || strcmp(expmap[m][n].str2, B2)==0) break;
  289. if (strcmp(expmap[m][n].str1, "XX")!=0 && strcmp(expmap[m][n].str2, "DD")!=0) {
  290. if (strcmp(expmap[m][n].str1, "MM")==0) {
  291. strcpy(expmap[m][n].str1, "FF");
  292. strcpy(expmap[m][n].str2, "DD");
  293. break;
  294. } else {
  295. strcpy(expmap[m][n].str1, "FF");
  296. strcpy(expmap[m][n].str2, "FF");
  297. }
  298. } else break;
  299. }
  300. //esquerda
  301. m = i;
  302. n = j;
  303. for (k=0; k<alcanceP1; k++) {
  304. m += dx[ESQUERDA];
  305. n += dy[ESQUERDA];
  306. if (m<0 || m>=11 || n<0 || n>=13) break;
  307. if (strcmp(expmap[m][n].str2, B1)==0 || strcmp(expmap[m][n].str2, B2)==0) break;
  308. if (strcmp(expmap[m][n].str1, "XX")!=0 && strcmp(expmap[m][n].str2, "DD")!=0) {
  309. if (strcmp(expmap[m][n].str1, "MM")==0) {
  310. strcpy(expmap[m][n].str1, "FF");
  311. strcpy(expmap[m][n].str2, "DD");
  312. break;
  313. } else {
  314. strcpy(expmap[m][n].str1, "FF");
  315. strcpy(expmap[m][n].str2, "FF");
  316. }
  317. } else break;
  318. }
  319. } else if(strcmp(expmap[i][j].str2, B2) == 0) { //BOMBA DO JOGADOR 2
  320. strcpy(expmap[i][j].str1, "FF");
  321. strcpy(expmap[i][j].str2, "FF");
  322. //cima
  323. m = i;
  324. n = j;
  325. for (k=0; k<alcanceP2; k++) {
  326. m += dx[CIMA];
  327. n += dy[CIMA];
  328. if (m<0 || m>=11 || n<0 || n>=13) break;
  329. if (strcmp(expmap[m][n].str2, B1)==0 || strcmp(expmap[m][n].str2, B2)==0) break;
  330. if (strcmp(expmap[m][n].str1, "XX")!=0 && strcmp(expmap[m][n].str2, "DD")!=0) {
  331. if (strcmp(expmap[m][n].str1, "MM")==0) {
  332. strcpy(expmap[m][n].str1, "FF");
  333. strcpy(expmap[m][n].str2, "DD");
  334. break;
  335. } else {
  336. strcpy(expmap[m][n].str1, "FF");
  337. strcpy(expmap[m][n].str2, "FF");
  338. }
  339. } else break;
  340. }
  341. //baixo
  342. m = i;
  343. n = j;
  344. for (k=0; k<alcanceP2; k++) {
  345. m += dx[BAIXO];
  346. n += dy[BAIXO];
  347. if (m<0 || m>=11 || n<0 || n>=13) break;
  348. if (strcmp(expmap[m][n].str2, B1)==0 || strcmp(expmap[m][n].str2, B2)==0) break;
  349. if (strcmp(expmap[m][n].str1, "XX")!=0 && strcmp(expmap[m][n].str2, "DD")!=0) {
  350. if (strcmp(expmap[m][n].str1, "MM")==0) {
  351. strcpy(expmap[m][n].str1, "FF");
  352. strcpy(expmap[m][n].str2, "DD");
  353. break;
  354. } else {
  355. strcpy(expmap[m][n].str1, "FF");
  356. strcpy(expmap[m][n].str2, "FF");
  357. }
  358. } else break;
  359. }
  360. //direita
  361. m = i;
  362. n = j;
  363. for (k=0; k<alcanceP2; k++) {
  364. m += dx[DIREITA];
  365. n += dy[DIREITA];
  366. if (m<0 || m>=11 || n<0 || n>=13) break;
  367. if (strcmp(expmap[m][n].str2, B1)==0 || strcmp(expmap[m][n].str2, B2)==0) break;
  368. if (strcmp(expmap[m][n].str1, "XX")!=0 && strcmp(expmap[m][n].str2, "DD")!=0) {
  369. if (strcmp(expmap[m][n].str1, "MM")==0) {
  370. strcpy(expmap[m][n].str1, "FF");
  371. strcpy(expmap[m][n].str2, "DD");
  372. break;
  373. } else {
  374. strcpy(expmap[m][n].str1, "FF");
  375. strcpy(expmap[m][n].str2, "FF");
  376. }
  377. } else break;
  378. }
  379. //esquerda
  380. m = i;
  381. n = j;
  382. for (k=0; k<alcanceP2; k++) {
  383. m += dx[ESQUERDA];
  384. n += dy[ESQUERDA];
  385. if (m<0 || m>=11 || n<0 || n>=13) break;
  386. if (strcmp(expmap[m][n].str2, B1)==0 || strcmp(expmap[m][n].str2, B2)==0) break;
  387. if (strcmp(expmap[m][n].str1, "XX")!=0 && strcmp(expmap[m][n].str2, "DD")!=0) {
  388. if (strcmp(expmap[m][n].str1, "MM")==0) {
  389. strcpy(expmap[m][n].str1, "FF");
  390. strcpy(expmap[m][n].str2, "DD");
  391. break;
  392. } else {
  393. strcpy(expmap[m][n].str1, "FF");
  394. strcpy(expmap[m][n].str2, "FF");
  395. }
  396. } else break;
  397. }
  398. }
  399. }
  400. salvarExpMap();
  401. }
  402.  
  403. //BUSCA DIREÇÃO PRA LUGAR SEGURO
  404. void BFSafe(pos init, char target[], tabela passada[11][13]) {
  405. //INITIALIZE
  406. pos Q[143];
  407. int m, n;
  408. for(m=0; m<11; m++) {
  409. for(n=0; n<13; n++) {
  410. parent[m][n].i = -1;
  411. parent[m][n].j = -1;
  412. if (strcmp(passada[m][n].str1, "XX") == 0 || strcmp(passada[m][n].str1, "MM") == 0
  413. || strcmp(passada[m][n].str2, "DD") == 0) visited[m][n]=1;
  414. else visited[m][n] = 0;
  415. }
  416. }
  417. for(n=0; n<143; n++) {
  418. Q[n].i = -1;
  419. Q[n].j = -1;
  420. }
  421. Q[0] = init;
  422. visited[Q[0].i][Q[0].j] = 1;
  423. int q = 143, y = 0, x = 1, c = 0;
  424.  
  425. //BEGIN THE LOOP
  426. while (q > 0 && y <= x) {
  427. int i, j;
  428. i = Q[y].i + 1;
  429. j = Q[y].j;
  430. if (i>=0 && j>=0 && i<11 && j<13) {
  431. if(!visited[i][j]) {
  432. visited[i][j] = 1;
  433. parent[i][j] = Q[y];
  434. if (strcmp(passada[i][j].str1, target) == 0) {
  435. iP = i;
  436. jP = j;
  437. return;
  438. }
  439. Q[x].i = i;
  440. Q[x].j = j;
  441. x++;
  442. }
  443. }
  444. i = Q[y].i;
  445. j = Q[y].j + 1;
  446. if (i>=0 && j>=0 && i<11 && j<13) {
  447. if(!visited[i][j]) {
  448. visited[i][j] = 1;
  449. parent[i][j] = Q[y];
  450. if (strcmp(passada[i][j].str1, target) == 0) {
  451. iP = i;
  452. jP = j;
  453. return;
  454. }
  455. Q[x].i = i;
  456. Q[x].j = j;
  457. x++;
  458. }
  459. }
  460. i = Q[y].i - 1;
  461. j = Q[y].j;
  462. if (i>=0 && j>=0 && i<11 && j<13) {
  463. if(!visited[i][j]) {
  464. visited[i][j] = 1;
  465. parent[i][j] = Q[y];
  466. if (strcmp(passada[i][j].str1, target) == 0) {
  467. iP = i;
  468. jP = j;
  469. return;
  470. }
  471. Q[x].i = i;
  472. Q[x].j = j;
  473. x++;
  474. }
  475. }
  476. i = Q[y].i;
  477. j = Q[y].j - 1;
  478. if (i>=0 && j>=0 && i<11 && j<13) {
  479. if(!visited[i][j]) {
  480. visited[i][j] = 1;
  481. parent[i][j] = Q[y];
  482. if (strcmp(passada[i][j].str1, target) == 0) {
  483. iP = i;
  484. jP = j;
  485. return;
  486. }
  487. Q[x].i = i;
  488. Q[x].j = j;
  489. x++;
  490. }
  491. }
  492. q--;
  493. y++;
  494. }
  495. }
  496.  
  497. int convDir(pos init, int i, int j) {
  498. int deltaI = i - init.i;
  499. int deltaJ = j - init.j;
  500. if (deltaI==-1 && deltaJ==0) return 1;
  501. if (deltaI==0 && deltaJ==-1) return 2;
  502. if (deltaI==1 && deltaJ==0) return 3;
  503. if (deltaI==0 && deltaJ==1) return 4;
  504. }
  505.  
  506. int bestDir(pos init, char target[]) {
  507. BFSafe(init, target, rexpmap);
  508. //if (iP == -1 && jP == -1) return 0; //fica parado se não tiver como andar
  509. int i = iP;
  510. int j = jP;
  511. //percorre as casas visitadas em ordem reversa, começa do objetivo
  512. //e vai vendo qual a casa pai até encontrar o início (posição P1)
  513. while(parent[i][j].i != -1) {
  514. if (parent[i][j].i == init.i && parent[i][j].j == init.j) {
  515. return convDir(init, i, j); //retorna a direção a ser andada
  516. }
  517. int tempi = i;
  518. int tempj = j;
  519. i = parent[i][j].i;
  520. j = parent[tempi][tempj].j;
  521. }
  522. }
  523.  
  524. int bestSimDir(pos init, char target[]) {
  525. BFSafe(init, target, simulexpmap);
  526. //if (iP == -1 && jP == -1) return 0; //fica parado se não tiver como andar
  527. int i = iP;
  528. int j = jP;
  529. //percorre as casas visitadas em ordem reversa, começa do objetivo
  530. //e vai vendo qual a casa pai até encontrar o início (posição P1)
  531. while(parent[i][j].i != -1) {
  532. if (parent[i][j].i == init.i && parent[i][j].j == init.j) {
  533. return convDir(init, i, j); //retorna a direção a ser andada
  534. }
  535. int tempi = i;
  536. int tempj = j;
  537. i = parent[i][j].i;
  538. j = parent[tempi][tempj].j;
  539. }
  540. }
  541.  
  542. //BUSCA BONUS MAIS PERTO E GRAVA SUA DIREÇÃO
  543. void BonusFS(pos init) {
  544. //INITIALIZE
  545. pos Q[143];
  546. int m, n;
  547. for(m=0; m<11; m++) {
  548. for(n=0; n<13; n++) {
  549. parent[m][n].i = -1;
  550. parent[m][n].j = -1;
  551. if (strcmp(rexpmap[m][n].str1, "XX") == 0 || strcmp(rexpmap[m][n].str1, "MM") == 0
  552. || strcmp(rexpmap[m][n].str2, "DD") == 0 || strcmp(rexpmap[m][n].str1, "FF") == 0) visited[m][n]=1;
  553. else visited[m][n] = 0;
  554. }
  555. }
  556. for(n=0; n<143; n++) {
  557. Q[n].i = -1;
  558. Q[n].j = -1;
  559. }
  560. Q[0] = init;
  561. visited[Q[0].i][Q[0].j] = 1;
  562. int q = 143, y = 0, x = 1, c = 0;
  563.  
  564. //BEGIN THE LOOP
  565. while (q > 0 && y <= x) {
  566. int i, j;
  567. i = Q[y].i + 1;
  568. j = Q[y].j;
  569. if (i>=0 && j>=0 && i<11 && j<13) {
  570. if(!visited[i][j]) {
  571. visited[i][j] = 1;
  572. parent[i][j] = Q[y];
  573. if (strcmp(rexpmap[i][j].str2, "+F") == 0) {
  574. iB = i;
  575. jB = j;
  576. return;
  577. }
  578. Q[x].i = i;
  579. Q[x].j = j;
  580. x++;
  581. }
  582. }
  583. i = Q[y].i;
  584. j = Q[y].j + 1;
  585. if (i>=0 && j>=0 && i<11 && j<13) {
  586. if(!visited[i][j]) {
  587. visited[i][j] = 1;
  588. parent[i][j] = Q[y];
  589. if (strcmp(rexpmap[i][j].str2, "+F") == 0) {
  590. iB = i;
  591. jB = j;
  592. return;
  593. }
  594. Q[x].i = i;
  595. Q[x].j = j;
  596. x++;
  597. }
  598. }
  599. i = Q[y].i - 1;
  600. j = Q[y].j;
  601. if (i>=0 && j>=0 && i<11 && j<13) {
  602. if(!visited[i][j]) {
  603. visited[i][j] = 1;
  604. parent[i][j] = Q[y];
  605. if (strcmp(rexpmap[i][j].str2, "+F") == 0) {
  606. iB = i;
  607. jB = j;
  608. return;
  609. }
  610. Q[x].i = i;
  611. Q[x].j = j;
  612. x++;
  613. }
  614. }
  615. i = Q[y].i;
  616. j = Q[y].j - 1;
  617. if (i>=0 && j>=0 && i<11 && j<13) {
  618. if(!visited[i][j]) {
  619. visited[i][j] = 1;
  620. parent[i][j] = Q[y];
  621. if (strcmp(rexpmap[i][j].str2, "+F") == 0) {
  622. iB = i;
  623. jB = j;
  624. return;
  625. }
  626. Q[x].i = i;
  627. Q[x].j = j;
  628. x++;
  629. }
  630. }
  631. q--;
  632. y++;
  633. }
  634. }
  635.  
  636. int getBonusDist(pos init) {
  637. BonusFS(init);
  638. if (iB == -1 && jB == -1) return 0;
  639. int i = iB;
  640. int j = jB;
  641. //percorre as casas visitadas em ordem reversa, começa do objetivo
  642. //e vai vendo qual a casa pai até encontrar o início (posição P1)
  643. while(parent[i][j].i != -1) {
  644. bonusDist++;
  645. if (parent[i][j].i == init.i && parent[i][j].j == init.j) {
  646. return convDir(init, i, j); //retorna a direção a ser andada
  647. }
  648. int tempi = i;
  649. int tempj = j;
  650. i = parent[i][j].i;
  651. j = parent[tempi][tempj].j;
  652. }
  653. }
  654.  
  655. //GRAVA DIREÇÃO PRA P2
  656. void P2FS(pos init) {
  657. //INITIALIZE
  658. pos Q[143];
  659. int m, n;
  660. for(m=0; m<11; m++) {
  661. for(n=0; n<13; n++) {
  662. parent[m][n].i = -1;
  663. parent[m][n].j = -1;
  664. if (strcmp(tab[m][n].str1, "XX") == 0 || strcmp(tab[m][n].str1, "MM") == 0
  665. || strcmp(tab[m][n].str2, "DD") == 0 || strcmp(tab[m][n].str1, "FF") == 0) visited[m][n]=1;
  666. else visited[m][n] = 0;
  667. }
  668. }
  669. for(n=0; n<143; n++) {
  670. Q[n].i = -1;
  671. Q[n].j = -1;
  672. }
  673. Q[0] = init;
  674. visited[Q[0].i][Q[0].j] = 1;
  675. int q = 143, y = 0, x = 1, c = 0;
  676.  
  677. //BEGIN THE LOOP
  678. while (q > 0 && y <= x) {
  679. int i, j;
  680. i = Q[y].i + 1;
  681. j = Q[y].j;
  682. if (i>=0 && j>=0 && i<11 && j<13) {
  683. if(!visited[i][j]) {
  684. visited[i][j] = 1;
  685. parent[i][j] = Q[y];
  686. if (strcmp(tab[i][j].str1, P2) == 0) {
  687. iF = i;
  688. jF = j;
  689. return;
  690. }
  691. Q[x].i = i;
  692. Q[x].j = j;
  693. x++;
  694. }
  695. }
  696. i = Q[y].i;
  697. j = Q[y].j + 1;
  698. if (i>=0 && j>=0 && i<11 && j<13) {
  699. if(!visited[i][j]) {
  700. visited[i][j] = 1;
  701. parent[i][j] = Q[y];
  702. if (strcmp(tab[i][j].str1, P2) == 0) {
  703. iF = i;
  704. jF = j;
  705. return;
  706. }
  707. Q[x].i = i;
  708. Q[x].j = j;
  709. x++;
  710. }
  711. }
  712. i = Q[y].i - 1;
  713. j = Q[y].j;
  714. if (i>=0 && j>=0 && i<11 && j<13) {
  715. if(!visited[i][j]) {
  716. visited[i][j] = 1;
  717. parent[i][j] = Q[y];
  718. if (strcmp(tab[i][j].str1, P2) == 0) {
  719. iF = i;
  720. jF = j;
  721. return;
  722. }
  723. Q[x].i = i;
  724. Q[x].j = j;
  725. x++;
  726. }
  727. }
  728. i = Q[y].i;
  729. j = Q[y].j - 1;
  730. if (i>=0 && j>=0 && i<11 && j<13) {
  731. if(!visited[i][j]) {
  732. visited[i][j] = 1;
  733. parent[i][j] = Q[y];
  734. if (strcmp(tab[i][j].str1, P2) == 0) {
  735. iF = i;
  736. jF = j;
  737. return;
  738. }
  739. Q[x].i = i;
  740. Q[x].j = j;
  741. x++;
  742. }
  743. }
  744. q--;
  745. y++;
  746. }
  747. }
  748.  
  749. int getP2Dist(pos init) {
  750. P2FS(init);
  751. if (iF == -1 && jF == -1) return 0;
  752. int i = iF;
  753. int j = jF;
  754. //percorre as casas visitadas em ordem reversa, começa do objetivo
  755. //e vai vendo qual a casa pai até encontrar o início (posição P1)
  756. while(parent[i][j].i != -1) {
  757. distP2++;
  758. if (parent[i][j].i == init.i && parent[i][j].j == init.j) {
  759. return convDir(init, i, j); //retorna a direção a ser andada
  760. }
  761. int tempi = i;
  762. int tempj = j;
  763. i = parent[i][j].i;
  764. j = parent[tempi][tempj].j;
  765. }
  766. }
  767.  
  768. //GRAVA DIREÇÃO PRA P2 COM BLOQUEIO
  769. void P2FSB(pos init) {
  770. //INITIALIZE
  771. pos Q[143];
  772. int m, n;
  773. for(m=0; m<11; m++) {
  774. for(n=0; n<13; n++) {
  775. parent[m][n].i = -1;
  776. parent[m][n].j = -1;
  777. if (strcmp(tab[m][n].str1, "XX") == 0 || strcmp(tab[m][n].str2, "DD") == 0 || strcmp(tab[m][n].str1, "FF") == 0) visited[m][n]=1;
  778. else visited[m][n] = 0;
  779. }
  780. }
  781. for(n=0; n<143; n++) {
  782. Q[n].i = -1;
  783. Q[n].j = -1;
  784. }
  785. Q[0] = init;
  786. visited[Q[0].i][Q[0].j] = 1;
  787. int q = 143, y = 0, x = 1, c = 0;
  788.  
  789. //BEGIN THE LOOP
  790. while (q > 0 && y <= x) {
  791. int i, j;
  792. i = Q[y].i + 1;
  793. j = Q[y].j;
  794. if (i>=0 && j>=0 && i<11 && j<13) {
  795. if(!visited[i][j]) {
  796. visited[i][j] = 1;
  797. parent[i][j] = Q[y];
  798. if (strcmp(tab[i][j].str1, P2) == 0) {
  799. iS = i;
  800. jS = j;
  801. return;
  802. }
  803. Q[x].i = i;
  804. Q[x].j = j;
  805. x++;
  806. }
  807. }
  808. i = Q[y].i;
  809. j = Q[y].j + 1;
  810. if (i>=0 && j>=0 && i<11 && j<13) {
  811. if(!visited[i][j]) {
  812. visited[i][j] = 1;
  813. parent[i][j] = Q[y];
  814. if (strcmp(tab[i][j].str1, P2) == 0) {
  815. iS = i;
  816. jS = j;
  817. return;
  818. }
  819. Q[x].i = i;
  820. Q[x].j = j;
  821. x++;
  822. }
  823. }
  824. i = Q[y].i - 1;
  825. j = Q[y].j;
  826. if (i>=0 && j>=0 && i<11 && j<13) {
  827. if(!visited[i][j]) {
  828. visited[i][j] = 1;
  829. parent[i][j] = Q[y];
  830. if (strcmp(tab[i][j].str1, P2) == 0) {
  831. iS = i;
  832. jS = j;
  833. return;
  834. }
  835. Q[x].i = i;
  836. Q[x].j = j;
  837. x++;
  838. }
  839. }
  840. i = Q[y].i;
  841. j = Q[y].j - 1;
  842. if (i>=0 && j>=0 && i<11 && j<13) {
  843. if(!visited[i][j]) {
  844. visited[i][j] = 1;
  845. parent[i][j] = Q[y];
  846. if (strcmp(tab[i][j].str1, P2) == 0) {
  847. iS = i;
  848. jS = j;
  849. return;
  850. }
  851. Q[x].i = i;
  852. Q[x].j = j;
  853. x++;
  854. }
  855. }
  856. q--;
  857. y++;
  858. }
  859. }
  860.  
  861. int getP2DistB(pos init) {
  862. P2FSB(init);
  863. if (iS == -1 && jS == -1) return 0;
  864. int i = iS;
  865. int j = jS;
  866. //percorre as casas visitadas em ordem reversa, começa do objetivo
  867. //e vai vendo qual a casa pai até encontrar o início (posição P1)
  868. while(parent[i][j].i != -1) {
  869. distP2B++;
  870. if (parent[i][j].i == init.i && parent[i][j].j == init.j) {
  871. return convDir(init, i, j); //retorna a direção a ser andada
  872. }
  873. int tempi = i;
  874. int tempj = j;
  875. i = parent[i][j].i;
  876. j = parent[tempi][tempj].j;
  877. }
  878. }
  879.  
  880. //SALVA POSIÇÃO DE ULTIMA BOMBA COLOCADA
  881. void uBomb() {
  882. FILE *matrix;
  883. matrix = fopen ("DDPuBomb.txt", "w");
  884. if (matrix == NULL) return;
  885. fprintf (matrix, "%d %d", curP1.i, curP1.j);
  886. fclose (matrix);
  887. }
  888.  
  889. //PEGA POSIÇÃO DE ULTIMA BOMBA COLOCADA
  890. void getuBomb () {
  891. FILE *matrix;
  892. matrix = fopen ("DDPuBomb.txt", "r");
  893. if (matrix == NULL) return;
  894. fscanf(matrix, "%d %d", &uBombI, &uBombJ);
  895. fclose (matrix);
  896. }
  897.  
  898. int main(int argc, char *argv[]) { //a assinatura da funcao principal deve ser dessa forma
  899. FILE* file;
  900. file = fopen("debugging.txt", "a");
  901. //convercao dos identificadores
  902. id = atoi(argv[1]); //identificador do Jogador
  903. ident = atoi(argv[2]); //identificador da partida
  904.  
  905. srand(time(NULL));
  906.  
  907. leitura();
  908. lerMapAnt();
  909.  
  910. if(id==1)strcpy(P1,"P1");
  911. else strcpy(P1,"P2");
  912.  
  913. cur_pos(); // o parametro a ser passado depende se o jogador atual e 1 o 2
  914. bonusCount(); //deve ser executado antes de se jogar
  915. getuBomb(); //pega posição de ultima bomba jogada
  916. wexpmap(rexpmap); //mapa de explosão
  917. strcpy(tab[curP1.i][curP1.j].str2, B1); //coloca uma bomba para simular
  918. wexpmap(simulexpmap); //gera mapa de simulação
  919. strcpy(tab[curP1.i][curP1.j].str2, "--"); //retira a bomba colocada
  920. salvarSimulExpMap();
  921. int bombs = getBombCount();
  922. int bombsMAX = 1 + p1b; //numero máximo atual de bombas
  923. int bonusDir = 666;
  924. if (safe(curP1.i, curP1.j)) bonusDir = getBonusDist(curP1); //pega direção para o bonus e ao mesmo tempo a distância até ele
  925. int P2Dir = 666;
  926. int P2DirB = 666;
  927. if (safe(curP1.i, curP1.j)) {
  928. P2Dir = getP2Dist(curP1); //pega direção para o inimigo e ao mesmo tempo a distância até ele
  929. P2DirB = getP2DistB(curP1);
  930. }
  931. int i, j, cont=0;
  932. if (!safe(curP1.i, curP1.j)) { //se estiver em perigo, anda na direção do lugar seguro mais próximo
  933. printf("%d 0 %d 0\n",ident, bestDir(curP1, "--"));
  934. fprintf(file, "fugiu exp [%d]\n", bonusDist);
  935. } else if (!bombs && safe(curP1.i, curP1.j) && bonusDist > 0 && bonusDist < 8) { //se estiver perto de bonus, anda uma direção segura até lá
  936. printf("%d 0 %d 0\n",ident, bonusDir);
  937. fprintf(file, "foi atras de bonus [%d]\n", bonusDist);
  938. } else if (!bombs && distP2 > 2 && safe(dx[P2Dir]+curP1.i, dy[P2Dir]+curP1.j)) { //se estiver longe do inimigo, anda em sua direção
  939. printf("%d 0 %d 0\n",ident, P2Dir);
  940. fprintf(file, "andando ateh o inimigo | distP2 -> [%d] | P2Dir -> [%d]\n", distP2, P2Dir);
  941. } else if (!bombs && distP2 > 0 && (curP1.i != uBombI) && (curP1.j != uBombJ)) { //se não tiver bomba e inimigo em mapa de simulação, solta
  942. //!safesim(curP2.i, curP2.j)
  943. printf("%d 1 0 0\n",ident);
  944. //bestSimDir(curP1, "--")
  945. fprintf(file, "soltou bomba perto do inimigo | distP2 -> [%d] | P2Dir -> [%d]\n", distP2, P2Dir);
  946. bombs++;
  947. bombCount(bombs);
  948. uBomb();
  949. } else if (!bombs && pertoDestr()) { //se não tiver bomba e estiver perto de um destrutível, solta
  950. printf("%d 1 0 0\n",ident);
  951. fprintf(file, "soltou bomba [%d]\n", bonusDist);
  952. bombs++;
  953. bombCount(bombs);
  954. uBomb();
  955. } else if (safe(curP1.i, curP1.j) && bombs>0) { //se tiver bomba e estiver num lugar seguro, explode
  956. printf("%d 0 0 1\n",ident);
  957. fprintf(file, "explodiu bomba [%d]\n", bonusDist);
  958. bombs--;
  959. bombCount(bombs);
  960. } else if (!bombs && safe(dx[P2DirB]+curP1.i, dy[P2DirB]+curP1.j)) { //caso nada, anda em direção à P2 mesmo com bloqueio
  961. printf("%d 0 %d 0\n",ident, P2DirB);
  962. fprintf(file, "andou pro mato com P2FSB - %d [%d]\n", cont, P2DirB);
  963. } else { //caso nada de nada, anda aleatóriamente
  964. do {
  965. i = 1 + (rand() % 2) + (rand() % 2) + (rand() % 2);
  966. j = i;
  967. cont++;
  968. if (cont>50) break;
  969. } while(!safe(dx[i]+curP1.i, dy[j]+curP1.j));
  970. printf("%d 0 %d 0\n",ident, i);
  971. fprintf(file, "fez nada - %d [%d]\n", cont, bonusDist);
  972. }
  973. salvarMapAnt(); //deve ser salvo no final da rodada
  974. fclose(file);
  975.  
  976. return 0;
  977. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement