Repository navigation
Expand file tree
/
Copy pathmaze.cpp
More file actions
172 lines (157 loc) · 3.75 KB
/
Copy pathmaze.cpp
File metadata and controls
172 lines (157 loc) · 3.75 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
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
#include "maze.h"
#include "agent.h"
#include <iostream>
#include <fstream> // lire fichier
#include <string>
#include <cstdlib> //rand()
using namespace std;
//constructeur du labyrinthe
maze::maze(string maze_name)
{
ifstream f(maze_name); //ouvre fichier
for (int i = 0; i < 21 * 12; i++)
{
f >> _map[i]; //lit le fichier case par case
}
f.close(); // ferme le fichier
// On place l'agent au debut
resetMaze();
}
// Met à jour l'affichage avec les emojis
void maze::refresh_map_char()
{
for (int i = 0; i < 21 * 12; i++)
{
if (_map[i] == 0) _map_char[i] = "🟦"; //sol
if (_map[i] == 1) _map_char[i] = "🌊";//mur
if (_map[i] == 2) _map_char[i] = "🟨"; // entrée
if (_map[i] == 3) _map_char[i] = "🟩"; //sortie
}
// Mettre l'agent par-dessus
_map_char[agent1.getVal()] = "⛵"; //agent
}
// Decide quelle direction prendre
char maze::move_random_agent()
{
char options[4]; //tableau des ptions
int count = 0; //compteur pour savoir combien d'options
// on regarde les capteurs et on ajoute les deplacements possibles
if (agent1.getNord() != -1) {
options[count] = 'N';
count++;
}
if (agent1.getSud() != -1) {
options[count] = 'S';
count++;
}
if (agent1.getEst() != -1) {
options[count] = 'E';
count++;
}
if (agent1.getOuest() != -1) {
options[count] = 'O';
count++;
}
// si count == 0, on est bloqué
if (count == 0)
{
return ' '; // on bouge pas
}
// on choisit un nombre au hasard
int index = rand() % count;
return options[index];
}
// Bouge l'agent
void maze::update_agent(char dir)
{
if (dir == 'N')
{
agent1.setVal(agent1.getVal() - 21); //nord
}
if (dir == 'S')
{
agent1.setVal(agent1.getVal() + 21); // sud
}
if (dir == 'E')
{
agent1.setVal(agent1.getVal() + 1); // est
}
if (dir == 'O')
{
agent1.setVal(agent1.getVal() - 1); // ouest
}
updateCapteurs();
}
// verifie si l'agent est sur la case 3
bool maze::isFinished()
{
if (_map[agent1.getVal()] == 3) // à la sortir
{
return true;
}
return false;
}
// Remet l'agent au debut
void maze::resetMaze()
{
// trouver la case 2
int startPos = 0;
for (int i = 0; i < 21 * 12; i++)
{
if (_map[i] == 2)
{
startPos = i;
break; // on a trouvé
}
}
agent1.setVal(startPos); // place l'agent
updateCapteurs(); // regle les capteurs
}
// La fonction privee pour checker les murs
void maze::updateCapteurs()
{
int pos = agent1.getVal();
int y = pos / 21; // ligne actuelle
int x = pos % 21; // colonne actuelle
// Check Nord
if (y == 0 || _map[pos - 21] == 1) // si on est au bord (y=0) ou sil ya un mur
{
agent1.setNord(-1); // bloqué
} else {
agent1.setNord(0); // libre
}
// Check Sud
if (y == 11 || _map[pos + 21] == 1) // si on est au bord (y=11)
{
agent1.setSud(-1);
} else {
agent1.setSud(0);
}
// Check Est
if (x == 20 || _map[pos + 1] == 1) // si on est au bord (x=20)
{
agent1.setEst(-1);
} else {
agent1.setEst(0);
}
// Check Ouest
if (x == 0 || _map[pos - 1] == 1) // si on est au bord (x=0)
{
agent1.setOuest(-1);
} else {
agent1.setOuest(0);
}
}
// Surcharge de << pour afficher le labyrinthe
ostream &operator<<(ostream &os, const maze &m)
{
for (int i = 0; i < 21 * 12; i++)
{
// on utilise modulo 21 pr nouvelle ligne
if (i % 21 == 0 && i != 0)
os << endl;
os << m._map_char[i];
}
os << endl;
return os;
}