rp-scrabble
Simple, terminal-based Scrabble game
bag.cc
Go to the documentation of this file.
1 
4 #include <iostream>
5 #include <cstdlib>
6 #include <ctime>
7 #include "bag.h"
8 #include "tile.h"
9 #include "utils.h"
10 
11 using namespace std;
12 
14 
20 {
21  Tile* curTile = nullptr;
22  /* Add a's */
23  for(int i = 0; i < 9; i++) {
24  curTile = new Tile('A', 1, loc);
25  bag.push_back(curTile);
26  }
27 
28  /* Add b's */
29  for(int i = 0; i < 2; i++) {
30  curTile = new Tile('B', 3, loc);
31  bag.push_back(curTile);
32  }
33 
34  /* Add c's */
35  for(int i = 0; i < 2; i++) {
36  curTile = new Tile('C', 3, loc);
37  bag.push_back(curTile);
38  }
39 
40  /* Add d's */
41  for(int i = 0; i < 4; i++) {
42  curTile = new Tile('D', 2, loc);
43  bag.push_back(curTile);
44  }
45 
46  /* Add e's */
47  for(int i = 0; i < 12; i++) {
48  curTile = new Tile('E', 1, loc);
49  bag.push_back(curTile);
50  }
51 
52  /* Add f's */
53  for(int i = 0; i < 2; i++) {
54  curTile = new Tile('F', 4, loc);
55  bag.push_back(curTile);
56  }
57 
58  /* Add g's */
59  for(int i = 0; i < 3; i++) {
60  curTile = new Tile('G', 2, loc);
61  bag.push_back(curTile);
62  }
63 
64  /* Add h's */
65  for(int i = 0; i < 2; i++) {
66  curTile = new Tile('H', 4, loc);
67  bag.push_back(curTile);
68  }
69 
70  /* Add i's */
71  for(int i = 0; i < 9; i++) {
72  curTile = new Tile('I', 1, loc);
73  bag.push_back(curTile);
74  }
75 
76  /* Add j */
77  curTile = new Tile('J', 8, loc);
78  bag.push_back(curTile);
79 
80  /* Add k */
81  curTile = new Tile('K', 5, loc);
82  bag.push_back(curTile);
83 
84  /* Add l's */
85  for(int i = 0; i < 4; i++) {
86  curTile = new Tile('L', 1, loc);
87  bag.push_back(curTile);
88  }
89 
90  /* Add m's */
91  for(int i = 0; i < 2; i++) {
92  curTile = new Tile('M', 3, loc);
93  bag.push_back(curTile);
94  }
95 
96  /* Add n's */
97  for(int i = 0; i < 6; i++) {
98  curTile = new Tile('N', 1, loc);
99  bag.push_back(curTile);
100  }
101 
102  /* Add o's */
103  for(int i = 0; i < 8; i++) {
104  curTile = new Tile('O', 1, loc);
105  bag.push_back(curTile);
106  }
107 
108  /* Add p's */
109  for(int i = 0; i < 2; i++) {
110  curTile = new Tile('P', 3, loc);
111  bag.push_back(curTile);
112  }
113 
114  /* Add q */
115  curTile = new Tile('Q', 10, loc);
116  bag.push_back(curTile);
117 
118  /* Add r's */
119  for(int i = 0; i < 6; i++) {
120  curTile = new Tile('R', 1, loc);
121  bag.push_back(curTile);
122  }
123 
124  /* Add s's */
125  for(int i = 0; i < 4; i++) {
126  curTile = new Tile('S', 1, loc);
127  bag.push_back(curTile);
128  }
129 
130  /* Add t's */
131  for(int i = 0; i < 6; i++) {
132  curTile = new Tile('T', 1, loc);
133  bag.push_back(curTile);
134  }
135 
136  /* Add u's */
137  for(int i = 0; i < 4; i++) {
138  curTile = new Tile('U', 3, loc);
139  bag.push_back(curTile);
140  }
141 
142  /* Add v's */
143  for(int i = 0; i < 2; i++) {
144  curTile = new Tile('V', 4, loc);
145  bag.push_back(curTile);
146  }
147 
148  /* Add w's */
149  for(int i = 0; i < 2; i++) {
150  curTile = new Tile('W', 4, loc);
151  bag.push_back(curTile);
152  }
153  /* Add x */
154  curTile = new Tile('X', 8, loc);
155  bag.push_back(curTile);
156 
157  /* Add y's */
158  for(int i = 0; i < 2; i++) {
159  curTile = new Tile('Y', 4, loc);
160  bag.push_back(curTile);
161  }
162 
163  /* Add z */
164  curTile = new Tile('Z', 10, loc);
165  bag.push_back(curTile);
166 
167  /*
168  * [> Add blanks <]
169  * for(int i = 0; i < 2; i++){
170  * curTile = new Tile('#', 0, loc);
171  * bag.push_back(curTile);
172  * }
173  */
174 
175  for(auto& t : bag) {
176  t->setBag(this);
177  }
178 }
179 
184 {
185  for(Tile* t : bag) {
186  delete t;
187  }
188 
189  bag.clear();
190 }
191 
195 void Bag::show()
196 {
197  for(Tile* i : bag) {
198  cout << i->getLetter();
199  }
200  BOLD(" (" + to_string(bag.size()) + " tiles remaining)\n");
201 }
202 
209 {
210  int j;
211  Tile* temp = nullptr;
212  srand(time(NULL));
213 
214  for(unsigned long i = 0; i < bag.size(); i++) {
215  j = rand() % bag.size(); // Generate random index
216  temp = bag[i];
217  bag[i] = bag[j];
218  bag[j] = temp;
219  }
220 
221 }
222 
232 vector<Tile*> Bag::draw(int count)
233 {
234  vector<Tile*> drawn;
235  unsigned long prev_size = bag.size();
236 
237  shuffle();
238 
239  for(unsigned long i = 0; i < (unsigned long) count && i < prev_size; i++) {
240  drawn.push_back(bag.back());
241  bag.pop_back();
242  }
243  return drawn;
244 }
245 
254 {
255  return(bag.empty());
256 }
enum_location loc
Definition: bag.cc:13
void show()
Definition: bag.cc:195
std::vector< Tile * > draw(int count)
Definition: bag.cc:232
~Bag()
Definition: bag.cc:183
bool isEmpty()
Definition: bag.cc:253
Bag()
Definition: bag.cc:19
void shuffle()
Definition: bag.cc:208
Definition: tile.h:22
enum_location
Definition: tile.h:15
@ BAG
Definition: tile.h:15
void BOLD(std::string x)
Definition: utils.h:37