-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTileBag.cpp
More file actions
75 lines (62 loc) · 2.2 KB
/
TileBag.cpp
File metadata and controls
75 lines (62 loc) · 2.2 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
// TileBag.cpp: implementation of the TileBag class.
//
//////////////////////////////////////////////////////////////////////
/*********************************************************************************
** Author : C.Brown (c)COPYRIGHT C.Brown 2001-2026 ALL RIGHTS RESERVED **
** Date : 19/09/2001 **
** File : TileBag.cpp **
** Purpose : Hold and distribute tiles **
** Version : 0.86 beta 28/10/2001 **
** History : Replaced rand() with <random> **
*********************************************************************************/
#include "TileBag.hpp"
TileBag::TileBag()
: rng_(std::random_device{}())
{
}
//=======================================================================
// RANDGRAB() This function grabs a tile at random from the bag
//=======================================================================
std::optional<Tile> TileBag::randGrab()
{
if (bag_.empty()) {
return std::nullopt;
}
std::uniform_int_distribution<size_t> dist(0, bag_.size() - 1);
size_t pos = dist(rng_);
Tile tile = bag_[pos];
bag_.erase(bag_.begin() + pos);
return tile;
}
//=======================================================================
// LOADBAG() This function loads the tile bag from external file
//=======================================================================
void TileBag::loadBag()
{
std::ifstream in("letfil.bin");
if (!in.is_open()) {
std::cout << "Unable to open file letfil.bin\n";
std::cout << "Press any key to exit...";
while (!_kbhit());
return;
}
std::string line;
while (std::getline(in, line)) {
std::istringstream lineStream(line);
unsigned char letter;
int score;
if (lineStream >> letter >> score) {
Tile temp;
temp.setLetter(letter);
temp.setScore(score);
bag_.push_back(temp);
}
}
}
//=======================================================================
// ADDTILE() Adds tiles back to the bag (for exchanging)
//=======================================================================
void TileBag::addTile(const std::vector<Tile>& tiles)
{
bag_.insert(bag_.end(), tiles.begin(), tiles.end());
}