-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.cpp
More file actions
188 lines (145 loc) · 3.67 KB
/
Copy pathPlayer.cpp
File metadata and controls
188 lines (145 loc) · 3.67 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
//
// Player class implementation from Jamie's solution
//
#include "Player.h"
#include "Exceptions.h"
#include "Util.h"
Player::Player(std::string const & name, size_t maxTiles):
_name(name),
_maxTiles(maxTiles),
_points(0),
_hand()
{
}
Player::~Player()
{
// delete any remaining tiles
for(std::multimap<char, Tile *>::const_iterator tileIter = _hand.cbegin(); tileIter != _hand.cend(); ++tileIter)
{
delete tileIter->second;
}
}
void Player::addPoints(unsigned int points)
{
_points += points;
}
void Player::subtractPoints(unsigned int pointsToSubtract)
{
if(pointsToSubtract > _points)
{
_points = 0;
}
else
{
_points -= pointsToSubtract;
}
}
std::string Player::getName() const
{
return _name;
}
unsigned int Player::getPoints() const
{
return _points;
}
size_t Player::getNumTiles() const
{
return _hand.size();
}
size_t Player::getMaxTiles() const
{
return _maxTiles;
}
unsigned int Player::remainingPoints() const
{
unsigned int remainingPoints = 0;
for(std::multimap<char, Tile *>::const_iterator tileIter = _hand.cbegin(); tileIter != _hand.cend(); ++tileIter)
{
remainingPoints += tileIter->second->getPoints();
}
return remainingPoints;
}
std::set<Tile*> Player::getHandTiles() const
{
std::set<Tile *> tileSet;
// add elements from multimap to set
for(std::multimap<char, Tile *>::const_iterator tileIter = _hand.cbegin(); tileIter != _hand.cend(); ++tileIter)
{
tileSet.insert(tileIter->second);
}
return tileSet;
}
bool Player::hasTiles(std::string const & move, bool resolveBlanks) const
{
std::string uppercaseMove = move;
makeUppercase(uppercaseMove);
for(size_t charIndex = 0; charIndex < uppercaseMove.length(); ++charIndex)
{
if(uppercaseMove[charIndex] == '?' && resolveBlanks)
{
ConstHandSearchResult blankSearchResult = _hand.equal_range('?');
if(blankSearchResult.first == blankSearchResult.second)
{
return false;
}
// skip next character
++charIndex;
}
else
{
ConstHandSearchResult charSearchResult = _hand.equal_range(uppercaseMove[charIndex]);
if(charSearchResult.first == charSearchResult.second)
{
return false;
}
}
}
return true;
}
std::vector<Tile *> Player::takeTiles(std::string const & move, bool resolveBlanks)
{
// before we make any modifications, check that it is actually possible
// to make the given move.
std::string uppercaseMove = move;
makeUppercase(uppercaseMove);
if(!hasTiles(uppercaseMove, resolveBlanks))
{
throw MoveException("WRONGTILES");
}
std::vector<Tile *> tilesRequested;
for(size_t charIndex = 0; charIndex < uppercaseMove.length(); ++charIndex)
{
if(uppercaseMove[charIndex] == '?' && resolveBlanks)
{
HandSearchResult blankSearchResult = _hand.equal_range('?');
++charIndex;
// remove the blank tile and assign a letter to it
Tile * blankTile = blankSearchResult.first->second;
_hand.erase(blankSearchResult.first);
blankTile->useAs(uppercaseMove[charIndex]);
tilesRequested.push_back(blankTile);
}
else
{
HandSearchResult charSearchResult = _hand.equal_range(uppercaseMove[charIndex]);
Tile * charTile = charSearchResult.first->second;
_hand.erase(charSearchResult.first);
tilesRequested.push_back(charTile);
}
}
return tilesRequested;
}
void Player::addTiles(std::vector<Tile *> const & tilesToAdd)
{
if(_hand.size() + tilesToAdd.size() > _maxTiles)
{
throw MoveException("TILELIMIT");
}
for(std::vector<Tile *>::const_iterator newTileIter = tilesToAdd.begin(); newTileIter != tilesToAdd.end(); ++newTileIter)
{
Tile * newTile = *newTileIter;
_hand.emplace(std::toupper(newTile->getLetter()), newTile);
}
}
void Player::performCPUMove(Board & board, Bag & bag, Dictionary & dictionary) {
}