improve error handling
This commit is contained in:
parent
cc9f073d51
commit
23978ada76
2 changed files with 32 additions and 35 deletions
|
@ -153,6 +153,10 @@ namespace Download {
|
||||||
}
|
}
|
||||||
}();
|
}();
|
||||||
|
|
||||||
|
if(game_json.empty()) {
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
const auto [deck, num_suits] = parse_deck(game_json.at("deck"));
|
const auto [deck, num_suits] = parse_deck(game_json.at("deck"));
|
||||||
const std::vector<Action> actions = parse_actions(game_json.at("actions"));
|
const std::vector<Action> actions = parse_actions(game_json.at("actions"));
|
||||||
const size_t num_players = game_json.at("players").as_array().size();
|
const size_t num_players = game_json.at("players").as_array().size();
|
||||||
|
|
63
src/main.cpp
63
src/main.cpp
|
@ -16,9 +16,14 @@
|
||||||
|
|
||||||
namespace Hanabi {
|
namespace Hanabi {
|
||||||
void download(std::variant<int, const char*> game_id, int turn) {
|
void download(std::variant<int, const char*> game_id, int turn) {
|
||||||
auto game = Download::get_game(game_id, turn);
|
auto game = Download::get_game(game_id, turn - 1);
|
||||||
|
if (game == nullptr) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
std::cout << "Analysing state: " << std::endl << *game << std::endl;
|
std::cout << "Analysing state: " << std::endl << *game << std::endl;
|
||||||
auto res = game->evaluate_state();
|
auto res = game->evaluate_state();
|
||||||
|
|
||||||
std::cout.precision(10);
|
std::cout.precision(10);
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
std::cout << "Probability with optimal play: " << res << std::endl;
|
std::cout << "Probability with optimal play: " << res << std::endl;
|
||||||
|
@ -36,30 +41,22 @@ namespace Hanabi {
|
||||||
cli(game_shared);
|
cli(game_shared);
|
||||||
}
|
}
|
||||||
|
|
||||||
void print_sizes() {
|
|
||||||
std::cout << "size of card -> hand map: " << sizeof(HanabiState<5, 3, 4>)
|
|
||||||
<< std::endl;
|
|
||||||
|
|
||||||
unsigned exp = 32;
|
|
||||||
std::cout << "Pair size: " << sizeof(std::pair<std::uint32_t, float>)
|
|
||||||
<< std::endl;
|
|
||||||
std::cout << sizeof(boost::rational<int>) << std::endl;
|
|
||||||
std::cout << (1ul << exp) << std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
void print_usage(const char* program_name) {
|
void print_usage(const char* program_name) {
|
||||||
std::cout << "Usage: " << program_name << " GAME_ID TURN" << std::endl;
|
std::cout << "Usage: " << program_name << "(GAME_ID | GAME_FILE) TURN" << std::endl;
|
||||||
|
std::cout << " where GAME_ID is an id from hanab.live and GAME_FILE is a path to a file describing the game in hanab.live json format";
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#define CHECK(test, condition) if (!(condition)) { std::cerr << "Test " << (test) << " failed." << std::endl; } else { std::cout << "Test " << (test) << " succeeded." << std::endl; }
|
|
||||||
|
|
||||||
void test() {
|
void test() {
|
||||||
{
|
{
|
||||||
auto game = Download::get_game("in/1005195", 43);
|
auto game = Download::get_game("in/1005195", 43);
|
||||||
auto res = game->evaluate_state();
|
auto res = game->evaluate_state();
|
||||||
CHECK("1005195", res == Hanabi::probability_t (7,8));
|
if (!(res == Hanabi::probability_t(7, 8))) {
|
||||||
|
std::cerr << "Test " << ("1005195") << " failed." << std::endl;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
std::cout << "Test " << ("1005195") << " succeeded." << std::endl;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -112,29 +109,25 @@ void check_games(unsigned num_players, unsigned max_draw_pile_size, unsigned fir
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
#ifndef NDEBUG
|
|
||||||
test();
|
|
||||||
#endif
|
|
||||||
if(argc == 3) {
|
if(argc == 3) {
|
||||||
std::string game(argv[1]);
|
std::string game_str(argv[1]);
|
||||||
std::string turn (argv[2]);
|
std::string turn_str (argv[2]);
|
||||||
|
|
||||||
|
int turn = 0;
|
||||||
try {
|
try {
|
||||||
Hanabi::download(std::stoi(game), std::stoi(turn));
|
turn = std::stoi(turn_str);
|
||||||
} catch(std::invalid_argument&) {
|
} catch(std::invalid_argument&) {
|
||||||
Hanabi::download(game.c_str(), std::stoi(turn));
|
std::cout << "Could not parse turn number " << turn << std::endl;
|
||||||
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
} else if (argc == 2) {
|
|
||||||
int player_count = std::stoi(argv[1]);
|
try {
|
||||||
if (player_count == 3) {
|
Hanabi::download(std::stoi(game_str), turn);
|
||||||
check_games(3, 10);
|
} catch(std::invalid_argument&) {
|
||||||
} else if (player_count == 4) {
|
Hanabi::download(game_str.c_str(), turn);
|
||||||
check_games(4, 9);
|
|
||||||
} else if (player_count == 5) {
|
|
||||||
check_games(5, 9);
|
|
||||||
} else if (player_count == 2) {
|
|
||||||
check_games(2, 10);
|
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
Hanabi::print_usage(argv[0]);
|
Hanabi::print_usage(argv[0]);
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
|
|
Loading…
Reference in a new issue