Advertisement
Guest User

Untitled

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