rp-scrabble
Simple, terminal-based Scrabble game
rack.cc
Go to the documentation of this file.
1 
4 #include <iostream>
5 #include "rack.h"
6 #include "tile.h"
7 #include "utils.h"
8 
9 using namespace std;
10 
15 {
16  rack.fill(nullptr);
17 }
18 
23 {
24  for(Tile* t : rack) {
25  if(t) {
26  delete t;
27  }
28  }
29 }
30 
34 void Rack::show()
35 {
36  int rackSize = rack.size();
37 
38  cout << " ";
39  for(int i = 0; i < rackSize; i++) {
40  BOARD_COLOURS("+-----");
41  }
42 
43  BOARD_COLOURS("+");
44  cout << "\n";
45  cout << " ";
46  BOARD_COLOURS("| ");
47 
48  for(unsigned long i = 0; i < rack.size(); i++) {
49  if(rack[i]) {
50  TILE_COLOURS(" " + rack[i]->getLetterStr() + " ");
51  }
52  else {
53  BOARD_COLOURS(" ");
54  }
55  if(i == 6) {
56  BOARD_COLOURS(" |");
57  }
58  else {
59  BOARD_COLOURS(" | ");
60  }
61  }
62  cout << "\n" << " ";
63  for(int i = 0; i < rackSize; i++) {
64  BOARD_COLOURS("+-----");
65  }
66 
67  BOARD_COLOURS("+");
68  cout << "\n";
69 }
70 
81 {
82  for(Tile* t : rack) {
83  if(t) {
84  if(t->getLetter() == ch) {
85  return t;
86  }
87  }
88  }
89  return nullptr;
90 }
91 
99 void Rack::fill(vector<Tile*> t)
100 {
101  unsigned long i = 0;
102  while(!t.empty() && i < rack.size()) {
103  if(!rack[i]) {
104  rack[i] = t.back();
105  rack[i]->setLoc(1);
106  rack[i]->setRack(this);
107  t.pop_back();
108  }
109  i++;
110  }
111 }
112 
121 {
122  for(unsigned long i = 0; i < rack.size(); i++) {
123  if(!rack[i]) {
124  rack[i] = t;
125  rack[i]->setLoc(1);
126  rack[i]->setRack(this);
127  break;
128  }
129  }
130 }
131 
141 vector<Tile*> Rack::getTileStrVec(string tileStr)
142 {
143  vector<Tile*> tileStrVec;
144  bool found;
145 
146  for(char ch : tileStr) {
147  found = false;
148  for(auto& t: rack) {
149  if(t && ch == t->getLetter()) {
150  found = true;
151  tileStrVec.push_back(t);
152  t = nullptr; // "Remove" from rack
153  break;
154  }
155  }
156  if(!found) {
157  // Abort!! Reset the rack and start again
158  fill(tileStrVec);
159  throw string(string(1, ch) + " not found\n");
160  }
161  }
162  return tileStrVec;
163 }
164 
173 {
174  for(unsigned long i = 0; i < rack.size(); i++) {
175  if(rack[i]) {
176  return false;
177  }
178  }
179  return true;
180 }
std::vector< Tile * > getTileStrVec(std::string tileStr)
Definition: rack.cc:141
void fill(std::vector< Tile * > t)
Definition: rack.cc:99
Rack()
Definition: rack.cc:14
void show()
Definition: rack.cc:34
Tile * getTile(char ch)
Definition: rack.cc:80
void addTile(Tile *t)
Definition: rack.cc:120
~Rack()
Definition: rack.cc:22
bool isEmpty()
Definition: rack.cc:172
Definition: tile.h:22
void setLoc(int loc)
Definition: tile.cc:183
void BOARD_COLOURS(std::string x)
Definition: utils.h:132
void TILE_COLOURS(std::string x)
Definition: utils.h:127