-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain_Robot_.cpp
229 lines (184 loc) · 6.84 KB
/
Main_Robot_.cpp
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include <iostream>
#include <vector>
#include <string>
class Objet {
public:
int positionX;
int positionY;
Objet() : positionX(0), positionY(0) {}
Objet(int posX, int posY) : positionX(posX), positionY(posY) {}
virtual void deplacer(int newX, int newY) = 0;
virtual void afficher() = 0;
};
class Tableau;
class Robot : public Objet {
public:
int batterie;
Robot(int posX, int posY) : Objet(posX, posY), batterie(100) {}
void deplacer(int newX, int newY) override {
int distanceX = std::abs(newX - positionX);
int distanceY = std::abs(newY - positionY);
if (distanceX == 1 && distanceY == 0 || distanceX == 0 && distanceY == 1) {
if (batterie > 0) {
positionX = newX;
positionY = newY;
batterie--;
} else {
std::cout << "Batterie épuisée, le robot ne peut pas se déplacer\n";
}
} else {
std::cout << "Déplacement impossible\n";
}
}
virtual void afficher() {
std::cout << "R \t";
}
};
class Arbre : public Objet {
public:
std::string statut;
Arbre(int posX, int posY, std::string statut) : Objet(posX, posY), statut(statut) {}
void deplacer(int newX, int newY) override {
// Les arbres ne peuvent pas être déplacés
}
void afficher() override {
if (statut == "pouce")
std::cout << "a \t";
else if (statut == "grand")
std::cout << "A \t";
}
};
class RobotPlanteur : public Robot {
public:
RobotPlanteur(int posX, int posY) : Robot(posX, posY) {}
void planterArbrePouce(Tableau &tableau);
void afficher() override {
std::cout << "RP \t";
}
};
class RobotArroseur : public Robot {
public:
RobotArroseur(int posX, int posY) : Robot(posX, posY) {}
void arroserArbre(Arbre &arbre) {
arbre.statut = "grand";
std::cout << "Le robot arrose l'arbre\n\n";
}
void afficher() override {
std::cout << "RA \t";
}
};
class Tableau {
private:
static const int numRows = 5;
static const int numCols = 5;
std::vector<std::vector<Objet*>> tableau;
bool isValidPosition(int posX, int posY) const {
return posX >= 0 && posX < numRows && posY >= 0 && posY < numCols;
}
public:
Tableau() {
tableau.resize(numRows, std::vector<Objet*>(numCols, nullptr));
}
void afficherTableau() {
for (int i = 0; i < numRows; ++i) {
for (int j = 0; j < numCols; ++j) {
if (tableau[i][j] != nullptr) {
tableau[i][j]->afficher();
} else {
std::cout << "- \t"; // Case vide
}
}
std::cout << std::endl;
}
}
int getNumCols() const {
return numCols;
}
void placerObjet(Objet* objet, int posX, int posY) {
if (isValidPosition(posX, posY)) {
tableau[posX][posY] = objet;
objet->positionX = posX;
objet->positionY = posY;
}
}
void deplacerObjet(Objet* objet, int newX, int newY) {
if (objet != nullptr && isValidPosition(newX, newY)) {
int distanceX = std::abs(newX - objet->positionX);
int distanceY = std::abs(newY - objet->positionY);
if (distanceX == 1 && distanceY == 0 || distanceX == 0 && distanceY == 1) {
Robot* robot = dynamic_cast<Robot*>(objet);
if (robot) {
if (robot->batterie > 0) {
tableau[objet->positionX][objet->positionY] = nullptr;
tableau[newX][newY] = objet;
objet->positionX = newX;
objet->positionY = newY;
robot->batterie--;
std::cout << "Batterie : " << robot->batterie << std::endl;
} else {
std::cout << "Batterie épuisée, le robot ne peut pas se déplacer\n";
}
} else {
tableau[objet->positionX][objet->positionY] = nullptr;
tableau[newX][newY] = objet;
objet->positionX = newX;
objet->positionY = newY;
}
} else {
std::cout << "Déplacement impossible\n";
}
}
}
};
void RobotPlanteur::planterArbrePouce(Tableau &tableau) {
int newX = positionX+1;
int newY = positionY ;
if (newY >= tableau.getNumCols()) {
std::cout << "Impossible de planter en bas, il n'y a plus de place.\n";
return;
}
Arbre *arbrePouce = new Arbre(newX, newY, "pouce");
tableau.placerObjet(arbrePouce, newX, newY);
std::cout << "Robot Planteur plante un arbre 'pouce' en bas.\n";
}
int main() {
Tableau tableau;
Robot robotB(1, 2);
Arbre arbreC(3, 4, "grand");
Arbre arbreF(1, 1, "pouce");
RobotPlanteur robotP(0, 0);
RobotArroseur robotR(2, 1);
tableau.placerObjet(&arbreF, 1, 1);
tableau.placerObjet(&robotP, 0, 0);
tableau.placerObjet(&robotR, 1, 2);
tableau.placerObjet(&arbreC, 3, 4);
tableau.afficherTableau();
std::cout << "\n________________________________________________________________________________________\n\n";
tableau.deplacerObjet(&robotR, 1, 3);
tableau.afficherTableau();
std::cout << "\n________________________________________________________________________________________\n\n";
tableau.deplacerObjet(&robotR, 2, 3);
tableau.afficherTableau();
std::cout << "\n________________________________________________________________________________________\n\n";
tableau.deplacerObjet(&robotR, 1, 3);
tableau.afficherTableau();
std::cout << "\n________________________________________________________________________________________\n\n";
tableau.deplacerObjet(&robotR, 1, 2);
tableau.afficherTableau();
std::cout << "\n________________________________________________________________________________________\n\n";
robotR.arroserArbre(arbreF);
tableau.afficherTableau();
std::cout << "\n________________________________________________________________________________________\n\n";
tableau.deplacerObjet(&robotP, 1, 0);
tableau.afficherTableau();
std::cout << "\n________________________________________________________________________________________\n\n";
tableau.deplacerObjet(&robotP, 2, 0);
tableau.afficherTableau();
std::cout << "\n________________________________________________________________________________________\n\n";
tableau.deplacerObjet(&robotP, 3, 0);
tableau.afficherTableau();
std::cout << "\n________________________________________________________________________________________\n\n";
robotP.planterArbrePouce(tableau);
tableau.afficherTableau();
return 0;
}