From 1c716ffeabaceb49a7cd1a5fbf7c7550fd94e3b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Sat, 12 Aug 2023 12:16:23 +0200 Subject: [PATCH] remove unused methods, put into own files --- src/check_games.cpp | 51 +++++++++++++++++++++++++++++++++++ src/main.cpp | 65 +++------------------------------------------ src/test.cpp | 15 +++++++++++ 3 files changed, 69 insertions(+), 62 deletions(-) create mode 100644 src/check_games.cpp create mode 100644 src/test.cpp diff --git a/src/check_games.cpp b/src/check_games.cpp new file mode 100644 index 0000000..cde1d4d --- /dev/null +++ b/src/check_games.cpp @@ -0,0 +1,51 @@ +#include + +#include "game_state.h" +#include "download.h" + +void check_games(unsigned num_players, unsigned max_draw_pile_size, unsigned first_game = 0, unsigned last_game = 9999) { + std::vector> winning_percentages(last_game + 2); + + for(size_t draw_pile_size = 0; draw_pile_size <= max_draw_pile_size; draw_pile_size++) { + Hanabi::probability_t total_chance = 0; + const std::string output_fname = "games_" + std::to_string(num_players) + "p_draw_size_" + std::to_string(draw_pile_size) + ".txt"; + std::ofstream file (output_fname); + for(size_t game_id = first_game; game_id <= last_game; game_id++) { + const std::string input_fname = "json/" + std::to_string(num_players) + "p/" + std::to_string(game_id) + ".json"; + auto game = Download::get_game(input_fname.c_str(), 50, draw_pile_size); + const Hanabi::probability_t chance = game->evaluate_state(); + winning_percentages[game_id].push_back(chance); + if(chance != 1) { + file << "Game " << game_id << ": " << chance << std::endl; + file << *game << std::endl << std::endl; + } + std::cout << "Finished game " << game_id << " with draw pile size " << draw_pile_size << ": " << chance << std::endl; + + total_chance += chance; + } + const Hanabi::probability_t total_average = total_chance / (last_game - first_game + 1); + winning_percentages.back().push_back(total_average); + file << "Total chance found over " << last_game - first_game + 1 << " many games: " << total_average << std::endl; + file.close(); + } + const std::string results_file_name {"results_" + std::to_string(num_players) + "p.txt"}; + std::ofstream results_file (results_file_name); + results_file << "game_id, "; + for(size_t draw_pile_size = 0; draw_pile_size <= max_draw_pile_size; draw_pile_size++) { + results_file << std::to_string(draw_pile_size) << ", "; + } + results_file << "\n"; + for(size_t game_id = first_game; game_id <= last_game; game_id++) { + results_file << game_id << ", "; + for(size_t draw_pile_size = 0; draw_pile_size <= max_draw_pile_size; draw_pile_size++) { + results_file << winning_percentages[game_id][draw_pile_size] << ", "; + } + results_file << std::endl; + } + results_file << "total, "; + for(size_t draw_pile_size = 0; draw_pile_size <= max_draw_pile_size; draw_pile_size++) { + results_file << winning_percentages.back()[draw_pile_size] << ", "; + } + results_file << std::endl; + results_file.close(); +} diff --git a/src/main.cpp b/src/main.cpp index a22017b..b83feeb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -15,7 +15,7 @@ namespace Hanabi { - void download(std::variant game_id, int turn) { + void analyze_game_and_start_cli(std::variant game_id, int turn) { auto game = Download::get_game(game_id, turn - 1); if (game == nullptr) { return; @@ -47,66 +47,7 @@ namespace Hanabi { } } -void test() { - { - auto game = Download::get_game("in/1005195", 43); - auto res = game->evaluate_state(); - if (!(res == Hanabi::probability_t(7, 8))) { - std::cerr << "Test " << ("1005195") << " failed." << std::endl; - } - else { - std::cout << "Test " << ("1005195") << " succeeded." << std::endl; - }; - } -} -void check_games(unsigned num_players, unsigned max_draw_pile_size, unsigned first_game = 0, unsigned last_game = 9999) { - - std::vector> winning_percentages(last_game + 2); - - for(size_t draw_pile_size = 0; draw_pile_size <= max_draw_pile_size; draw_pile_size++) { - Hanabi::probability_t total_chance = 0; - const std::string output_fname = "games_" + std::to_string(num_players) + "p_draw_size_" + std::to_string(draw_pile_size) + ".txt"; - std::ofstream file (output_fname); - for(size_t game_id = first_game; game_id <= last_game; game_id++) { - const std::string input_fname = "json/" + std::to_string(num_players) + "p/" + std::to_string(game_id) + ".json"; - auto game = Download::get_game(input_fname.c_str(), 50, draw_pile_size); - const Hanabi::probability_t chance = game->evaluate_state(); - winning_percentages[game_id].push_back(chance); - if(chance != 1) { - file << "Game " << game_id << ": " << chance << std::endl; - file << *game << std::endl << std::endl; - } - std::cout << "Finished game " << game_id << " with draw pile size " << draw_pile_size << ": " << chance << std::endl; - - total_chance += chance; - } - const Hanabi::probability_t total_average = total_chance / (last_game - first_game + 1); - winning_percentages.back().push_back(total_average); - file << "Total chance found over " << last_game - first_game + 1 << " many games: " << total_average << std::endl; - file.close(); - } - const std::string results_file_name {"results_" + std::to_string(num_players) + "p.txt"}; - std::ofstream results_file (results_file_name); - results_file << "game_id, "; - for(size_t draw_pile_size = 0; draw_pile_size <= max_draw_pile_size; draw_pile_size++) { - results_file << std::to_string(draw_pile_size) << ", "; - } - results_file << "\n"; - for(size_t game_id = first_game; game_id <= last_game; game_id++) { - results_file << game_id << ", "; - for(size_t draw_pile_size = 0; draw_pile_size <= max_draw_pile_size; draw_pile_size++) { - results_file << winning_percentages[game_id][draw_pile_size] << ", "; - } - results_file << std::endl; - } - results_file << "total, "; - for(size_t draw_pile_size = 0; draw_pile_size <= max_draw_pile_size; draw_pile_size++) { - results_file << winning_percentages.back()[draw_pile_size] << ", "; - } - results_file << std::endl; - results_file.close(); -} int main(int argc, char *argv[]) { if(argc == 3) { @@ -122,9 +63,9 @@ int main(int argc, char *argv[]) { } try { - Hanabi::download(std::stoi(game_str), turn); + Hanabi::analyze_game_and_start_cli(std::stoi(game_str), turn); } catch(std::invalid_argument&) { - Hanabi::download(game_str.c_str(), turn); + Hanabi::analyze_game_and_start_cli(game_str.c_str(), turn); } } else { diff --git a/src/test.cpp b/src/test.cpp new file mode 100644 index 0000000..175e2df --- /dev/null +++ b/src/test.cpp @@ -0,0 +1,15 @@ +#include "download.h" +#include "game_state.h" + +void test() { + { + auto game = Download::get_game("in/1005195", 43); + auto res = game->evaluate_state(); + if (!(res == Hanabi::probability_t(7, 8))) { + std::cerr << "Test " << ("1005195") << " failed." << std::endl; + } + else { + std::cout << "Test " << ("1005195") << " succeeded." << std::endl; + }; + } +}