rp-scrabble
Simple, terminal-based Scrabble game
play.cc
Go to the documentation of this file.
1 
4 #include "tile.h"
5 #include "square.h"
6 #include "board.h"
7 #include "player.h"
8 #include "play.h"
9 
10 using namespace std;
11 
20 {
21  pointsMade = 0;
22  playMaker = p;
23  playStr = "";
24 }
25 
30 {
31  delete playMaker;
32 }
33 
37 void Play::show()
38 {
39  string wordStr = "";
40  for(vector<Tile*> word : wordsInPlay) {
41  for(Tile* t : word) {
42  wordStr.append(t->getLetterStr());
43  }
44  wordStr.append(" + ");
45  }
46  if(!wordStr.empty()) {
47  wordStr.replace(wordStr.end()-3, wordStr.end(), "");
48  }
49 
50  BOLD_WHITE_FG(" Words in play: " + wordStr + "\n");
51  BOLD_WHITE_FG(" " + to_string(pointsMade) + " points\n");
52 }
53 
54 
63 {
64  playMaker = p;
65 }
66 
90 bool Play::validate(string tileStr, Board* b, int r, int c, char dir)
91 {
92  int max = tileStr.length();
93  bool result = false;
94  Square* curr;
95 
96  if(dir == 'h') {
97  int currCol = c;
98  for(int i = 0; i < max; i++) {
99  try {
100  curr = b->getSquare(r, currCol);
101  Square* currLeft = curr->getLeft();
102  Square* currRight = curr->getRight();
103  Square* currAbove = curr->getAbove();
104  Square* currBelow = curr->getBelow();
105 
106  result =
107  ( currLeft && !currLeft->isEmpty() )
108  || ( currRight && !currRight->isEmpty() )
109  || ( currAbove && !currAbove->isEmpty() )
110  || ( currBelow && !currBelow->isEmpty() );
111 
112  if(result) {
113  playStr = tileStr;
114  break;
115  }
116  else {
117  currCol++;
118  }
119  }
120  catch(string err) {
121  throw;
122  }
123  }
124  }
125  else if(dir == 'v') {
126  int currRow = r;
127  try {
128  for(int i = 0; i < max; i++) {
129  curr = b->getSquare(currRow, c);
130  Square* currLeft = curr->getLeft();
131  Square* currRight = curr->getRight();
132  Square* currAbove = curr->getAbove();
133  Square* currBelow = curr->getBelow();
134 
135  result =
136  ( currLeft && !currLeft->isEmpty() )
137  || ( currRight && !currRight->isEmpty() )
138  || ( currAbove && !currAbove->isEmpty() )
139  || ( currBelow && !currBelow->isEmpty() );
140 
141  if(result) {
142  playStr = tileStr;
143  break;
144  }
145  else {
146  currRow++;
147  }
148  }
149  }
150  catch(string err) {
151  throw;
152  }
153  }
154  else {
155  throw(string("Invalid direction\n"));
156  }
157 
158  return result;
159 }
160 
175 vector<vector<Tile*>> Play::getWords(vector<Tile*> tilesInStr, Board* b, int r, int c, char dir)
176 {
177  vector<Tile*> placedTiles;
178  int currRow = r;
179  int currCol = c;
180  Square* currSquare;
181 
182  try {
183  if(dir == 'h') {
184  try {
185  currSquare = b->getSquare(currRow, currCol);
186  while(currSquare && currSquare->getLeft() && !currSquare->getLeft()->isEmpty()) {
187  currSquare = currSquare->getLeft();
188  }
189 
190  while(currSquare && !currSquare->isEmpty()) {
191  placedTiles.push_back(currSquare->getTile());
192  currSquare = currSquare->getRight();
193  }
194  wordsInPlay.push_back(placedTiles);
195 
196  for(Tile* t : tilesInStr) {
197  currSquare = t->getSquare();
198  if(( currSquare && currSquare->getAbove() && !currSquare->getAbove()->isEmpty() )
199  || ( currSquare && currSquare->getBelow() && !currSquare->getBelow()->isEmpty() )) {
200  wordsInPlay.push_back(getConnectedWord(t, 'v'));
201  }
202  }
203  }
204  catch(string err) {
205  throw;
206  }
207  }
208  else if(dir == 'v') {
209  try {
210  currSquare = b->getSquare(currRow, currCol);
211  while(currSquare && currSquare->getAbove() && !currSquare->getAbove()->isEmpty()) {
212  if(currSquare) {
213  currSquare = currSquare->getAbove();
214  }
215  }
216 
217  while(currSquare && !currSquare->isEmpty()) {
218  placedTiles.push_back(currSquare->getTile());
219  currSquare = currSquare->getBelow();
220  }
221  wordsInPlay.push_back(placedTiles);
222 
223  for(Tile* t : tilesInStr) {
224  currSquare = t->getSquare();
225  if(( currSquare && currSquare->getLeft() && !currSquare->getLeft()->isEmpty() )
226  || ( currSquare && currSquare->getRight() && !currSquare->getRight()->isEmpty() )) {
227  wordsInPlay.push_back(getConnectedWord(t, 'h'));
228  }
229  }
230  }
231  catch(string err) {
232  throw;
233  }
234  }
235 
236  }
237  catch(string err) {
238  throw;
239  }
240 
241  return wordsInPlay;
242 }
243 
254 vector<Tile*> Play::getConnectedWord(Tile* t, char searchDir)
255 {
256  Square* currSquare;
257  vector<Tile*> connectedWord;
258 
259  if(searchDir == 'h') {
260  currSquare = t->getSquare();
261  while(currSquare && currSquare->getLeft() && !currSquare->getLeft()->isEmpty()) {
262  currSquare = currSquare->getLeft();
263  }
264 
265  while(currSquare && !currSquare->isEmpty()) {
266  connectedWord.push_back(currSquare->getTile());
267  currSquare = currSquare->getRight();
268  }
269  }
270  else if(searchDir == 'v') {
271  currSquare = t->getSquare();
272  while(currSquare && currSquare->getAbove() && !currSquare->getAbove()->isEmpty()) {
273  currSquare = currSquare->getAbove();
274  }
275 
276  while(currSquare && !currSquare->isEmpty()) {
277  connectedWord.push_back(currSquare->getTile());
278  currSquare = currSquare->getBelow();
279  }
280  }
281  else {
282  throw(string("Invalid direction\n"));
283  }
284 
285  return connectedWord;
286 }
287 
296 void Play::calculatePoints(vector<vector<Tile*>> words, vector<Tile*> tileStrVec)
297 {
298  int multiplier = 1;
299 
300  /*
301  * enum enum_sqType {N, DWS, TWS, DLS, TLS};
302  */
303  for(vector<Tile*> word : words) {
304  for(Tile* t : word) {
305  switch(t->getSquare()->getType()) {
306  case N:
307  pointsMade += t->getPoints();
308  break;
309  case DLS:
310  if(tilePresent(tileStrVec, t)) {
311  pointsMade += 2*(t->getPoints());
312  }
313  else {
314  t->show();
315  pointsMade += t->getPoints();
316  }
317  break;
318  case TLS:
319  if(tilePresent(tileStrVec, t)) {
320  pointsMade += 3*(t->getPoints());
321  }
322  else {
323  pointsMade += t->getPoints();
324  }
325  break;
326  case TWS:
327  pointsMade += t->getPoints();
328  if(tilePresent(tileStrVec, t)) {
329  multiplier *= 3;
330  }
331  break;
332  case DWS:
333  pointsMade += t->getPoints();
334  if(tilePresent(tileStrVec, t)) {
335  multiplier *= 2;
336  }
337  break;
338  }
339  }
340  }
341  pointsMade *= multiplier;
342 }
343 
352 {
353  return pointsMade;
354 }
355 
364 {
365  char ch;
366  PALE_GREEN_FG(" Confirm the play? (y/n) ");
367  cin >> ch;
368 
369  switch(ch) {
370  case 'y':
371  return true;
372  break;
373  case 'n':
374  return false;
375  break;
376  default:
377  return false;
378  break;
379  }
380 }
381 
388 {
389  pointsMade = 0;
390  wordsInPlay.clear();
391  playStr = "";
392 }
Definition: board.h:20
Square * getSquare(int r, int c)
Definition: board.cc:321
bool confirmPlay()
Definition: play.cc:363
void reset()
Definition: play.cc:387
void show()
Definition: play.cc:37
Play(Player *p)
Definition: play.cc:19
void calculatePoints(std::vector< std::vector< Tile * >> words, std::vector< Tile * > tileStrVec)
Definition: play.cc:296
std::vector< Tile * > getConnectedWord(Tile *t, char dir)
Definition: play.cc:254
~Play()
Definition: play.cc:29
int getPointsMade()
Definition: play.cc:351
std::vector< std::vector< Tile * > > getWords(std::vector< Tile * > tilesInStr, Board *b, int r, int c, char dir)
Definition: play.cc:175
bool validate(std::string tileStr, Board *b, int r, int c, char dir)
Definition: play.cc:90
void setPlayer(Player *p)
Definition: play.cc:62
Definition: player.h:19
Definition: square.h:30
Square * getRight()
Definition: square.cc:63
bool isEmpty()
Definition: square.cc:137
Square * getBelow()
Definition: square.cc:87
Square * getLeft()
Definition: square.cc:51
Square * getAbove()
Definition: square.cc:75
Tile * getTile()
Definition: square.cc:167
Definition: tile.h:22
Square * getSquare()
Definition: tile.cc:145
@ N
Definition: square.h:14
@ DWS
Definition: square.h:16
@ TWS
Definition: square.h:18
@ TLS
Definition: square.h:22
@ DLS
Definition: square.h:20
bool tilePresent(std::vector< Tile * > t, Tile *target)
Definition: utils.cc:43
void PALE_GREEN_FG(std::string x)
Definition: utils.h:82
void BOLD_WHITE_FG(std::string x)
Definition: utils.h:72