-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpiece_source.cpp
52 lines (45 loc) · 1.79 KB
/
piece_source.cpp
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
#include "piece_source.hpp"
///////////////////////////////////////////////////////////////////////////////
Piece_Source::~Piece_Source()
///////////////////////////////////////////////////////////////////////////////
{
for (unsigned i = 0; i < m_source.size(); i++) {
delete m_source[i];
}
}
///////////////////////////////////////////////////////////////////////////////
const Scrabble_Piece* Piece_Source::get_piece()
///////////////////////////////////////////////////////////////////////////////
{
my_static_assert(!is_empty(), "Tried to get_piece from an empty source");
return m_source[m_curr_idx++]; //return current piece and iterate
}
///////////////////////////////////////////////////////////////////////////////
const Scrabble_Piece* Piece_Source::get_piece(char value, bool tolerate_overflow)
///////////////////////////////////////////////////////////////////////////////
{
static constexpr unsigned not_found = static_cast<unsigned>(-1);
unsigned swap_idx = not_found;
for (unsigned i = m_curr_idx; i < m_source.size(); ++i) {
if (m_source[i]->get_letter() == value) {
swap_idx = i;
}
}
if (tolerate_overflow) {
if (swap_idx == not_found) {
// Create the missing piece and add it to the source. This creates a
// situation where the game now has more pieces that it normally would,
// but this is the best we can do.
const auto& scr_pm = get_point_map();
m_source.push_back(new Scrabble_Piece(m_parent, value, scr_pm));
swap_idx = m_source.size() - 1;
}
}
else {
my_static_assert(swap_idx != not_found, std::string("Requested unavailable value: ") + value);
}
const Scrabble_Piece* tmp = m_source[m_curr_idx];
m_source[m_curr_idx] = m_source[swap_idx];
m_source[swap_idx] = tmp;
return m_source[m_curr_idx++];
}