-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRobot.cpp
209 lines (188 loc) · 3.65 KB
/
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
/*
Implementation des méthodes de Robot.h
auteurs : Molinengo/Soumille
date : Nov 2015
*/
#include "Robot.h"
#include <iostream>
using namespace std;
//constructeur
Robot::Robot(): _position(0,0),
_direction("N"),
_objetSaisi(),
_plot() {
_etat = EtatRobot::getEtatInitial();
}
/*
Le robot saisit l'objet passé en paramètre
*/
void Robot::saisir(Objet O){
try {
_etat = _etat->saisir();
_objetSaisi = O;
_ordre = "saisir";
notifier();
} catch(EtatRobot::UnavailableFunction) {
throw EtatRobot::UnavailableFunction();
}
}
/*
Le robot se déplace à la position passée en paramètre
*/
void Robot::avancer(int x, int y){
try {
_etat = _etat->avancer();
_position.setx(x);
_position.sety(y);
_ordre = "avancer";
notifier();
} catch(EtatRobot::UnavailableFunction) {
throw EtatRobot::UnavailableFunction();
}
}
/*
Le robot tourne dans la direction passée en paramètre
si ce n'est pas la même
*/
void Robot::tourner(string direction){
try {
if(direction != _direction){
_etat = _etat->tourner();
_direction = direction;
_plot = Plot();
}
_ordre = "tourner";
notifier();
} catch(EtatRobot::UnavailableFunction) {
throw EtatRobot::UnavailableFunction();
}
}
/*
Le robot pose l'objet qu'il détient
*/
void Robot::poser(){
try {
_etat = _etat->poser();
_objetSaisi = Objet();
_ordre = "poser";
notifier();
} catch(EtatRobot::UnavailableFunction) {
throw EtatRobot::UnavailableFunction();
}
}
/*
Retourne le poids de l'objet porté par le robot
0 si il ne porte rien
*/
int Robot::peser(){
try {
_etat = _etat->peser();
_ordre = "peser";
notifier();
return _objetSaisi.getPoids();
} catch(EtatRobot::UnavailableFunction) {
throw EtatRobot::UnavailableFunction();
}
return 0;
}
/*
Retourne la hauteur du plot devant le robot
0 si il n'y en a pas
*/
int Robot::evaluerPlot(){
try {
_etat = _etat->evaluerPlot();
_ordre = "evaluerPlot";
notifier();
return _plot.getHauteur();
} catch(EtatRobot::UnavailableFunction) {
throw EtatRobot::UnavailableFunction();
}
return 0;
}
/*
Fige le robot
*/
void Robot::figer(){
try {
_etat = _etat->figer();
_ordre = "figer";
notifier();
} catch(EtatRobot::UnavailableFunction) {
throw EtatRobot::UnavailableFunction();
}
}
/*
Associe le plot au robot
*/
void Robot::rencontrerPlot(Plot p){
try {
_etat = _etat->rencontrerPlot();
_plot = p;
_ordre = "rencontrerPlot";
notifier();
} catch(EtatRobot::UnavailableFunction) {
throw EtatRobot::UnavailableFunction();
}
}
/*
Fonction qui remet le robot en route
*/
void Robot::repartir(){
try {
_etat = _etat->repartir();
_ordre = "repartir";
notifier();
} catch(EtatRobot::UnavailableFunction) {
throw EtatRobot::UnavailableFunction();
}
}
/*
Ajoute un afficheur qui se mettra à jour
lors des changements sur le robot
*/
void Robot::attacher(Afficheur * a){
_afficheurs.push_back(a);
}
/*
Retire un afficheur du vector
*/
void Robot::detacher(Afficheur * a){
for(auto i = _afficheurs.begin() ; i != _afficheurs.end()
&& _afficheurs.size() != 0 ; ++i){
if(*i == a)
_afficheurs.erase(i);
}
}
/*
Indique aux afficheurs de faire une mise a jour
*/
void Robot::notifier(){
for(auto i = _afficheurs.begin() ; i != _afficheurs.end() ; ++i){
(*i)->afficher(this);
}
}
//retourne la position
Position Robot::getPosition(){
return _position;
}
//retourne la direction
string Robot::getDirection(){
return _direction;
}
//retourne l'objet
Objet Robot::getObjet(){
return _objetSaisi;
}
//retourne le plot
Plot Robot::getPlot(){
return _plot;
}
//retourne l'état
EtatRobot * Robot::getEtat(){
return _etat;
}
//retourne l'ordre
string Robot::getOrdre(){
return _ordre;
}