-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
196 lines (169 loc) · 6.71 KB
/
main.cpp
File metadata and controls
196 lines (169 loc) · 6.71 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include <iostream>
#include <thread>
#include <chrono>
#include <ctime>
#include <vector>
#include "POO/Robot.h"
#include "POO/Porte.h"
#include "POO/Cle.h"
#include "POA/AgentReactif.h"
#include "POA/AgentCognitif.h"
using namespace std;
struct Position {
int x;
int y;
};
const int WIDTH = 10;
const int HEIGHT = 6;
void afficherGrille(const Position &robot, const Position &cle, const Position &porte, bool clePrise, EtatPorte etatPorte)
{
for(int y = 0; y < HEIGHT; y++) {
for(int x = 0; x < WIDTH; x++) {
if(robot.x == x && robot.y == y) cout << "R ";
else if(!clePrise && cle.x == x && cle.y == y) cout << "K ";
else if(porte.x == x && porte.y == y) cout << (etatPorte == PO ? "O " : "D ");
else cout << ". ";
}
cout << endl;
}
cout << endl;
this_thread::sleep_for(chrono::milliseconds(300));
}
Position positionAleatoire() {
Position p;
p.x = rand() % WIDTH;
p.y = rand() % HEIGHT;
return p;
}
Position positionPorteExtremite() {
Position p;
if(rand() % 2 == 0)
{
p.x = (rand() % 2 == 0) ? 0 : WIDTH-1;
p.y = rand() % HEIGHT;
} else
{
p.x = rand() % WIDTH;
p.y = (rand() % 2 == 0) ? 0 : HEIGHT-1;
}
return p;
}
int main() {
srand(static_cast<unsigned int>(time(0)));
int choix;
cout << "Choisissez le type d'agent :\n1 - Agent Reactif (exploration)\n2 - Agent Cognitif (planifie)\nVotre choix : ";
cin >> choix;
if(choix == 1) {
Position posRobot = positionAleatoire();
Robot jr(posRobot.x,posRobot.y);
Position posPorte = positionPorteExtremite();
Porte porte(PFC, posPorte.x, posPorte.y);
Position posCle = positionAleatoire();
Cle cle(posCle.x, posCle.y);
while(posCle.x == posRobot.x && posCle.y == posRobot.y)
posCle = positionAleatoire();
while((posPorte.x == posRobot.x && posPorte.y == posRobot.y) ||
(posPorte.x == posCle.x && posPorte.y == posCle.y))
posPorte = positionPorteExtremite();
AgentReactif reactif;
cout << "\n=== Simulation Agent Réactif (exploration complète) ===\n";
bool visite[HEIGHT][WIDTH] = {false};
visite[posRobot.y][posRobot.x] = true;
vector<Position> directions = {{-1,0},{1,0},{0,-1},{0,1}};
bool phaseCle = true;
bool phasePorte = false;
while(true) {
afficherGrille(posRobot, posCle, posPorte, cle.estPrise(), porte.getEtat());
if(phaseCle && posRobot.x == posCle.x && posRobot.y == posCle.y) {
reactif.agir(jr, porte, cle);
phaseCle = false;
phasePorte = true;
for(int i=0;i<HEIGHT;i++)
for(int j=0;j<WIDTH;j++)
visite[i][j] = false;
visite[posRobot.y][posRobot.x] = true;
}
if(phasePorte && cle.estPrise() && porte.getEtat() != PO) {
if((posRobot.x == posPorte.x && abs(posRobot.y - posPorte.y) == 1) ||
(posRobot.y == posPorte.y && abs(posRobot.x - posPorte.x) == 1)) {
porte.ouvrir();
}
}
if(phasePorte && posRobot.x == posPorte.x && posRobot.y == posPorte.y && cle.estPrise() && porte.getEtat() == PO) {
afficherGrille(posRobot, posCle, posPorte, cle.estPrise(), porte.getEtat());
cout << "Le robot réactif a atteint la porte avec la clé !\n";
break;
}
vector<Position> moves;
for(auto &d : directions)
{
int nx = posRobot.x + d.x;
int ny = posRobot.y + d.y;
if(nx >=0 && nx < WIDTH && ny >=0 && ny < HEIGHT && !visite[ny][nx])
{
if(phaseCle)
{
moves.push_back({nx,ny});
} else if(phasePorte)
{
if(nx == 0 || nx == WIDTH-1 || ny == 0 || ny == HEIGHT-1)
moves.push_back({nx,ny});
}
}
}
if(moves.empty())
{
for(int i=0;i<HEIGHT;i++)
for(int j=0;j<WIDTH;j++)
visite[i][j] = false;
moves.push_back(posRobot);
}
int idx = rand() % moves.size();
posRobot = moves[idx];
visite[posRobot.y][posRobot.x] = true;
}
} else if(choix == 2) {
Position posRobot = positionAleatoire();
Robot jr2(posRobot.x, posRobot.y);
Position posPorte = positionPorteExtremite();
Porte porte2(PFC, posPorte.x, posPorte.y);
Position posCle = positionAleatoire();
Cle cle2(posCle.x, posCle.y);
Position posRobotC = positionAleatoire();
Position posCleC = positionAleatoire();
Position posPorteC = positionPorteExtremite();
while(posCleC.x == posRobotC.x && posCleC.y == posRobotC.y)
posCleC = positionAleatoire();
while((posPorteC.x == posRobotC.x && posPorteC.y == posRobotC.y) ||
(posPorteC.x == posCleC.x && posPorteC.y == posCleC.y))
posPorteC = positionPorteExtremite();
AgentCognitif cognitif;
cout << "\n=== Simulation Agent Cognitif (planifié) ===\n";
while(true) {
afficherGrille(posRobotC, posCleC, posPorteC, cle2.estPrise(), porte2.getEtat());
if(!cle2.estPrise()) {
if(posRobotC.x < posCleC.x) posRobotC.x++;
else if(posRobotC.x > posCleC.x) posRobotC.x--;
else if(posRobotC.y < posCleC.y) posRobotC.y++;
else if(posRobotC.y > posCleC.y) posRobotC.y--;
if(posRobotC.x == posCleC.x && posRobotC.y == posCleC.y)
cognitif.planifier(jr2, porte2, cle2);
} else {
if(posRobotC.x < posPorteC.x) posRobotC.x++;
else if(posRobotC.x > posPorteC.x) posRobotC.x--;
else if(posRobotC.y < posPorteC.y) posRobotC.y++;
else if(posRobotC.y > posPorteC.y) posRobotC.y--;
if(posRobotC.x == posPorteC.x && posRobotC.y == posPorteC.y-1)
porte2.ouvrir();
}
if(posRobotC.x == posPorteC.x && posRobotC.y == posPorteC.y && cle2.estPrise() && porte2.getEtat() == PO) {
afficherGrille(posRobotC, posCleC, posPorteC, cle2.estPrise(), porte2.getEtat());
cout << "Le robot cognitif a atteint la porte avec la clé !\n";
break;
}
}
} else {
cout << "Choix invalide !" << endl;
}
return 0;
}