-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscrabble_facade.cpp
223 lines (189 loc) · 7.47 KB
/
scrabble_facade.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include "scrabble_facade.hpp"
#include "human_player.hpp"
#include "gui_human_player.hpp"
#include "ai_player.hpp"
#include "scrabble_board.hpp"
#include "scrabble_game_builder.hpp"
#include "standard_board_builder.hpp"
#include "wwf_solo_board_builder.hpp"
#include "wwf_board_builder.hpp"
#include "standard_piece_source.hpp"
#include "wwf_piece_source.hpp"
#include "scrabble_game.hpp"
#include "scrabble_config.hpp"
#include "scrabble_kokkos.hpp"
#include <cstdlib>
#include <ctime>
#include <fstream>
using namespace std;
////////////////////////////////////////////////////////////////////////////////
void Scrabble_Facade::play(PyObject* py,
const std::vector<std::string>& players,
const std::vector<std::string>& ais,
const std::string& dictionary,
const Board_Type board,
const Piece_Source_Type tileset,
const int random_seed,
const Output_Type output)
////////////////////////////////////////////////////////////////////////////////
{
//allocate variables to hold the options
std::vector<Player_Type> player_types;
std::vector<std::string> player_names;
std::vector<std::string> tests_to_run;
const unsigned num_player_pieces = 7;
const bool color_output = true;
const bool clear_screen_before_output = true;
const unsigned max_num_log_msgs_to_displ = 10;
const unsigned constrained_square_limit = 3;
srand(random_seed);
for (const string& name : players) {
player_names.push_back(name);
player_types.push_back(HUMAN);
}
for (const string& name : ais) {
player_names.push_back(name);
player_types.push_back(AI);
}
Scrabble_Config config(player_types.size(), player_types, player_names,
num_player_pieces, board,
output, color_output, clear_screen_before_output,
dictionary, tileset, max_num_log_msgs_to_displ,
constrained_square_limit, py);
auto game = create_game(config);
game->play();
}
////////////////////////////////////////////////////////////////////////////////
void Scrabble_Facade::load(PyObject* py,
const std::string& save_path,
const int random_seed,
const Output_Type output)
////////////////////////////////////////////////////////////////////////////////
{
auto game = load_game(save_path, py, &random_seed, output);
game->play();
}
////////////////////////////////////////////////////////////////////////////////
std::shared_ptr<Scrabble_Game> Scrabble_Facade::get_test_game(const int* random_seed)
////////////////////////////////////////////////////////////////////////////////
{
// Default config with one AI player
std::vector<std::string> names = {"testbot"};
std::vector<Player_Type> types = {AI};
Scrabble_Config default_config(types.size(), types, names);
if (random_seed != nullptr) {
srand(*random_seed);
}
return create_game(default_config);
}
////////////////////////////////////////////////////////////////////////////////
std::shared_ptr<Scrabble_Game> Scrabble_Facade::load_test_game(const std::string& save_path, const int* random_seed)
////////////////////////////////////////////////////////////////////////////////
{
return load_game(save_path, nullptr, random_seed, TEXT);
}
////////////////////////////////////////////////////////////////////////////////
std::shared_ptr<Scrabble_Game> Scrabble_Facade::create_game(const Scrabble_Config& config)
////////////////////////////////////////////////////////////////////////////////
{
Scrabble_Game_Builder builder;
//build up the game
builder.build_scrabble_game(config);
if (config.BOARD_TYPE() == STANDARD_BOARD) {
builder.build_game_board<Standard_Board_Builder>();
}
else if (config.BOARD_TYPE() == WWF_BOARD) {
builder.build_game_board<Wwf_Board_Builder>();
}
else if (config.BOARD_TYPE() == WWF_SOLO_BOARD) {
builder.build_game_board<Wwf_Solo_Board_Builder>();
}
else {
my_static_assert(false, "unknown board type");
}
std::vector<Player_Type> player_types = config.PLAYER_TYPES();
std::vector<std::string> player_names = config.PLAYER_NAMES();
my_static_assert(player_types.size() == player_names.size(),
"Player-type vector size did not match that of player-name vector");
for (unsigned i = 0; i < player_types.size(); ++i) {
if (player_types[i] == HUMAN) {
if (config.OUTPUT() == GUI) {
builder.build_player<GUI_Human_Player>(player_names[i]);
}
else {
builder.build_player<Human_Player>(player_names[i]);
}
}
else if (player_types[i] == AI) {
builder.build_player<AI_Player>(player_names[i]);
}
else {
my_static_assert(false, "unknown player type");
}
}
if (config.PIECE_SOURCE_TYPE() == STANDARD_SOURCE) {
builder.build_piece_source<Standard_Piece_Source>();
}
else if (config.PIECE_SOURCE_TYPE() == WWF_SOURCE) {
builder.build_piece_source<Wwf_Piece_Source>();
}
else {
my_static_assert(false, "unknown source type");
}
return std::shared_ptr<Scrabble_Game>(builder.get_game());
}
////////////////////////////////////////////////////////////////////////////////
std::shared_ptr<Scrabble_Game> Scrabble_Facade::load_game(const std::string& save_path,
PyObject* py,
const int* random_seed,
const Output_Type output)
////////////////////////////////////////////////////////////////////////////////
{
ifstream in(save_path);
my_require(in.is_open(), std::string("Failed to load file ") + save_path);
Scrabble_Config config(in, output, py);
if (random_seed != nullptr) {
srand(*random_seed);
}
auto game = create_game(config);
game->load(in);
in.close();
return game;
}
////////////////////////////////////////////////////////////////////////////////
void c_scrabble(PyObject* py,
const int num_players, const char** players,
const int num_ais, const char** ais,
const char* load_game,
const char* dictionary,
const char* board,
const char* tileset,
const int random_seed,
const bool gui)
////////////////////////////////////////////////////////////////////////////////
{
std::vector<std::string> cxx_players(num_players), cxx_ais(num_ais);
std::string cxx_load_game(load_game);
std::string cxx_dict(dictionary);
std::string cxx_board_str(board);
std::string cxx_tileset_str(tileset);
for (int i = 0; i < num_players; ++i) {
cxx_players[i] = std::string(players[i]);
}
for (int i = 0; i < num_ais; ++i) {
cxx_ais[i] = std::string(ais[i]);
}
Board_Type cxx_board = Scrabble_Config::str_to_board(cxx_board_str);
Piece_Source_Type cxx_tileset = Scrabble_Config::str_to_tileset(cxx_tileset_str);
Output_Type cxx_output = gui ? GUI : TEXT;
const char* ignore = "";
int ignore_i = 1;
Kokkos::initialize(ignore_i, const_cast<char**>(&ignore));
if (cxx_load_game != "") {
Scrabble_Facade::load(py, cxx_load_game, random_seed, cxx_output);
}
else {
Scrabble_Facade::play(py, cxx_players, cxx_ais, cxx_dict, cxx_board, cxx_tileset, random_seed, cxx_output);
}
Kokkos::finalize();
}