Last active
May 5, 2026 18:18
-
-
Save mauricioaniche/43d5cf4b0489a7d839d4 to your computer and use it in GitHub Desktop.
Foge Foge - Coding like a pro - Parte 2
This file contains hidden or 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 == 'a' || | |
| direcao == 'w' || | |
| direcao == 's' || | |
| direcao == 'd'; | |
| } | |
| void move(char direcao) { | |
| if(!ehDirecao(direcao)) | |
| return; | |
| int proximox = heroi.x; | |
| int proximoy = heroi.y; | |
| switch(direcao) { | |
| case 'a': | |
| proximoy--; | |
| break; | |
| case 'w': | |
| proximox--; | |
| break; | |
| case 's': | |
| proximox++; | |
| break; | |
| case 'd': | |
| 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; | |
| } | |
| int main() { | |
| lemapa(&m); | |
| encontramapa(&m, &heroi, '@'); | |
| do { | |
| imprimemapa(&m); | |
| char comando; | |
| scanf(" %c", &comando); | |
| move(comando); | |
| } while (!acabou()); | |
| liberamapa(&m); | |
| } |
This file contains hidden or 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
| int acabou(); | |
| void move(char direcao); | |
| int ehDirecao(char direcao); |
This file contains hidden or 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" | |
| 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]); | |
| } | |
| } | |
| 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] == '.'; | |
| } | |
| 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] = '.'; | |
| } |
This file contains hidden or 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
| 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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment