Advertisement
Guest User

Untitled

a guest
Apr 1st, 2015
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "time.h"
  4. #include "fogefoge.h"
  5. #include "mapa.h"
  6.  
  7. MAPA m;
  8. POSICAO heroi;
  9.  
  10.  
  11. int acabou() {
  12. return 0;
  13. }
  14.  
  15. int ehdirecao(char direcao) {
  16. return
  17. direcao == ESQUERDA ||
  18. direcao == CIMA ||
  19. direcao == BAIXO ||
  20. direcao == DIREITA;
  21. }
  22.  
  23. void move(char direcao) {
  24.  
  25. if(!ehdirecao(direcao))
  26. return;
  27.  
  28. int proximox = heroi.x;
  29. int proximoy = heroi.y;
  30.  
  31. switch(direcao) {
  32. case ESQUERDA:
  33. proximoy--;
  34. break;
  35. case CIMA:
  36. proximox--;
  37. break;
  38. case BAIXO:
  39. proximox++;
  40. break;
  41. case DIREITA:
  42. proximoy++;
  43. break;
  44. }
  45.  
  46. if(!podeandar(&m, proximox, proximoy))
  47. return;
  48.  
  49. andanomapa(&m, heroi.x, heroi.y, proximox, proximoy);
  50. heroi.x = proximox;
  51. heroi.y = proximoy;
  52. }
  53.  
  54. int praondefantasmavai(int xatual, int yatual,
  55. int* xdestino, int* ydestino) {
  56.  
  57. int opcoes[4][2] = {
  58. { xatual , yatual+1 },
  59. { xatual+1 , yatual },
  60. { xatual , yatual-1 },
  61. { xatual-1 , yatual }
  62. };
  63.  
  64. srand(time(0));
  65. for(int i = 0; i < 10; i++) {
  66. int posicao = rand() % 4;
  67.  
  68. if(podeandar(&m, opcoes[posicao][0], opcoes[posicao][1])) {
  69. *xdestino = opcoes[posicao][0];
  70. *ydestino = opcoes[posicao][1];
  71. return 1;
  72. }
  73. }
  74.  
  75. return 0;
  76. }
  77.  
  78. void fantasmas() {
  79. MAPA copia;
  80.  
  81. copiamapa(&copia, &m);
  82.  
  83. for(int i = 0; i < copia.linhas; i++) {
  84. for(int j = 0; j < copia.colunas; j++) {
  85. if(copia.matriz[i][j] == FANTASMA) {
  86.  
  87. int xdestino;
  88. int ydestino;
  89.  
  90. int encontrou = praondefantasmavai(i, j, &xdestino, &ydestino);
  91.  
  92. if(encontrou) {
  93. andanomapa(&m, i, j, xdestino, ydestino);
  94. }
  95. }
  96. }
  97. }
  98.  
  99. liberamapa(&copia);
  100. }
  101.  
  102. int main() {
  103.  
  104. lemapa(&m);
  105. encontramapa(&m, &heroi, HEROI);
  106.  
  107. do {
  108. imprimemapa(&m);
  109.  
  110. char comando;
  111. scanf(" %c", &comando);
  112.  
  113. move(comando);
  114. fantasmas();
  115.  
  116. } while (!acabou());
  117.  
  118. liberamapa(&m);
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement