-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathbag.cc
115 lines (96 loc) · 1.99 KB
/
bag.cc
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
#include<iostream>
#include<vector>
#include<cassert>
#include<cstdlib>
#include<ctime>
#include "tile.h"
#include "bag.h"
using namespace std;
//using array
Bag::Bag():
numTiles(100)
{
//random picking of tiles
srand(time(nullptr));
const char letters[] = {' ','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
const int counts[] = {2,9,2,2,4,12,2,3,2,9,1,1,4,2,6,8,2,1,6,4,6,4,2,2,1,2,1};
const int points[] = {0,1,3,3,2,1,4,2,4,1,8,5,1,3,1,1,3,10,1,1,1,1,4,4,8,4,10};
//~ const int points[] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
int n = 0;
for(int i=0; i<27; i++)
for(int j=0; j<counts[i]; j++)
tiles[n++] = new Tile(letters[i], points[i]);
selfTest();
}
Bag::~Bag()
{
for(int i=0; i<100; i++)
delete tiles[i];
}
void Bag::show() const
{
for(int i=0; i<numTiles; i++)
{
tiles[i]->show();
cout << endl;
}
}
int Bag::getNumTiles() const
{
return numTiles;
}
Tile* Bag::getTile()
{
if(numTiles)
{
int index = rand()%numTiles;
Tile* temp = tiles[index]; // return this
tiles[index] = tiles[numTiles - 1]; // move last tile to random index
tiles[numTiles - 1] = temp; // move random tile to last
numTiles--;
selfTest();
return temp;
}
else
return nullptr;
}
void Bag::reset()
{
numTiles = 100;
selfTest();
}
void Bag::selfTest() const
{
//cout << "Testing bag ... ";
for(int i=0; i<numTiles; i++)
tiles[i]->selfTest();
//cout << "passed" << endl;
}
void Bag::addTile(Tile* t)
{
if(numTiles != 100)
tiles[numTiles] = t;
numTiles++;
selfTest();
}
Tile* Bag::getTile( char letter )
{
int location = 0;
bool found = 0;
Tile *tile = nullptr;
for( int ndx = 0; !found && ndx < numTiles; ndx++ ){
tile = tiles[ndx];
if( tile->getLetter() == letter ){
found = true;
location = ndx;
}
}
if( found ){
// Push the null to the back
tiles[location] = tiles[numTiles-1];
tiles[numTiles-1] = tile;
numTiles--;
}
selfTest();
return tile;
}