-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscrabble_config.cpp
173 lines (155 loc) · 5.47 KB
/
scrabble_config.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
#include "scrabble_config.hpp"
#include "scrabble_exception.hpp"
#include <cassert>
#include <sstream>
using namespace std;
////////////////////////////////////////////////////////////////////////////////
Board_Type Scrabble_Config::str_to_board(const std::string& board_str)
////////////////////////////////////////////////////////////////////////////////
{
if (board_str == "scrabble") {
return STANDARD_BOARD;
}
else if (board_str == "wwf") {
return WWF_BOARD;
}
else if (board_str == "wwf-solo") {
return WWF_SOLO_BOARD;
}
else {
my_require(false, std::string("Unknown board string: ") + board_str);
}
}
////////////////////////////////////////////////////////////////////////////////
Piece_Source_Type Scrabble_Config::str_to_tileset(const std::string& tileset_str)
////////////////////////////////////////////////////////////////////////////////
{
if (tileset_str == "scrabble") {
return STANDARD_SOURCE;
}
else if (tileset_str == "wwf") {
return WWF_SOURCE;
}
else {
my_require(false, std::string("Unknown tileset string: ") + tileset_str);
}
}
////////////////////////////////////////////////////////////////////////////////
Scrabble_Config::Scrabble_Config(unsigned num_players,
const std::vector<Player_Type>& player_types,
const std::vector<std::string>& player_names,
unsigned num_player_pieces,
Board_Type board_type,
Output_Type output,
bool color_output,
bool clear_screen_before_output,
const std::string& dictionary,
Piece_Source_Type piece_source_type,
unsigned max_num_log_msgs_to_displ,
unsigned constrained_square_limit,
PyObject* py) :
m_num_players(num_players),
m_player_types(player_types),
m_player_names(player_names),
m_num_player_pieces(num_player_pieces),
m_board_type(board_type),
m_output(output),
m_color_output(color_output),
m_clear_screen_before_output(clear_screen_before_output),
m_dictionary(dictionary),
m_piece_source_type(piece_source_type),
m_max_num_log_msgs_to_displ(max_num_log_msgs_to_displ),
m_constrained_square_limit(constrained_square_limit),
m_py(py)
////////////////////////////////////////////////////////////////////////////////
{
my_static_assert(num_players == player_types.size(),
"num_players did not match player_types.size()");
my_static_assert(!(py == nullptr && output == GUI),
"Cannot do GUI without py callback obj");
}
////////////////////////////////////////////////////////////////////////////////
Scrabble_Config::Scrabble_Config(std::istream& in, Output_Type output, PyObject* py) :
m_num_players(),
m_player_types(),
m_player_names(),
m_num_player_pieces(),
m_board_type(),
m_output(output),
m_color_output(true),
m_clear_screen_before_output(true),
m_dictionary(),
m_piece_source_type(),
m_max_num_log_msgs_to_displ(10),
m_constrained_square_limit(3),
m_py(py)
////////////////////////////////////////////////////////////////////////////////
{
std::string line;
int rv;
getline(in, line);
my_require(line == "Config", "Expected 'Config'");
getline(in, line);
rv = sscanf(line.c_str(), "num players: %d", &m_num_players);
my_require(rv == 1, "Bad num players format");
getline(in, line);
istringstream iss(line);
for (unsigned i = 0; i < m_num_players; ++i) {
int tmp;
iss >> tmp;
Player_Type t = static_cast<Player_Type>(tmp);
m_player_types.push_back(t);
}
getline(in, line);
std::string curr_name;
for (char c : line) {
if (c == '|') {
m_player_names.push_back(curr_name);
curr_name = "";
}
else {
curr_name += c;
}
}
getline(in, line);
rv = sscanf(line.c_str(), "num pieces: %d", &m_num_player_pieces);
my_require(rv == 1, "Bad num pieces format");
getline(in, line);
rv = sscanf(line.c_str(), "board type: %d", &m_board_type);
my_require(rv == 1, "Bad board type format");
char buf[64];
getline(in, line);
rv = sscanf(line.c_str(), "dict: %s", buf);
my_require(rv == 1, "Bad dict format");
m_dictionary = std::string(buf);
getline(in, line);
rv = sscanf(line.c_str(), "piece source: %d", &m_piece_source_type);
my_require(rv == 1, "Bad piece source format");
getline(in, line);
my_require(line == "", "Expected blank line");
}
////////////////////////////////////////////////////////////////////////////////
ostream& Scrabble_Config::operator<<(ostream& out) const
////////////////////////////////////////////////////////////////////////////////
{
out << "num players: " << m_num_players << "\n";
for (auto item : m_player_types) {
out << item << " ";
}
out << "\n";
for (auto name : m_player_names) {
out << name << "|";
}
out << "\n";
out << "num pieces: " << m_num_player_pieces << "\n";
out << "board type: " << m_board_type << "\n";
out << "dict: " << m_dictionary << "\n";
out << "piece source: " << m_piece_source_type << "\n";
return out;
}
////////////////////////////////////////////////////////////////////////////////
ostream& operator<<(ostream& out, const Scrabble_Config& config)
////////////////////////////////////////////////////////////////////////////////
{
return config.operator<<(out);
}