-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMove.cpp
More file actions
203 lines (166 loc) · 4.47 KB
/
Move.cpp
File metadata and controls
203 lines (166 loc) · 4.47 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
//
// Move class family from Jamie's solution
//
#include "Move.h"
#include "Util.h"
#include "Exceptions.h"
#include "Board.h"
#include "rang.h"
#include "Tile.h"
#include <sstream>
#include <iostream>
#include <iomanip>
Move* Move::parseMove(std::string moveString, Player &p)
{
makeUppercase(moveString);
std::istringstream moveStream(moveString);
// pull the first word out of the command
std::string commandWord;
moveStream >> commandWord;
if(commandWord == "PASS")
{
return new PassMove(&p);
}
else if(commandWord == "EXCHANGE")
{
// pull tile string out of command
std::string tilesToExchange;
moveStream >> tilesToExchange;
return new ExchangeMove(tilesToExchange, &p);
}
else if(commandWord == "PLACE")
{
// pull place args out of command
char direction;
size_t row;
size_t col;
std::string tilesToExchange;
moveStream >> direction >> row >> col >> tilesToExchange;
if(!(direction == '-' || direction == '|'))
{
throw MoveException("MALFORMED");
}
return new PlaceMove(col, row, direction == '-', tilesToExchange, &p);
}
else
{
// unknown word
throw MoveException("UNKNOWN");
}
}
Move::Move(Player *player):
_player(player)
{
}
Move::~Move()
{
}
PassMove::PassMove(Player *player):
Move(player)
{
}
void PassMove::execute(Board & board, Bag & bag, Dictionary & dictionary)
{
// silence unused parameter warnings
(void)board;
(void)bag;
(void)dictionary;
// do nothing at all
}
ExchangeMove::ExchangeMove(std::string tileString, Player *p):
Move(p),
_tiles(p->takeTiles(tileString, false))
{
if(_tiles.empty())
{
throw MoveException("EMPTY");
}
}
void ExchangeMove::execute(Board & board, Bag & bag, Dictionary & dictionary)
{
// silence unused parameter warnings
(void)board;
(void)dictionary;
std::cout << std::endl << "Throwing back these tiles:" << rang::fg::green << rang::style::bold;
for(std::vector<Tile *>::const_iterator sequencesIter = _tiles.cbegin(); sequencesIter != _tiles.end(); ++sequencesIter)
{
std::cout << ' ' << static_cast<char>(std::toupper((*sequencesIter)->getLetter()));
}
std::cout << rang::style::reset << std::endl;
bag.addTiles(_tiles);
}
PlaceMove::PlaceMove(size_t x, size_t y, bool horizontal, std::string tileString, Player *p):
Move(p),
_x(x),
_y(y),
_horizontal(horizontal),
_tiles(p->takeTiles(tileString, true))
{
if(_tiles.empty())
{
throw MoveException("EMPTY");
}
}
size_t PlaceMove::x() const
{
return _x;
}
size_t PlaceMove::y() const
{
return _y;
}
bool PlaceMove::isHorizontal() const
{
return _horizontal;
}
std::vector<Tile *> const &PlaceMove::tileVector() const
{
return _tiles;
}
void PlaceMove::execute(Board & board, Bag & bag, Dictionary & dictionary)
{
// silence unused parameter warnings
(void)bag;
try
{
// first, try this move on the board and see what words are generated
// (may generate exceptions)
std::vector<std::pair<std::string, unsigned int>> sequencesFormed = board.getPlaceMoveResults(*this);
// check that each sequence of tiles in in fact a word
for(size_t sequenceIndex = 0; sequenceIndex < sequencesFormed.size(); ++sequenceIndex)
{
if(!dictionary.isLegalWord(sequencesFormed[sequenceIndex].first))
{
throw MoveException("INVALIDWORD:" + sequencesFormed[sequenceIndex].first);
}
}
// print the words that would be created, and add up scores
unsigned int totalScore = 0;
std::cout << std::endl << "This move creates these words:";
for(size_t sequenceIndex = 0; sequenceIndex < sequencesFormed.size(); ++sequenceIndex)
{
std::cout << ' ' << rang::fg::cyan << rang::style::bold << sequencesFormed[sequenceIndex].first << rang::style::reset;
std::cout << " (" << rang::fgB::magenta << rang::style::bold << std::setw(0) << sequencesFormed[sequenceIndex].second << rang::style::reset << ')';
totalScore += sequencesFormed[sequenceIndex].second;
}
std::cout << std::endl;
// now that we're sure it's going to work,
// we can execute the move!
board.executePlaceMove(*this);
_player->addPoints(totalScore);
if(_tiles.size() == _player->getMaxTiles())
{
std::cout << "You used all tiles in your hand, earning a 50-point bonus!" << std::endl;
_player->addPoints(50);
}
}
catch(MoveException & moveException)
{
// the move execution failed, but the tiles in _tiles have still been removed from
// the player's hand. So, we need to put them back.
// note to self: add unit tests for this mistake
_player->addTiles(_tiles);
// now, you can get on with your exception
throw moveException;
}
}