Projet Darts  0.2
BTS SNIR LaSalle Avignon 2020
solution.cpp
Aller à la documentation de ce fichier.
1 #include "solution.h"
2 
20 Solution::Solution(QObject *parent) : QObject(parent), solution("")
21 {
22  qDebug() << Q_FUNC_INFO;
23 }
24 
31 {
32  qDebug() << Q_FUNC_INFO;
33 }
34 
35 
43 {
44  qDebug() << Q_FUNC_INFO << "Score = " << score << " : " << solution;
45  emit solutionTrouver("↣ " + QString::number(score) + " ➤ " + solution + " ↢");
46 }
47 
56 bool Solution::aTriple(int points, const int score)
57 {
58  if((score / (points*3)) >= 1)
59  {
60  return true;
61  }
62  return false;
63 }
64 
74 bool Solution::rechercherTriple(int &score, QString &combinaison, int start=20)
75 {
76  bool trouve = false;
77  for(int i=start; i>1 && !trouve; i--)
78  {
79  if(aTriple(i, score))
80  {
81  combinaison = "T" + QString::number(i);
82  score -= (i*3);
83  trouve = true;
84  }
85  }
86  return trouve;
87 }
88 
97 bool Solution::aDouble(int points, const int score)
98 {
99  if((score / (points*2)) >= 1)
100  {
101  return true;
102  }
103  return false;
104 }
105 
114 bool Solution::rechercherDouble(int &score, QString &combinaison)
115 {
116  bool trouve = false;
117  // Remarque : le 2 est plus facile que le D1 !
118  for(int i=BULL; i>1 && !trouve; i--)
119  {
120  // cibles inexistantes ?
121  if(i > 20)
122  continue;
123  if(aDouble(i, score))
124  {
125  combinaison = "D" + QString::number(i);
126  score -= (i*2);
127  trouve = true;
128  }
129  }
130  return trouve;
131 }
132 
141 bool Solution::aSimple(int points, const int score)
142 {
143  if((score / points) >= 1)
144  return true;
145  return false;
146 }
147 
156 bool Solution::rechercherSimple(int &score, QString &combinaison)
157 {
158  bool trouve = false;
159 
160  for(int i=BULL; i>0 && !trouve; i--)
161  {
162  // cibles inexistantes ?
163  if(i > 20)
164  continue;
165  if(aSimple(i, score))
166  {
167  combinaison = QString::number(i);
168  score -= i;
169  trouve = true;
170  }
171  }
172  return trouve;
173 }
174 
183 bool Solution::estDouble(int points, const int score)
184 {
185  if(score == (points*2))
186  return true;
187  return false;
188 }
189 
198 bool Solution::extraireDouble(int &score, int cible)
199 {
200  if(aDouble(cible, score))
201  {
202  score -= (cible*2);
203  return true;
204  }
205  return false;
206 }
207 
217 bool Solution::rechercher(int score, int nbFlechettes, bool still)
218 {
219  QString combinaison;
220  int s = score;
221  int n = RECHERCHE_TRIPLE;
222  int start = 20;
223  bool trouve = false;
224 
225  solution.clear();
226  for(int flechettes=nbFlechettes; flechettes>0 && score>0;)
227  {
228  if(n == RECHERCHE_TRIPLE)
229  {
230  trouve = rechercherTriple(score, combinaison, start);
231  if(!trouve)
232  {
233  n = RECHERCHE_DOUBLE;
234  }
235  }
236  else if(n == RECHERCHE_DOUBLE)
237  {
238  trouve = rechercherDouble(score, combinaison);
239  if(!trouve)
240  {
241  n = RECHERCHE_SIMPLE;
242  }
243  }
244  else if(n == RECHERCHE_SIMPLE)
245  {
246  trouve = rechercherSimple(score, combinaison);
247  }
248 
249  if(trouve)
250  {
251  solution += " " + combinaison;
252  flechettes--;
253  if(score == 0)
254  {
255  return true;
256  }
257  }
258 
259  // pas trouvé ?
260  if(flechettes == 0 && score != 0)
261  {
262  if(still)
263  return true;
264  // on recommence
265  solution.clear();
266  score = s;
267  flechettes = nbFlechettes;
268  n++; // on change de tactique
269  }
270 
271  if(n == RECHERCHE_FINIE)
272  {
273  start--; // on essaye un autre triple
274  // on recommence
275  solution.clear();
276  n = RECHERCHE_TRIPLE;
277  score = s;
278  flechettes = nbFlechettes;
279  }
280 
281  if(start == 1)
282  {
283  return false;
284  }
285  }
286  return false;
287 }
288 
296 void Solution::trouverSolution(int scoreJoueur, int flechettes)
297 {
298  int score = 0;
299  bool trouve = false;
300  int nbFlechettes = 0;
301  solution = "";
302 
303  trouve = false;
304  for(int i=BULL; i>0 && !trouve; i--)
305  {
306  // cibles inexistantes ?
307  if(i > 20)
308  continue;
309  score = scoreJoueur; // <- le score à determiner
310  nbFlechettes = flechettes; // <- le nombre de fléchettes
311  if(extraireDouble(score, i))
312  {
313  nbFlechettes--;
314  if(rechercher(score, nbFlechettes) || score == 0)
315  {
316  solution += " D" + QString::number(i) + "*";
317  transmettreSolution(scoreJoueur);
318  trouve = true;
319  break;
320  }
321  }
322  }
323  if(!trouve)
324  {
325  rechercher(score, nbFlechettes+1, true);
326  //transmettreSolution(scoreJoueur); //activer pour avoir l'aide tout le temps même quand on ne peut pas finir
327  }
328 }
void trouverSolution(int s, int flechettes)
Méthode qui trouve la meilleure solution.
Definition: solution.cpp:296
#define RECHERCHE_SIMPLE
Definition: solution.h:22
QString solution
contient la solution pour finir la partie
Definition: solution.h:39
Déclaration de la classe solution (Module Ecran-DARTS)
void transmettreSolution(int score)
Méthode qui émet un signal pour que l&#39;Ihm affiche la solution trouver.
Definition: solution.cpp:42
void solutionTrouver(QString solution)
bool aTriple(int points, const int score)
Test si la triple choisie et possible.
Definition: solution.cpp:56
bool rechercher(int score, int nbFlechettes, bool still=false)
Méthode qui recherche la meilleure combinaison pour finir.
Definition: solution.cpp:217
~Solution()
Destructeur de la classe Solution.
Definition: solution.cpp:30
bool aSimple(int points, const int score)
Test si le simple choisi et possible.
Definition: solution.cpp:141
bool rechercherDouble(int &score, QString &combinaison)
Méthode qui recherche le meilleur double pour la solution.
Definition: solution.cpp:114
#define RECHERCHE_DOUBLE
Definition: solution.h:21
Solution(QObject *parent=nullptr)
Constructeur de la classe Solution.
Definition: solution.cpp:20
bool rechercherSimple(int &score, QString &combinaison)
Méthode qui recherche le meilleur simple pour la solution.
Definition: solution.cpp:156
#define RECHERCHE_TRIPLE
Definition: solution.h:20
#define BULL
Definition: darts.h:22
bool rechercherTriple(int &score, QString &combinaison, int start)
Méthode qui recherche le meilleur triple pour la solution.
Definition: solution.cpp:74
bool estDouble(int points, const int score)
Méthode qui teste si les points son double.
Definition: solution.cpp:183
bool extraireDouble(int &score, int cible)
Méthode qui cherche le double pour finir la partie.
Definition: solution.cpp:198
bool aDouble(int points, const int score)
Test si le double choisie et possible.
Definition: solution.cpp:97
La classe QObject est la classe de base de tous les objets Qt. Elle permet à ces objets Qt de dispose...
#define RECHERCHE_FINIE
Definition: solution.h:23