improve error handling

This commit is contained in:
Maximilian Keßler 2023-08-12 12:14:41 +02:00
parent cc9f073d51
commit 23978ada76
Signed by: max
GPG key ID: BCC5A619923C0BA5
2 changed files with 32 additions and 35 deletions

View file

@ -153,6 +153,10 @@ namespace Download {
}
}();
if(game_json.empty()) {
return nullptr;
}
const auto [deck, num_suits] = parse_deck(game_json.at("deck"));
const std::vector<Action> actions = parse_actions(game_json.at("actions"));
const size_t num_players = game_json.at("players").as_array().size();

View file

@ -16,9 +16,14 @@
namespace Hanabi {
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;
auto res = game->evaluate_state();
std::cout.precision(10);
std::cout << std::endl;
std::cout << "Probability with optimal play: " << res << std::endl;
@ -36,30 +41,22 @@ namespace Hanabi {
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) {
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() {
{
auto game = Download::get_game("in/1005195", 43);
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[]) {
#ifndef NDEBUG
test();
#endif
if(argc == 3) {
std::string game(argv[1]);
std::string turn (argv[2]);
std::string game_str(argv[1]);
std::string turn_str (argv[2]);
int turn = 0;
try {
Hanabi::download(std::stoi(game), std::stoi(turn));
turn = std::stoi(turn_str);
} 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]);
if (player_count == 3) {
check_games(3, 10);
} else if (player_count == 4) {
check_games(4, 9);
} else if (player_count == 5) {
check_games(5, 9);
} else if (player_count == 2) {
check_games(2, 10);
try {
Hanabi::download(std::stoi(game_str), turn);
} catch(std::invalid_argument&) {
Hanabi::download(game_str.c_str(), turn);
}
} else {
}
else {
Hanabi::print_usage(argv[0]);
}
return 0;