-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLecteurCommande.cpp
70 lines (60 loc) · 1.8 KB
/
LecteurCommande.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
/*
Implementation des méthodes de LecteurCommande.h
auteurs : Molinengo/Soumille
date : Nov 2015
*/
#include "LecteurCommande.h"
#include "Position.h"
#include <algorithm>
using namespace std;
static vector<string> allCommands;
bool isNumber(string s){
for(int i = 0 ; i < s.size() ; ++i){
if(! isdigit(s[i]))
return false;
}
return true;
}
int LecteurCommande::getInt(){
string value;
cin >> value;
while(! isNumber(value) || value.size() == 0){
cout << "La valeur doit etre positive, Retapez une valeur :" << endl << "valeur = ";
cin.ignore(10000, '\n');
cin >> value;
cin.ignore(10000, '\n');
}
cout << "end" << endl;
return stoi(value);
}
string LecteurCommande::getDirection(){
string value;
cin >> value;
transform(value.begin(), value.end(), value.begin(), ::toupper);
while(value != "N" && value != "S" && value != "E" && value != "O"){
cout << "Les directions sont N, S, E ou O, Retapez la direction :" << endl << "direction = ";
cin >> value;
transform(value.begin(), value.end(), value.begin(), ::toupper);
}
return value;
}
bool isCommandInParam(vector<string> list, string nom){
return (find(list.begin(), list.end(), nom) != list.end());
}
void LecteurCommande::read(){
for(auto it = Commande::commandesDisponibles().begin(); it != Commande::commandesDisponibles().end(); ++it){
allCommands.push_back(it->first);
}
cout << "Début de la simulation (Ctrl^C pour quitter)" << endl << "> ";
string nomCommande;
while(cin >> nomCommande){
transform(nomCommande.begin(), nomCommande.end(),nomCommande.begin(), ::toupper);
if(isCommandInParam(allCommands, nomCommande)){
Commande * cmd = Commande::nouvelleCommande(nomCommande, this);
cmd->execute();
} else {
cerr << "Erreur Commande. Tapez de nouveau la commande :" << endl;
}
cout << "> ";
}
}