-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlabirinto.c
181 lines (154 loc) · 4.3 KB
/
labirinto.c
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
//Headers includes
#include "main.h"
#include "structs.h"
#include "ghosts.h"
#include "auxiliars.h"
#include "labirinto.h"
///Reads the maze archive
int readLab(char lab[HEIGHT][WIDTH], int labOption)
{
char str[101];
int i=0, j=0;
FILE *arq;
char url[30];
switch(labOption)
{
case 1:
strcpy(url, "data/labirinto.txt");
break;
case 2:
strcpy(url, "data/labirintoOneDot.txt");
break;
case 3:
strcpy(url, "data/labirintoProfessor.txt");
break;
case 4:
strcpy(url, "data/labirintoNoLab.txt");
break;
default:
strcpy(url, "nolab.txt");
break;
}
//Pointer to the file read
arq = fopen(url, "r");
//Tests the file opening
if(arq == NULL)
{
printf("ERROR: The lab couldn't be opened\n");
fclose(arq);
return 1;
}
//Reads each of the file's line
while(fgets(str, 101, arq) != NULL || i<30)
{
//If the line it isn't a blank line
if(str[0] != '\n')
{
//Add's to the lab the string
for(j=0; j<100; j++)
{
lab[i][j] = str[j];
}
i++;
}
}
fclose(arq);
return 0;
}
///Starts and load all the structs
int startLab(char lab[HEIGHT][WIDTH], int *qtPacDots, pacmanInfo *pacman, ghostsInfo *fantasmas, int labOption)
{
int i, j; //Matrix counter
//Variables inicializations
*qtPacDots=0;
fantasmas->quant=0;
if(readLab(lab, labOption)) //Reads the lab
{
return 1;
}
//Prints the lab
gotoXY(1,1);
for(i=0; i<HEIGHT; i++)
{
for(j=0; j<WIDTH; j++)
{
switch(lab[i][j])
{
case 'c':
case 'C':
//Pacman's origin (x,y)
pacman->origin.x = j+1;
pacman->origin.y = i+1;
//Pacman's actual (x,y)
pacman->pos.x = j+1;
pacman->pos.y = i+1;
lab[i][j] = ' ';
break;
case 'w':
case 'W': // Ghosts
if(fantasmas->quant < MAX_GHOSTS)
{
//Ghost's origin (x,y)
fantasmas->unid[fantasmas->quant].origin.x=j;
fantasmas->unid[fantasmas->quant].origin.y=i;
//Ghost's actual (x,y)
fantasmas->unid[fantasmas->quant].pos.x = j;
fantasmas->unid[fantasmas->quant].pos.y = i;
fantasmas->unid[fantasmas->quant].alive = 1; // Ghost live
fantasmas->unid[fantasmas->quant].key = 'W';
(fantasmas->quant)++;
}
lab[i][j] = ' ';
break;
case 'o': //Pastils
(*qtPacDots)++;
break;
}
}
}
return 0;
}
///Prints the lab in the screen
int showLab(char lab[HEIGHT][WIDTH], pacmanInfo *pacman, ghostsInfo *fantasmas)
{
int i, j;
int q_fantasmas=fantasmas->quant;
//Lab's Print/reprint
gotoXY(1,1);
for(i=0; i<HEIGHT; i++)
{
for(j=0; j<WIDTH; j++)
{
switch(lab[i][j])
{
case '#':
textcolor(CINZA_CLARO);
printf("%c", PAREDE);
break;
case 'o':
textcolor(AMARELO);
printf("%c", PACDOT);
break;
case '*':
textcolor(VERMELHO);
printf("%c", POWERPELLET);
break;
default:
printf(" ");
break;
}
}
//printf("\n"); //Must uncomment in some PC's
}
//Sets the position of the pacman
pacman->pos.x=pacman->origin.x;
pacman->pos.y=pacman->origin.y;
//Sets the initial position of the ghost
for(i=0; i<q_fantasmas; i++)
{
fantasmas->unid[i].pos.x = fantasmas->unid[i].origin.x;
fantasmas->unid[i].pos.y = fantasmas->unid[i].origin.y;
fantasmas->unid[i].alive = 1; // Sets ghosts lives
}
return 0;
}