From 907fb3ae47e72652c69dd3a2621c944b7b5445f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Fri, 11 Aug 2023 14:39:55 +0200 Subject: [PATCH] refactor. make reverting possible in non-backtracking state --- download.h | 2 +- game_state.h | 10 +++------- game_state.hpp | 7 ++++--- 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/download.h b/download.h index c75b537..1689a38 100644 --- a/download.h +++ b/download.h @@ -140,7 +140,7 @@ namespace Download { return game; } } - game->normalize_draw_and_positions(); + game->init_backtracking_information(); return game; } diff --git a/game_state.h b/game_state.h index e17734f..3613be9 100644 --- a/game_state.h +++ b/game_state.h @@ -153,15 +153,10 @@ enum class ActionType { class HanabiStateIF { public: - virtual probability_t evaluate_state() = 0; - virtual void clue() = 0; virtual void discard(hand_index_t index) = 0; virtual void play(hand_index_t index) = 0; - /** - * May only be used if the relative state is already initialized - */ virtual void revert() = 0; [[nodiscard]] virtual hand_index_t find_card_in_hand(const Card& card) const = 0; @@ -173,7 +168,8 @@ public: [[nodiscard]] virtual std::uint64_t enumerated_states() const = 0; [[nodiscard]] virtual const std::unordered_map& position_tablebase() const = 0; - virtual void normalize_draw_and_positions() = 0; + virtual void init_backtracking_information() = 0; + virtual probability_t evaluate_state() = 0; virtual ~HanabiStateIF() = default; @@ -206,7 +202,7 @@ public: [[nodiscard]] std::uint64_t enumerated_states() const final; [[nodiscard]] const std::unordered_map& position_tablebase() const final; - void normalize_draw_and_positions() final; + void init_backtracking_information() final; auto operator<=>(const HanabiState &) const = default; diff --git a/game_state.hpp b/game_state.hpp index d61b60f..b4f3d82 100644 --- a/game_state.hpp +++ b/game_state.hpp @@ -294,7 +294,7 @@ namespace Hanabi { _draw_pile.push_back({drawn, 1}); } - if (!drawn.initial_trash) { + if (_relative_representation.initialized && !drawn.initial_trash) { ASSERT(drawn.in_starting_hand == false); auto drawn_card_it = std::ranges::find(_relative_representation.card_positions_draw[drawn.local_index], _turn); ASSERT(drawn_card_it != _relative_representation.card_positions_draw[drawn.local_index].end()); @@ -307,7 +307,7 @@ namespace Hanabi { ASSERT(_hands[_turn][index] == discarded_card); } - if (!discarded_card.initial_trash) { + if (_relative_representation.initialized && !discarded_card.initial_trash) { if (discarded_card.in_starting_hand) { ASSERT(_relative_representation.card_positions_hands[discarded_card.local_index] == false); _relative_representation.card_positions_hands[discarded_card.local_index] = true; @@ -323,7 +323,7 @@ namespace Hanabi { } template - void HanabiState::normalize_draw_and_positions() { + void HanabiState::init_backtracking_information() { // Note that this function does not have to be particularly performant, we only call it once to initialize. const Card trash = [this]() -> Card { for (suit_t suit = 0; suit < num_suits; suit++) { @@ -454,6 +454,7 @@ namespace Hanabi { template probability_t HanabiState::evaluate_state() { + ASSERT(_relative_representation.initialized); _enumerated_states++; const unsigned long id_of_state = unique_id();