-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfogefoge.c
More file actions
155 lines (115 loc) · 2.63 KB
/
Copy pathfogefoge.c
File metadata and controls
155 lines (115 loc) · 2.63 KB
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
#include <stdio.h>
#include <stdlib.h>
#include "time.h"
#include "fogefoge.h"
#include "mapa.h"
#include "ui.h"
MAPA m;
POSICAO heroi;
int tempilula = 0;
int acabou() {
POSICAO pos;
int perdeu = !encontramapa(&m, &pos, HEROI);
int ganhou = !encontramapa(&m, &pos, FANTASMA);
return ganhou || perdeu;
}
int ehdirecao(char direcao) {
return
direcao == ESQUERDA ||
direcao == CIMA ||
direcao == BAIXO ||
direcao == DIREITA;
}
void move(char direcao) {
int proximox = heroi.x;
int proximoy = heroi.y;
switch(direcao) {
case ESQUERDA:
proximoy--;
break;
case CIMA:
proximox--;
break;
case BAIXO:
proximox++;
break;
case DIREITA:
proximoy++;
break;
}
if(!podeandar(&m, HEROI, proximox, proximoy))
return;
if(ehpersonagem(&m, PILULA, proximox, proximoy)) {
tempilula=1;
}
andanomapa(&m, heroi.x, heroi.y, proximox, proximoy);
heroi.x = proximox;
heroi.y = proximoy;
}
int praondefantasmavai(int xatual, int yatual,
int* xdestino, int* ydestino) {
int opcoes[4][2] = {
{ xatual , yatual+1 },
{ xatual+1 , yatual },
{ xatual , yatual-1 },
{ xatual-1 , yatual }
};
srand(time(0));
for(int i = 0; i < 10; i++) {
int posicao = rand() % 4;
if(podeandar(&m, FANTASMA, opcoes[posicao][0], opcoes[posicao][1])) {
*xdestino = opcoes[posicao][0];
*ydestino = opcoes[posicao][1];
return 1;
}
}
return 0;
}
void fantasmas() {
MAPA copia;
copiamapa(&copia, &m);
for(int i = 0; i < copia.linhas; i++) {
for(int j = 0; j < copia.colunas; j++) {
if(copia.matriz[i][j] == FANTASMA) {
int xdestino;
int ydestino;
int encontrou = praondefantasmavai(i, j, &xdestino, &ydestino);
if(encontrou) {
andanomapa(&m, i, j, xdestino, ydestino);
}
}
}
}
liberamapa(&copia);
}
void explodepilula2(int x, int y, int somax, int somay, int qtd) {
if(qtd == 0) return;
int novox = x+somax;
int novoy = y+somay;
if(!ehvalida(&m, novox, novoy)) return;
if(ehparede(&m, novox, novoy)) return;
m.matriz[novox][novoy] = VAZIO;
explodepilula2(novox, novoy, somax, somay, qtd-1);
}
void explodepilula() {
if(!tempilula) return;
explodepilula2(heroi.x, heroi.y, 0, 1, 3);
explodepilula2(heroi.x, heroi.y, 0, -1, 3);
explodepilula2(heroi.x, heroi.y, 1, 0, 3);
explodepilula2(heroi.x, heroi.y, -1, 0, 3);
tempilula = 0;
}
int main() {
lemapa(&m);
encontramapa(&m, &heroi, HEROI);
do {
printf("Pilula: %s\n", (tempilula ? "SIM" : "NAO"));
imprimemapa(&m);
char comando;
scanf(" %c", &comando);
if(ehdirecao(comando)) move(comando);
if(comando == BOMBA) explodepilula();
fantasmas();
} while (!acabou());
liberamapa(&m);
}