Last active
September 19, 2017 20:20
Foge Foge - Coding like a pro - Parte 6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include "time.h" | |
#include "fogefoge.h" | |
#include "mapa.h" | |
MAPA m; | |
POSICAO heroi; | |
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) { | |
if(!ehdirecao(direcao)) | |
return; | |
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; | |
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); | |
} | |
int main() { | |
lemapa(&m); | |
encontramapa(&m, &heroi, HEROI); | |
do { | |
imprimemapa(&m); | |
char comando; | |
scanf(" %c", &comando); | |
move(comando); | |
fantasmas(); | |
} while (!acabou()); | |
liberamapa(&m); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define CIMA 'w' | |
#define BAIXO 's' | |
#define DIREITA 'd' | |
#define ESQUERDA 'a' | |
int acabou(); | |
void move(char direcao); | |
int ehdirecao(char direcao); | |
void fantasmas(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include "mapa.h" | |
#include <string.h> | |
void lemapa(MAPA* m) { | |
FILE* f; | |
f = fopen("mapa.txt", "r"); | |
if(f == 0) { | |
printf("Erro na leitura do mapa"); | |
exit(1); | |
} | |
fscanf(f, "%d %d", &(m->linhas), &(m->colunas)); | |
alocamapa(m); | |
for(int i = 0; i < m->linhas; i++) { | |
fscanf(f, "%s", m->matriz[i]); | |
} | |
fclose(f); | |
} | |
void alocamapa(MAPA* m) { | |
m->matriz = malloc(sizeof(char*) * m->linhas); | |
for(int i = 0; i < m->linhas; i++) { | |
m->matriz[i] = malloc(sizeof(char) * m->colunas + 1); | |
} | |
} | |
void copiamapa(MAPA* destino, MAPA* origem) { | |
destino->linhas = origem->linhas; | |
destino->colunas = origem->colunas; | |
alocamapa(destino); | |
for(int i = 0; i < origem->linhas; i++) { | |
strcpy(destino->matriz[i], origem->matriz[i]); | |
} | |
} | |
void liberamapa(MAPA* m) { | |
for(int i = 0; i < m->linhas; i++) { | |
free(m->matriz[i]); | |
} | |
free(m->matriz); | |
} | |
void imprimemapa(MAPA* m) { | |
for(int i = 0; i < m->linhas; i++) { | |
printf("%s\n", m->matriz[i]); | |
} | |
} | |
int encontramapa(MAPA* m, POSICAO* p, char c) { | |
for(int i = 0; i < m->linhas; i++) { | |
for(int j = 0; j < m->colunas; j++) { | |
if(m->matriz[i][j] == c) { | |
p->x = i; | |
p->y = j; | |
return 1; | |
} | |
} | |
} | |
// não encontramos! | |
return 0; | |
} | |
int podeandar(MAPA* m, char personagem, int x, int y) { | |
return | |
ehvalida(m, x, y) && | |
!ehparede(m, x, y) && | |
!ehpersonagem(m, personagem, x, y); | |
} | |
int ehvalida(MAPA* m, int x, int y) { | |
if(x >= m->linhas) | |
return 0; | |
if(y >= m->colunas) | |
return 0; | |
return 1; | |
} | |
int ehpersonagem(MAPA* m, char personagem, int x, int y) { | |
return | |
m->matriz[x][y] == personagem; | |
} | |
int ehparede(MAPA* m, int x, int y) { | |
return | |
m->matriz[x][y] == PAREDE_VERTICAL || | |
m->matriz[x][y] == PAREDE_HORIZONTAL; | |
} | |
void andanomapa(MAPA* m, int xorigem, int yorigem, | |
int xdestino, int ydestino) { | |
char personagem = m->matriz[xorigem][yorigem]; | |
m->matriz[xdestino][ydestino] = personagem; | |
m->matriz[xorigem][yorigem] = VAZIO; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define HEROI '@' | |
#define VAZIO '.' | |
#define PAREDE_VERTICAL '|' | |
#define PAREDE_HORIZONTAL '-' | |
#define FANTASMA 'F' | |
struct mapa { | |
char** matriz; | |
int linhas; | |
int colunas; | |
}; | |
typedef struct mapa MAPA; | |
void alocamapa(MAPA* m); | |
void lemapa(MAPA* m); | |
void liberamapa(MAPA* m); | |
void imprimemapa(MAPA* m); | |
struct posicao { | |
int x; | |
int y; | |
}; | |
typedef struct posicao POSICAO; | |
int encontramapa(MAPA* m, POSICAO* p, char c); | |
int ehvalida(MAPA* m, int x, int y); | |
int ehparede(MAPA* m, int x, int y); | |
int ehpersonagem(MAPA* m, char personagem, int x, int y); | |
void andanomapa(MAPA* m, int xorigem, int yorigem, | |
int xdestino, int ydestino); | |
void copiamapa(MAPA* destino, MAPA* origem); | |
int podeandar(MAPA* m, char personagem, int x, int y); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 10 | |
|--------| | |
|FFF|....| | |
|FF-|.@..| | |
|..F.....| | |
|--------| |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment