Last active
May 5, 2026 18:15
-
-
Save mauricioaniche/effe539d9ad12d2312e5 to your computer and use it in GitHub Desktop.
Foge Foge - Struct - Parte 3
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; | |
| 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); | |
| } |
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); |
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]); | |
| } | |
| } |
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); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment