From 87ff267f800fc72a0859a529070a9aa0dab3c037 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Sun, 6 Aug 2023 17:34:07 +0200 Subject: [PATCH] fix clue count when playing 5s on 8 clues --- game_state.h | 2 +- game_state.hpp | 16 +++++++++------- main.cpp | 2 +- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/game_state.h b/game_state.h index 424e09b..15c31e9 100644 --- a/game_state.h +++ b/game_state.h @@ -162,7 +162,7 @@ public: void normalize_draw_and_positions(); void revert_clue(); - void revert_play(const BacktrackAction &action); + void revert_play(const BacktrackAction &action, bool was_on_8_clues); void revert_discard(const BacktrackAction &action); uint8_t draw(uint8_t index); diff --git a/game_state.hpp b/game_state.hpp index 1b05326..1f52c37 100644 --- a/game_state.hpp +++ b/game_state.hpp @@ -128,7 +128,7 @@ namespace Hanabi { BacktrackAction ret{_hands[_turn][index], index, 0}; - if (card.rank == 0) { + if (card.rank == 0 and _num_clues < max_num_clues) { // update clues if we played the last card of a stack _num_clues++; } @@ -301,9 +301,10 @@ namespace Hanabi { } template - void HanabiState::revert_play(const BacktrackAction& action) { + void HanabiState::revert_play(const BacktrackAction& action, bool was_on_8_clues) { + ASSERT(!was_on_8_clues or _num_clues == 8); decr_turn(); - if (action.discarded.rank == 0) { + if (action.discarded.rank == 0 and not was_on_8_clues) { _num_clues--; } revert_draw(action.index, action.discarded); @@ -353,19 +354,20 @@ namespace Hanabi { for(std::uint8_t index = 0; index < hand_size; index++) { if(is_playable(hand[index])) { if (_draw_pile.empty()) { - auto copy = *this; + bool on_8_clues = _num_clues == 8; BacktrackAction action = play(index); const double probability_for_this_play = backtrack(depth + 1); - revert_play(action); + revert_play(action, on_8_clues); UPDATE_PROBABILITY(probability_for_this_play); } else { double sum_of_probabilities = 0; uint8_t sum_of_mults = 0; for (size_t i = 0; i < _draw_pile.size(); i++) { + bool on_8_clues = _num_clues == 8; BacktrackAction action = play(index); sum_of_probabilities += backtrack(depth + 1) * action.multiplicity; sum_of_mults += action.multiplicity; - revert_play(action); + revert_play(action, on_8_clues); ASSERT(sum_of_mults <= _weighted_draw_pile_size); } ASSERT(sum_of_mults == _weighted_draw_pile_size); @@ -376,7 +378,7 @@ namespace Hanabi { } // Check for discards now - if(_pace > 0) { + if(_pace > 0 and _num_clues < max_num_clues) { for(std::uint8_t index = 0; index < hand_size; index++) { if (is_trash(hand[index])) { double sum_of_probabilities = 0; diff --git a/main.cpp b/main.cpp index d350a12..fd143ca 100644 --- a/main.cpp +++ b/main.cpp @@ -30,7 +30,7 @@ void test_game() { auto a = state.play(4); std::cout << state; - state.revert_play(a); + state.revert_play(a, false); std::cout << state << std::endl; std::cout << state2 << std::endl;