-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolitaire.cpp
More file actions
267 lines (233 loc) · 8.09 KB
/
Copy pathSolitaire.cpp
File metadata and controls
267 lines (233 loc) · 8.09 KB
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#include "Solitaire.h"
namespace tp
{
/**
* \fn Solitaire::Solitaire()
*/
Solitaire::Solitaire()
{
_init();
}
/**
* \fn Solitaire::Solitaire(const Solitaire& p_cp) throw(std::bad_alloc)
*/
Solitaire::Solitaire(const Solitaire& p_cp) throw(std::bad_alloc)
{
m_talon = p_cp.m_talon;
for (int i = 0 ; i < 7; i++)
m_colonnes[i] = p_cp.m_colonnes[i];
for (int i = 0 ; i < 4; i++)
m_piles[i] = p_cp.m_piles[i];
}
/**
* \brief Destructeur
*
* \fn Solitaire::~Solitaire()
*/
Solitaire::~Solitaire()
{
}
//Surcharge d'opérateurs
/**
* \fn const Solitaire::Solitaire& operator = (const Solitaire& p_cp) throw (std::bad_alloc)
*/
const Solitaire& Solitaire::operator = (const Solitaire& p_cp) throw (std::bad_alloc)
{
m_talon = p_cp.m_talon;
for (int i = 0 ; i < 7; i++)
m_colonnes[i] = p_cp.m_colonnes[i];
for (int i = 0 ; i < 4; i++)
m_piles[i] = p_cp.m_piles[i];
return (*this);
}
/**
* \brief Surcharge de l'opérateur <<
*
* \fn std::ostream& Solitaire::operator << (std::ostream& p_f, const Solitaire& p_l)
*/
std::ostream& operator << (std::ostream& p_f, const Solitaire& p_l)
{
(void)p_l;
p_f << p_l.reqEtatJeu();
return (p_f);
}
/**
* \fn void Solitaire::avancerTalon()
*/
void Solitaire::avancerTalon()
{
if (m_talon.taille() > 1)
{
m_talon.enfiler(m_talon.defiler());
}
}
/**
* \fn void Solitaire::deplacerColonneAColonne( int p_colonneSource, int p_colonneDestination, int p_nbCartes) throw (std::runtime_error)
*
* \param[in] p_colonneSource le numéro de la colonne source.
* \param[in] p_colonneDestination le numéro de la colonne de destination.
* \param[in] p_nbCartes le nombre de cartes à déplacer
*/
void Solitaire::deplacerColonneAColonne( int p_colonneSource, int p_colonneDestination, int p_nbCartes) throw (std::runtime_error)
{
if (p_colonneSource < 0 && p_colonneSource > 6)
throw std::runtime_error("deplacerColonneAColonne() : choix colonne source incorrect");
if (p_colonneDestination < 0 && p_colonneDestination > 6)
throw std::runtime_error("deplacerColonneAColonne() : choix colonne destination incorrect");
m_colonnes[p_colonneSource].deplacePaquet(m_colonnes[p_colonneDestination], p_nbCartes);
}
/**
* \fn void Solitaire::deplacerTalonAColonne ( int p_colonneDestination ) throw (std::runtime_error)
*
* \param[in] p_colonneDestination le numéro de la colonne de destination.
*/
void Solitaire::deplacerTalonAColonne ( int p_colonneDestination ) throw (std::runtime_error)
{
if (p_colonneDestination < 0 && p_colonneDestination > 6)
throw std::runtime_error("deplacerTalonAColonne() : choix colonne incorrect");
if (m_talon.taille() == 0)
throw std::runtime_error("deplacerTalonAColonne() : erreur talon");
m_colonnes[p_colonneDestination].ajoute(m_talon.premier());
m_talon.defiler();
}
/**
* \fn void Solitaire::deplacerTalonAPile ( int p_pileDestination ) throw (std::runtime_error)
*
* \param[in] p_pileDestination le numéro de la pile de destination
*/
void Solitaire::deplacerTalonAPile ( int p_pileDestination ) throw (std::runtime_error)
{
if (p_pileDestination < 0 && p_pileDestination > 6)
throw std::runtime_error("deplacerTalonAPile() : choix pile incorrect");
if (m_talon.taille() == 0)
throw std::runtime_error("deplacerTalonAColonne() : erreur talon");
if (
(m_piles[p_pileDestination].taille() == 0 && m_talon.premier().reqValeur() == AS) ||
(m_piles[p_pileDestination].taille() > 0 && m_piles[p_pileDestination].premier().estSuivante(m_talon.premier()) )
){
m_piles[p_pileDestination].empiler(m_talon.premier());
m_talon.defiler();
}
else
throw std::runtime_error("deplacerTalonAPile() : valeur ou sorte de carte incorrect");
}
/**
* \fn void Solitaire::deplacerColonneAPile ( int p_colonneSource, int p_pileDestination) throw (std::runtime_error)
*
* \param[in] p_pileDestination le numéro de la pile de destination
* \param[in] p_colonneSource le numéro de la colonne source.
*/
void Solitaire::deplacerColonneAPile ( int p_colonneSource, int p_pileDestination) throw (std::runtime_error)
{
if (p_pileDestination < 0 || p_pileDestination > 3)
throw std::runtime_error("deplacerColonneAPile() : choix de pile incorrect");
if (p_colonneSource < 0 || p_colonneSource > 6)
throw std::runtime_error("deplacerColonneAPile() : choix de colonne incorrect");
if ((m_piles[p_pileDestination].taille() == 0 && m_colonnes[p_colonneSource].reqLesCartes().element(1).reqValeur() == AS) ||
(m_piles[p_pileDestination].taille() > 0 && m_piles[p_pileDestination].premier().estSuivante(m_colonnes[p_colonneSource].reqLesCartes().element(1))))
{
m_piles[p_pileDestination].empiler(m_colonnes[p_colonneSource].reqLesCartes().element(1));
m_colonnes[p_colonneSource].supprimerDerniereCarte();
}
else
throw std::runtime_error("deplacerColonneAPile() : deplacement incorrect");
}
/**
* \fn bool Solitaire::verifieGagne ( ) const
* \return VRAI ou FAUX selon que l'état du jeu indique que le joueur a gagné la partie, (si les sept colonnes sont vides et que le talon est vide).
*/
bool Solitaire::verifieGagne ( ) const
{
for (int i = 0; i < 4; ++i)
if (m_piles[i].taille() < 14)
return false;
return true;
}
/**
* \fn std::string Solitaire::reqEtatJeu ( ) const
* \return l'état du jeu sous la forme d'un objet string formaté exactement de la même façon que la démonstration.
*/
std::string Solitaire::reqEtatJeu ( ) const
{
std::stringstream etatJeu;
etatJeu << "Talon: ";
if (m_talon.taille() > 0)
etatJeu << m_talon.premier();
else
etatJeu << "X";
etatJeu << "\t\t\t\t";
etatJeu << "Piles";
for (int i = 0; i < 4; ++i)
{
etatJeu << " ";
if (m_piles[i].taille() > 0)
etatJeu << m_piles[i].premier();
else
etatJeu << "X";
}
etatJeu << std::endl << std::endl;
for (int i = 0; i < 7 ; ++i)
etatJeu << "Col." << i << ": " << m_colonnes[i] << std::endl;
return etatJeu.str();
}
/**
* \fn void Solitaire::_init()
*/
void Solitaire::_init()
{
try {
_initTalon();
_initColonnes();
} catch (std::exception & e)
{
std::cout << e.what() << std::endl;
throw;
}
}
/**
* \fn void Solitaire::_copie(const Solitaire& p_cp)
*/
void Solitaire::_copie(const Solitaire& p_cp)
{
}
/**
* \fn void Solitaire::_detruire()
*/
void Solitaire::_detruire()
{
}
/**
* \fn void Solitaire::_initTalon()
*/
void Solitaire::_initTalon()
{
std::vector<Carte> v;
std::srand ( unsigned ( std::time(0) ) );
for (int i = 0 ; i < 4 ; i++)
{
for (int j = 0 ; j < 13 ; j++)
{
Carte nouvelleCarte((Sorte)i, (Valeur)j);
v.push_back(nouvelleCarte);
}
}
std::random_shuffle ( v.begin(), v.end() );
for (std::vector<Carte>::iterator it = v.begin(); it < v.end() ; it++)
m_talon.enfiler(*it);
}
/**
* \fn void Solitaire::_initColonnes()
*/
void Solitaire::_initColonnes()
{
for (int i = 0 ; i < 7 ; i++)
{
Liste<Carte> colonne;
for (int j = 0 ; j <= i; j++)
{
colonne.ajouter(m_talon.defiler(), 1);
}
m_colonnes[i].initColonneCartes(colonne);
}
}
}