-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmapa.c
More file actions
103 lines (79 loc) · 2.17 KB
/
Copy pathmapa.c
File metadata and controls
103 lines (79 loc) · 2.17 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
102
103
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mapa.h"
void lerMapa(MAPA* m) {
FILE* f;
f = fopen("mapa.txt", "r");
if (f == 0) {
printf("Erro na leitura do arquivo\n");
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* person, char c) {
for (int i = 0; i < m->linhas; i++) {
for (int j = 0; j < m->colunas; j++) {
if (m->matriz[i][j] == c) {
person->x = i;
person->y = j;
return 1;
}
}
}
return 0;
}
int podeAndar(MAPA* m, char person, int x, int y) {
return
ehValida(m, x, y) &&
!ehParede(m, x, y) &&
!ehPerson(m, person, 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 ehPerson(MAPA* m, char person, int x, int y) {
return m->matriz[x][y] == person;
}
int ehParede(MAPA* m, int x, int y) {
return
m->matriz[x][y] == WALL_VERTICAL ||
m->matriz[x][y] == WALL_HORIZONTAL;
}
int ehPilula(MAPA* m, int x, int y) {
return m->matriz[x][y] == PILULA;
}
void moveMapa(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] = EMPTY;
}