refactor. make reverting possible in non-backtracking state

This commit is contained in:
Maximilian Keßler 2023-08-11 14:39:55 +02:00
parent ec203198c0
commit 907fb3ae47
Signed by: max
GPG key ID: BCC5A619923C0BA5
3 changed files with 8 additions and 11 deletions

View file

@ -140,7 +140,7 @@ namespace Download {
return game; return game;
} }
} }
game->normalize_draw_and_positions(); game->init_backtracking_information();
return game; return game;
} }

View file

@ -153,15 +153,10 @@ enum class ActionType {
class HanabiStateIF { class HanabiStateIF {
public: public:
virtual probability_t evaluate_state() = 0;
virtual void clue() = 0; virtual void clue() = 0;
virtual void discard(hand_index_t index) = 0; virtual void discard(hand_index_t index) = 0;
virtual void play(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; virtual void revert() = 0;
[[nodiscard]] virtual hand_index_t find_card_in_hand(const Card& card) const = 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 std::uint64_t enumerated_states() const = 0;
[[nodiscard]] virtual const std::unordered_map<unsigned long, probability_t>& position_tablebase() const = 0; [[nodiscard]] virtual const std::unordered_map<unsigned long, probability_t>& 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; virtual ~HanabiStateIF() = default;
@ -206,7 +202,7 @@ public:
[[nodiscard]] std::uint64_t enumerated_states() const final; [[nodiscard]] std::uint64_t enumerated_states() const final;
[[nodiscard]] const std::unordered_map<unsigned long, probability_t>& position_tablebase() const final; [[nodiscard]] const std::unordered_map<unsigned long, probability_t>& position_tablebase() const final;
void normalize_draw_and_positions() final; void init_backtracking_information() final;
auto operator<=>(const HanabiState &) const = default; auto operator<=>(const HanabiState &) const = default;

View file

@ -294,7 +294,7 @@ namespace Hanabi {
_draw_pile.push_back({drawn, 1}); _draw_pile.push_back({drawn, 1});
} }
if (!drawn.initial_trash) { if (_relative_representation.initialized && !drawn.initial_trash) {
ASSERT(drawn.in_starting_hand == false); ASSERT(drawn.in_starting_hand == false);
auto drawn_card_it = std::ranges::find(_relative_representation.card_positions_draw[drawn.local_index], _turn); 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()); 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); 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) { if (discarded_card.in_starting_hand) {
ASSERT(_relative_representation.card_positions_hands[discarded_card.local_index] == false); ASSERT(_relative_representation.card_positions_hands[discarded_card.local_index] == false);
_relative_representation.card_positions_hands[discarded_card.local_index] = true; _relative_representation.card_positions_hands[discarded_card.local_index] = true;
@ -323,7 +323,7 @@ namespace Hanabi {
} }
template<suit_t num_suits, player_t num_players, hand_index_t hand_size> template<suit_t num_suits, player_t num_players, hand_index_t hand_size>
void HanabiState<num_suits, num_players, hand_size>::normalize_draw_and_positions() { void HanabiState<num_suits, num_players, hand_size>::init_backtracking_information() {
// Note that this function does not have to be particularly performant, we only call it once to initialize. // Note that this function does not have to be particularly performant, we only call it once to initialize.
const Card trash = [this]() -> Card { const Card trash = [this]() -> Card {
for (suit_t suit = 0; suit < num_suits; suit++) { for (suit_t suit = 0; suit < num_suits; suit++) {
@ -454,6 +454,7 @@ namespace Hanabi {
template<suit_t num_suits, player_t num_players, hand_index_t hand_size> template<suit_t num_suits, player_t num_players, hand_index_t hand_size>
probability_t HanabiState<num_suits, num_players, hand_size>::evaluate_state() { probability_t HanabiState<num_suits, num_players, hand_size>::evaluate_state() {
ASSERT(_relative_representation.initialized);
_enumerated_states++; _enumerated_states++;
const unsigned long id_of_state = unique_id(); const unsigned long id_of_state = unique_id();