-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscrabble_square.cpp
194 lines (176 loc) · 5.13 KB
/
scrabble_square.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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include "scrabble_square.hpp"
#include "scrabble_exception.hpp"
#include "scrabble_game.hpp"
#include <algorithm>
#include <sstream>
using namespace std;
////////////////////////////////////////////////////////////////////////////////
void Scrabble_Square::add_piece(const Scrabble_Piece* piece)
////////////////////////////////////////////////////////////////////////////////
{
my_assert(piece, "Tried to add NULL piece");
my_assert(is_free(), "Tried to add piece to non-null square.");
m_piece = piece;
m_piece->played();
}
////////////////////////////////////////////////////////////////////////////////
void Scrabble_Square::set_bonus(Bonus bonus)
////////////////////////////////////////////////////////////////////////////////
{
my_assert(m_bonus == NONE, "Bonus had already been set");
my_assert(bonus != NONE, "No need to set bonus to NONE");
m_bonus = bonus;
}
////////////////////////////////////////////////////////////////////////////////
int Scrabble_Square::get_color_code() const
////////////////////////////////////////////////////////////////////////////////
{
switch(m_bonus) {
case NONE:
return 29;
case DBL_LET:
return 34;
case TRP_LET:
return 32;
case DBL_WRD:
return 31;
case TRP_WRD:
return 33;
default:
my_static_assert(false, "Missing case for some bonus.");
}
return -1;
}
////////////////////////////////////////////////////////////////////////////////
ostream& Scrabble_Square::operator<<(ostream& out) const
////////////////////////////////////////////////////////////////////////////////
{
out << "| ";
if (m_piece) {
out << " " << *m_piece << " ";
}
else {
const int color_code = get_color_code();
const bool use_color = m_parent != nullptr && m_parent->get_config().COLOR_OUTPUT();
if (use_color) {
out << "\033[1;" << color_code << "m";
}
out << m_bonus;
if (use_color) {
out << "\033[0m";
}
}
out << " ";
return out;
}
////////////////////////////////////////////////////////////////////////////////
istream& Scrabble_Square::operator>>(istream& in)
////////////////////////////////////////////////////////////////////////////////
{
auto& piece_source = const_cast<Scrabble_Game*>(m_parent)->get_piece_source();
constexpr unsigned width_without_sep = OUTPUT_LEN - 1;
std::string line;
for (unsigned i = 0; i < width_without_sep; ++i) {
char c;
in.read(&c, 1);
line += c;
}
unsigned blanks = std::count(line.begin(), line.end(), ' ');
if (blanks == width_without_sep) {
// blank square, do nothing
}
else if (blanks == width_without_sep - 1) {
// it has a piece on it
char piece_val = line[OUTPUT_LEN / 2 - 1];
if (islower(piece_val)) {
auto piece = piece_source.get_piece('-');
piece->set_wildcard_value(toupper(piece_val));
add_piece(piece);
}
else {
// We need to tolerate overflow here since the user could have used
// admin more to create words with more letters of type X than exist
// in the piece source.
add_piece(piece_source.get_piece(piece_val, true /*tolerate_overflow*/));
}
}
else if (blanks == width_without_sep - 3) {
// bonus
Bonus bonus;
std::string bonus_str = line.substr(1, 3);
istringstream iss(bonus_str);
iss >> bonus;
//set_bonus(bonus); // should already be set by builder
}
else {
my_static_assert(false, std::string("Could not recognize square: ") + line);
}
return in;
}
////////////////////////////////////////////////////////////////////////////////
ostream& operator<<(ostream& out, const Scrabble_Square& sq)
////////////////////////////////////////////////////////////////////////////////
{
return sq.operator<<(out);
}
////////////////////////////////////////////////////////////////////////////////
istream& operator>>(istream& in, Scrabble_Square& sq)
////////////////////////////////////////////////////////////////////////////////
{
return sq.operator>>(in);
}
////////////////////////////////////////////////////////////////////////////////
ostream& operator<<(ostream& out, const Bonus& b)
////////////////////////////////////////////////////////////////////////////////
{
switch(b) {
case NONE:
out << " ";
break;
case DBL_LET:
out << "b2l";
break;
case TRP_LET:
out << "b3l";
break;
case DBL_WRD:
out << "b2w";
break;
case TRP_WRD:
out << "b3w";
break;
default:
my_static_assert(false, "Missing case for some bonus.");
}
return out;
}
////////////////////////////////////////////////////////////////////////////////
istream& operator>>(istream& in, Bonus& b)
////////////////////////////////////////////////////////////////////////////////
{
std::string bonus_str;
for (unsigned i = 0; i < 3; ++i) {
char c;
in >> c;
bonus_str += c;
}
if (bonus_str == " ") {
b = NONE;
}
else if (bonus_str == "b2l") {
b = DBL_LET;
}
else if (bonus_str == "b3l") {
b = TRP_LET;
}
else if (bonus_str == "b2w") {
b = DBL_WRD;
}
else if (bonus_str == "b3w") {
b = TRP_WRD;
}
else {
my_static_assert(false, std::string("Unrecognized bonus string: ") + bonus_str );
}
return in;
}