Skip to content

Instantly share code, notes, and snippets.

@mauricioaniche
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mauricioaniche/effe539d9ad12d2312e5 to your computer and use it in GitHub Desktop.
Save mauricioaniche/effe539d9ad12d2312e5 to your computer and use it in GitHub Desktop.
Foge Foge - Struct - Parte 3
#include <stdio.h>
#include <stdlib.h>
#include "fogefoge.h"
#include "mapa.h"
MAPA m;
int acabou() {
return 0;
}
void move(char direcao) {
int x;
int y;
for(int i = 0; i < m.linhas; i++) {
for(int j = 0; j < m.colunas; j++) {
if(m.matriz[i][j] == '@') {
x = i;
y = j;
break;
}
}
}
switch(direcao) {
case 'a':
m.matriz[x][y-1] = '@';
break;
case 'w':
m.matriz[x-1][y] = '@';
break;
case 's':
m.matriz[x+1][y] = '@';
break;
case 'd':
m.matriz[x][y+1] = '@';
break;
}
m.matriz[x][y] = '.';
}
int main() {
lemapa(&m);
do {
imprimemapa(&m);
char comando;
scanf(" %c", &comando);
move(comando);
} while (!acabou());
liberamapa(&m);
}
int acabou();
void move(char direcao);
#include <stdio.h>
#include <stdlib.h>
#include "mapa.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 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]);
}
}
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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment