From 59709e23755cc1c76bf7b3fb0dd753ed5e0d3b7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Sat, 11 Nov 2023 11:44:06 +0100 Subject: [PATCH] rework -r option: solve state in reverse order --- include/game_state.h | 2 ++ include/game_state.hpp | 7 +++++++ src/main.cpp | 24 +++++++++++++++--------- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/include/game_state.h b/include/game_state.h index 4b4aa95..b9ee2a5 100644 --- a/include/game_state.h +++ b/include/game_state.h @@ -218,6 +218,7 @@ public: virtual void play(hand_index_t index) = 0; virtual void rotate_next_draw(const Card& card) = 0; + virtual ActionType last_action_type() const = 0; virtual void revert() = 0; virtual void modify_clues(clue_t change) = 0; @@ -283,6 +284,7 @@ public: void play(hand_index_t index) final; void rotate_next_draw(const Card& card) final; + ActionType last_action_type() const final; void revert() final; diff --git a/include/game_state.hpp b/include/game_state.hpp index e063bcc..9dbded4 100644 --- a/include/game_state.hpp +++ b/include/game_state.hpp @@ -678,6 +678,13 @@ namespace Hanabi { std::swap(*card_it, _draw_pile.front()); } + template + ActionType HanabiState::last_action_type() const + { + assert(not _actions_log.empty()); + return _actions_log.top().action_type; + } + template probability_t HanabiState::evaluate_state() { ASSERT(_relative_representation.initialized); diff --git a/src/main.cpp b/src/main.cpp index b4374ad..5694ee7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -36,13 +36,17 @@ namespace Hanabi { } game.state->init_backtracking_information(); auto start = std::chrono::high_resolution_clock::now(); - auto res = game.state->evaluate_state(); + boost::rational result; + if (not print_remaining_states) { + result = game.state->evaluate_state(); + } auto end = std::chrono::high_resolution_clock::now(); std::cout.precision(10); - if (not (quiet and print_remaining_states)) { + + if (not print_remaining_states) { std::cout << "Probability with optimal play: "; - print_probability(std::cout, res) << std::endl; + print_probability(std::cout, result) << std::endl; } if (not quiet) { std::cout << "Took " << std::chrono::duration_cast(end - start) << "." << std::endl; @@ -52,22 +56,24 @@ namespace Hanabi { if (print_remaining_states) { - for(size_t remaining_cards = game.state->draw_pile_size(); remaining_cards > 0; remaining_cards--) { - game.forward_until(100, remaining_cards); + size_t const max_draw_pile_size = game.state->draw_pile_size(); + game.forward_until(100, 1); + for(size_t remaining_cards = 1; remaining_cards <= max_draw_pile_size; remaining_cards++) { + game.revert_until(remaining_cards); if (all_clues) { clue_t original_num_clues = game.state->num_clues(); for(clue_t num_clues = 0; num_clues <= 8; num_clues++) { game.state->set_clues(num_clues); - res = game.state->evaluate_state(); + result = game.state->evaluate_state(); std::cout << "Probability with " << remaining_cards << " cards left in deck and " << +num_clues << " clues (" << std::showpos << +(num_clues - original_num_clues) << "): " << std::noshowpos; - print_probability(std::cout, res) << std::endl; + print_probability(std::cout, result) << std::endl; } game.state->set_clues(original_num_clues); } else { - res = game.state->evaluate_state(); + result = game.state->evaluate_state(); std::cout << "Probability with " << remaining_cards << " cards left in deck: "; - print_probability(std::cout, res) << std::endl; + print_probability(std::cout, result) << std::endl; } } }