Last active
August 29, 2015 14:14
-
-
Save mauricioaniche/905820c66a4f8e1a3a1c to your computer and use it in GitHub Desktop.
Foge Foge - Coding like a pro - Parte 4
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 "fogefoge.h" | |
#include "mapa.h" | |
MAPA m; | |
POSICAO heroi; | |
int acabou() { | |
return 0; | |
} | |
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(!ehvalida(&m, proximox, proximoy)) | |
return; | |
if(!ehvazia(&m, proximox, proximoy)) | |
return; | |
andanomapa(&m, heroi.x, heroi.y, proximox, proximoy); | |
heroi.x = proximox; | |
heroi.y = proximoy; | |
} | |
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) { | |
if(ehvalida(&m, i, j+1) && ehvazia(&m, i, j+1)) { | |
andanomapa(&m, i, j, i, j+1); | |
} | |
} | |
} | |
} | |
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]); | |
} | |
} | |
void 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; | |
} | |
} | |
} | |
} | |
int ehvalida(MAPA* m, int x, int y) { | |
if(x >= m->linhas) | |
return 0; | |
if(y >= m->colunas) | |
return 0; | |
return 1; | |
} | |
int ehvazia(MAPA* m, int x, int y) { | |
return m->matriz[x][y] == VAZIO; | |
} | |
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; | |
void encontramapa(MAPA* m, POSICAO* p, char c); | |
int ehvalida(MAPA* m, int x, int y); | |
int ehvazia(MAPA* m, int x, int y); | |
void andanomapa(MAPA* m, int xorigem, int yorigem, | |
int xdestino, int ydestino); | |
void copiamapa(MAPA* destino, MAPA* origem); |
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 | |
|--------| | |
|.F.|....| | |
|..-|.@..| | |
|..F.....| | |
|--------| |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment