3 |
* Authors: Antoine Fraboulet <antoine.fraboulet@free.fr> |
* Authors: Antoine Fraboulet <antoine.fraboulet@free.fr> |
4 |
* Olivier Teuliere <ipkiss@via.ecp.fr> |
* Olivier Teuliere <ipkiss@via.ecp.fr> |
5 |
* |
* |
6 |
|
<<<<<<< pldrack.cpp |
7 |
|
======= |
8 |
* $Id$ |
* $Id$ |
9 |
* |
* |
10 |
|
>>>>>>> 1.5 |
11 |
* This program is free software; you can redistribute it and/or modify |
* This program is free software; you can redistribute it and/or modify |
12 |
* it under the terms of the GNU General Public License as published by |
* it under the terms of the GNU General Public License as published by |
13 |
* the Free Software Foundation; either version 2 of the License, or |
* the Free Software Foundation; either version 2 of the License, or |
23 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
24 |
*****************************************************************************/ |
*****************************************************************************/ |
25 |
|
|
26 |
|
/* $Id$ */ |
27 |
|
|
28 |
|
/** |
29 |
|
* \file pldrack.cpp |
30 |
|
* \brief Improved Rack class with old and new tiles |
31 |
|
* \author Antoine Fraboulet & Olivier Teuliere |
32 |
|
* \date 2002 - 2005 |
33 |
|
*/ |
34 |
|
|
35 |
|
#include <string> |
36 |
#include "rack.h" |
#include "rack.h" |
37 |
#include "pldrack.h" |
#include "pldrack.h" |
38 |
|
|
39 |
#include "debug.h" |
#include "debug.h" |
40 |
|
|
41 |
|
const int PlayedRack::RACK_SIZE = 7; |
42 |
|
|
43 |
|
PlayedRack::PlayedRack() |
44 |
|
{ |
45 |
|
reset(); |
46 |
|
reject = false; |
47 |
|
} |
48 |
|
|
49 |
|
int PlayedRack::replace(Bag& bag, set_rack_mode mode, const std::string manual_rack) |
50 |
|
{ |
51 |
|
int res = 0; |
52 |
|
int tiles_to_take = 0; |
53 |
|
std::vector<Tile> tiles; |
54 |
|
|
55 |
|
// 1: we replace the tiles from the old rack in the bag |
56 |
|
// kept tiles are kept and placed in the current rack |
57 |
|
switch (mode) |
58 |
|
{ |
59 |
|
case RACK_NEW: |
60 |
|
bag.putbackTiles(m_newTiles); |
61 |
|
resetNew(); |
62 |
|
tiles_to_take = RACK_SIZE - m_oldTiles.size(); |
63 |
|
break; |
64 |
|
case RACK_ALL: |
65 |
|
if (m_oldTiles.size() > 0) |
66 |
|
reject = true; |
67 |
|
/* fall */ |
68 |
|
case RACK_MANUAL: |
69 |
|
/// TODO: set reject boolean for RACK_MANUAL |
70 |
|
bag.putbackTiles(m_oldTiles); |
71 |
|
bag.putbackTiles(m_newTiles); |
72 |
|
reset(); |
73 |
|
tiles_to_take = RACK_SIZE; |
74 |
|
break; |
75 |
|
} |
76 |
|
|
77 |
|
// 2: we take some random tiles or the proposed tiles |
78 |
|
tiles.clear(); |
79 |
|
if (mode == RACK_MANUAL) |
80 |
|
{ |
81 |
|
std::string::iterator it; |
82 |
|
std::string uLetters = manual_rack; |
83 |
|
for(it = uLetters.begin(); it != uLetters.end(); it ++) |
84 |
|
{ |
85 |
|
*it = toupper(*it); |
86 |
|
Tile t = Tile(*it); |
87 |
|
if (bag.in(t) < 1) |
88 |
|
{ |
89 |
|
res = 1; |
90 |
|
break; |
91 |
|
} |
92 |
|
bag.takeTile(t); |
93 |
|
tiles.push_back(t); |
94 |
|
} |
95 |
|
} |
96 |
|
else |
97 |
|
{ |
98 |
|
res = bag.takeRandomTiles(tiles_to_take,tiles); |
99 |
|
} |
100 |
|
|
101 |
|
// 3: we set the rack |
102 |
|
m_newTiles = tiles; |
103 |
|
return res; |
104 |
|
} |
105 |
|
|
106 |
void PlayedRack::addOld(const Tile &t) |
void PlayedRack::addOld(const Tile &t) |
107 |
{ |
{ |
115 |
} |
} |
116 |
|
|
117 |
|
|
118 |
void PlayedRack::getOldTiles(vector<Tile> &oTiles) const |
void PlayedRack::getOldTiles(std::vector<Tile> &oTiles) const |
119 |
{ |
{ |
120 |
oTiles.clear(); |
oTiles.clear(); |
121 |
for (int i = 0; i < nOld(); i++) |
for (int i = 0; i < nOld(); i++) |
123 |
} |
} |
124 |
|
|
125 |
|
|
126 |
void PlayedRack::getNewTiles(vector<Tile> &oTiles) const |
void PlayedRack::getNewTiles(std::vector<Tile> &oTiles) const |
127 |
{ |
{ |
128 |
oTiles.clear(); |
oTiles.clear(); |
129 |
for (int i = 0; i < nNew(); i++) |
for (int i = 0; i < nNew(); i++) |
131 |
} |
} |
132 |
|
|
133 |
|
|
134 |
void PlayedRack::getAllTiles(vector<Tile> &oTiles) const |
void PlayedRack::getAllTiles(std::vector<Tile> &oTiles) const |
135 |
{ |
{ |
136 |
oTiles.clear(); |
oTiles.clear(); |
137 |
for (int i = 0; i < nOld(); i++) |
for (int i = 0; i < nOld(); i++) |
156 |
|
|
157 |
void PlayedRack::getOld(Rack &oRack) const |
void PlayedRack::getOld(Rack &oRack) const |
158 |
{ |
{ |
159 |
vector<Tile>::const_iterator it; |
std::vector<Tile>::const_iterator it; |
160 |
oRack.clear(); |
oRack.clear(); |
161 |
for (it = m_oldTiles.begin(); it != m_oldTiles.end(); it++) |
for (it = m_oldTiles.begin(); it != m_oldTiles.end(); it++) |
162 |
{ |
{ |
167 |
|
|
168 |
void PlayedRack::getNew(Rack &oRack) const |
void PlayedRack::getNew(Rack &oRack) const |
169 |
{ |
{ |
170 |
vector<Tile>::const_iterator it; |
std::vector<Tile>::const_iterator it; |
171 |
oRack.clear(); |
oRack.clear(); |
172 |
for (it = m_newTiles.begin(); it != m_newTiles.end(); it++) |
for (it = m_newTiles.begin(); it != m_newTiles.end(); it++) |
173 |
{ |
{ |
178 |
|
|
179 |
void PlayedRack::getRack(Rack &oRack) const |
void PlayedRack::getRack(Rack &oRack) const |
180 |
{ |
{ |
181 |
vector<Tile>::const_iterator it; |
std::vector<Tile>::const_iterator it; |
182 |
getOld(oRack); |
getOld(oRack); |
183 |
for (it = m_newTiles.begin(); it != m_newTiles.end(); it++) |
for (it = m_newTiles.begin(); it != m_newTiles.end(); it++) |
184 |
{ |
{ |
189 |
|
|
190 |
void PlayedRack::setOld(const Rack &iRack) |
void PlayedRack::setOld(const Rack &iRack) |
191 |
{ |
{ |
192 |
list<Tile> l; |
std::list<Tile> l; |
193 |
iRack.getTiles(l); |
iRack.getTiles(l); |
194 |
|
|
195 |
m_oldTiles.clear(); |
m_oldTiles.clear(); |
196 |
list<Tile>::const_iterator it; |
std::list<Tile>::const_iterator it; |
197 |
for (it = l.begin(); it != l.end(); it++) |
for (it = l.begin(); it != l.end(); it++) |
198 |
{ |
{ |
199 |
addOld(*it); |
addOld(*it); |
203 |
|
|
204 |
void PlayedRack::setNew(const Rack &iRack) |
void PlayedRack::setNew(const Rack &iRack) |
205 |
{ |
{ |
206 |
list<Tile> l; |
std::list<Tile> l; |
207 |
iRack.getTiles(l); |
iRack.getTiles(l); |
208 |
|
|
209 |
m_newTiles.clear(); |
m_newTiles.clear(); |
210 |
list<Tile>::const_iterator it; |
std::list<Tile>::const_iterator it; |
211 |
for (it = l.begin(); it != l.end(); it++) |
for (it = l.begin(); it != l.end(); it++) |
212 |
{ |
{ |
213 |
addNew(*it); |
addNew(*it); |
215 |
} |
} |
216 |
|
|
217 |
|
|
218 |
bool PlayedRack::checkRack(int iMin) const |
bool PlayedRack::checkRack(int cMin, int vMin) const |
219 |
{ |
{ |
220 |
vector<Tile>::const_iterator it; |
std::vector<Tile>::const_iterator it; |
221 |
int v = 0; |
int v = 0; |
222 |
int c = 0; |
int c = 0; |
223 |
|
|
231 |
if (it->isVowel()) v++; |
if (it->isVowel()) v++; |
232 |
if (it->isConsonant()) c++; |
if (it->isConsonant()) c++; |
233 |
} |
} |
234 |
return (v >= iMin) && (c >= iMin); |
return (v >= vMin) && (c >= cMin); |
235 |
} |
} |
236 |
|
|
237 |
|
|
242 |
} |
} |
243 |
|
|
244 |
|
|
245 |
void PlayedRack::toString(string& s) const |
std::string PlayedRack::toString(int size, display_mode mode) const |
246 |
{ |
{ |
247 |
vector<Tile>::const_iterator it; |
std::string s(""); |
248 |
s = ""; |
std::vector<Tile>::const_iterator it; |
249 |
|
|
250 |
if (nOld() > 0) |
if (nOld() > 0) |
251 |
{ |
{ |
252 |
for (it = m_oldTiles.begin(); it != m_oldTiles.end(); it++) |
for (it = m_oldTiles.begin(); it != m_oldTiles.end(); it++) |
253 |
s += it->toChar(); |
s += it->toChar(); |
254 |
} |
} |
255 |
if (nOld() > 0 && nNew() > 0) |
|
256 |
{ |
if (mode > RACK_SIMPLE && nOld() > 0 && nNew() > 0) |
257 |
s += "+"; |
{ |
258 |
} |
s += "+"; |
259 |
|
} |
260 |
|
|
261 |
|
if (mode > RACK_EXTRA && reject) |
262 |
|
{ |
263 |
|
s += "-"; |
264 |
|
// nouveau tirage : rejet |
265 |
|
// pas après un scrabble |
266 |
|
} |
267 |
|
|
268 |
if (nNew() > 0) |
if (nNew() > 0) |
269 |
{ |
{ |
270 |
for (it = m_newTiles.begin(); it != m_newTiles.end(); it++) |
for (it = m_newTiles.begin(); it != m_newTiles.end(); it++) |
271 |
s += it->toChar(); |
s += it->toChar(); |
272 |
} |
} |
273 |
} |
|
274 |
|
if (size > 0 && (unsigned int)size > s.size()) |
275 |
|
{ |
276 |
|
s += std::string(size - s.size(), ' '); |
277 |
|
} |
278 |
|
|
279 |
|
return s; |
280 |
|
} |
281 |
|
|
282 |
|
|
283 |
|
/// Local Variables: |
284 |
|
/// mode: hs-minor |
285 |
|
/// c-basic-offset: 4 |
286 |
|
/// End: |