-
Notifications
You must be signed in to change notification settings - Fork 2
/
scrabble_word.cpp
74 lines (67 loc) · 2.46 KB
/
scrabble_word.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "scrabble_word.hpp"
using namespace std;
////////////////////////////////////////////////////////////////////////////////
unsigned Scrabble_Word::score() const
////////////////////////////////////////////////////////////////////////////////
{
unsigned word_multiplier = 1, pre_multiplier_total = 0;
for (std::map<unsigned, pair<const Scrabble_Piece*, Bonus> >::const_iterator itr = m_word.begin();
itr != m_word.end(); itr++) {
unsigned base_pts = itr->second.first->get_point_val();
Bonus bonus = itr->second.second;
switch(bonus) {
case NONE:
pre_multiplier_total += base_pts;
break;
case DBL_LET:
pre_multiplier_total += base_pts*2;
break;
case TRP_LET:
pre_multiplier_total += base_pts*3;
break;
case DBL_WRD:
pre_multiplier_total += base_pts;
word_multiplier *= 2;
break;
case TRP_WRD:
pre_multiplier_total += base_pts;
word_multiplier *= 3;
break;
default:
my_assert(false, std::string("Missing case for: ") + obj_to_str(bonus));
}
}
return pre_multiplier_total * word_multiplier;
}
////////////////////////////////////////////////////////////////////////////////
void Scrabble_Word::add_component(const Scrabble_Piece* piece, Bonus bonus, unsigned order)
////////////////////////////////////////////////////////////////////////////////
{
my_assert(m_word.find(order) == m_word.end(),
obj_to_str(order) + " was already in the word.");
m_word[order] = pair<const Scrabble_Piece*, Bonus>(piece, bonus);
}
////////////////////////////////////////////////////////////////////////////////
std::string Scrabble_Word::get_word_str() const
////////////////////////////////////////////////////////////////////////////////
{
std::string rv;
for (std::map<unsigned, pair<const Scrabble_Piece*, Bonus> >::const_iterator itr = m_word.begin();
itr != m_word.end(); itr++) {
rv += itr->second.first->get_letter();
}
return rv;
}
////////////////////////////////////////////////////////////////////////////////
ostream& Scrabble_Word::operator<<(ostream& out) const
////////////////////////////////////////////////////////////////////////////////
{
out << get_word_str();
return out;
}
////////////////////////////////////////////////////////////////////////////////
ostream& operator<<(ostream& out, const Scrabble_Word& sw)
////////////////////////////////////////////////////////////////////////////////
{
return sw.operator<<(out);
}