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
|
#include "JDV0-bis.h"
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <exception>
using namespace std;
void JeuDeLaVie::nettoie(std::string &s){
size_t pos=s.find_first_of("#");
s=s.substr(0,pos);
int beg=0,end=s.size()-1;
while(beg<end+1 && (s[beg]==' ' || s[beg]=='\t' )) beg++;
while(end>beg-1 && (s[end]==' ' || s[end]=='\t' )) end--;
s=s.substr(beg,end-beg+1);
}
bool JeuDeLaVie::findCleVal(std::string &s, std::string &s1,std::string &s2){
nettoie(s);
if (s==string("")) return false;
size_t pos=s.find_first_of(":");
if (pos==string::npos) {
cerr << "Le fichier est mal formé" << endl;
terminate();
}
s1=s.substr(0,pos);
s2=s.substr(pos+1);
nettoie(s1);
nettoie(s2);
//cerr<<"Found cle/val -> "<< s1<< " and "<<s2<<endl;
return true;
}
void JeuDeLaVie::TraiteOption(const string &cle, const string &valeur, size_t num_ligne){
if (cle == "Dimension") {
POP.setDimension(atoi(valeur.c_str()));
POP.reset();
}
if (cle == "Probability") {
POP.setProbability(atof(valeur.c_str()));
POP.reset();
}
if (cle == "Cell") {
size_t x, y;
size_t pos = valeur.find_first_of("x, ");
if (pos == string::npos || valeur[pos] == '\0') {
cerr << "Le fichier est mal formé. Vérifiez la syntaxe de la ligne "<< num_ligne << endl;
}
else {
x = atoi(valeur.substr(0, pos).c_str());
y = atoi(valeur.substr(pos).c_str());
POP.birth(x,y);
}
}
}
void JeuDeLaVie::loadConfig(std::string file){
ifstream input(file.c_str());
string cle, valeur;
size_t num_ligne=0;
if (!input.is_open()) {
cerr << "Le fichier " << file << " n'a pas pu être ouvert." << endl;
terminate();
}
string line;
while (!input.eof()) {
getline(input,line);
//cout<<"reading line ("<<num_ligne<<") -> "<<line<<endl;
if (!input.eof()) {
if (findCleVal(line,cle,valeur))
TraiteOption(cle,valeur,num_ligne);
}
num_ligne++;
}
input.close();
}
void JeuDeLaVie::run(size_t n) {
POP.print();
for(size_t i=0;i<n;i++){
POP=POP.next();
POP.print();
}
}
JeuDeLaVie::JeuDeLaVie() : POP(8,0.25) {
opts.addOption(Option(HELP_ID, "--help", "-h",
"Affiche l'aide du programme", Option::AUCUN));
opts.addOption(Option(VERSION_ID, "--version", "-v",
"Affiche la version du programme", Option::AUCUN));
opts.addOption(Option(DIMENSION_ID, "--dimension", "-N",
"Initialise une matrice carrée de la dimension spécifiée",
Option::ENTIER));
opts.addOption(Option(PROBABILITY_ID, "--probability", "-p",
"Probabilité d'une cellule d'être en vie au démarrage",
Option::REEL));
opts.addOption(Option(CONFIG_ID, "--config", "-f",
"Charge la configuration initiale du jeu "
"à partir du fichier passé en paramètre",
Option::CHAINE));
}
void JeuDeLaVie::parseOptions(int argc, char** argv){
bool opt_error = false;
int i = 1;
while (!opt_error && i < argc) {
int x = opts.getOptionId(argv[i]);
switch (x) {
case HELP_ID:
cout << "usage " << argv[0] << " [Options]" << endl;
opts.print(); return;
case VERSION_ID:
cout << "Programme " << argv[0] << " version 0.0.0" << endl;
return;
case DIMENSION_ID:
POP.setDimension(atoi(argv[++i]));
break;
case PROBABILITY_ID:
POP.setProbability(atof(argv[++i]));
break;
case CONFIG_ID:
loadConfig(argv[++i]);
break;
default:
if (opts.optionHasArgument(argv[i])) {
cout << "L'option " << argv[i] << " a été trouvée.";
cout << " Elle attend un argument de type "
<< Type2String(opts.optionArgumentType(argv[i]));
cout << " => " << (++i < argc ? argv[i] : "Erreur");
} else {
cout << "Cette option n'a pas été reconnue.";
opt_error = true;
}
cout << endl;
}
i++;
}
if (opt_error) {
cout << "Usage : " << argv[0] << " [Options]" << endl;
opts.print();
terminate();
}
}
|