Repository navigation
Expand file tree
/
Copy pathmapa.c
More file actions
101 lines (78 loc) · 1.8 KB
/
Copy pathmapa.c
File metadata and controls
101 lines (78 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#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);
}
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;
}
}
}
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;
}