From 89e6e02603a02505dea3caeb61215c6093e01667 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Sun, 6 Aug 2023 12:49:52 +0200 Subject: [PATCH] fix bug on not doing stuff in extraround --- download.h | 1 - game_state.hpp | 42 ++++++++++++++++++++++++++++-------------- main.cpp | 2 +- 3 files changed, 29 insertions(+), 16 deletions(-) diff --git a/download.h b/download.h index 72e13a0..f20d034 100644 --- a/download.h +++ b/download.h @@ -109,7 +109,6 @@ namespace Download { size_t num_turns_to_replicate ) { Hanabi::HanabiState game(deck); - std::cout << game << std::endl; std::uint8_t index; for (size_t i = 0; i < num_turns_to_replicate; i++) { switch(actions[i].type) { diff --git a/game_state.hpp b/game_state.hpp index 4929523..ea0885d 100644 --- a/game_state.hpp +++ b/game_state.hpp @@ -170,7 +170,7 @@ namespace Hanabi { template std::ostream &operator<<(std::ostream &os, const HanabiState hanabi_state) { os << "Stacks: " << hanabi_state._stacks << " (score " << +hanabi_state._score << ")"; - os << ", clues: " << +hanabi_state._num_clues << std::endl; + os << ", clues: " << +hanabi_state._num_clues << ", turn: " << +hanabi_state._turn << std::endl; os << "Draw pile: "; for (const auto &[card, mul]: hanabi_state._draw_pile) { os << card; @@ -349,18 +349,25 @@ namespace Hanabi { // First, check for playables for(std::uint8_t index = 0; index < hand_size; index++) { if(is_playable(hand[index])) { - double sum_of_probabilities = 0; - uint8_t sum_of_mults = 0; - for(size_t i = 0; i < _draw_pile.size(); i++) { + if (_draw_pile.empty()) { BacktrackAction action = play(index); - sum_of_probabilities += backtrack() * action.multiplicity; - sum_of_mults += action.multiplicity; + const double probability_for_this_play = backtrack(); revert(action); - assert(sum_of_mults <= _weighted_draw_pile_size); + 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++) { + BacktrackAction action = play(index); + sum_of_probabilities += backtrack() * action.multiplicity; + sum_of_mults += action.multiplicity; + revert(action); + assert(sum_of_mults <= _weighted_draw_pile_size); + } + assert(sum_of_mults == _weighted_draw_pile_size); + const double probability_for_this_play = sum_of_probabilities / _weighted_draw_pile_size; + UPDATE_PROBABILITY(probability_for_this_play); } - assert(sum_of_mults == _weighted_draw_pile_size); - const double probability_for_this_play = sum_of_probabilities / _weighted_draw_pile_size; - UPDATE_PROBABILITY(probability_for_this_play); } } @@ -369,13 +376,20 @@ namespace Hanabi { for(std::uint8_t index = 0; index < hand_size; index++) { if (is_trash(hand[index])) { double sum_of_probabilities = 0; - for(size_t i = 0; i < _draw_pile.size(); i++) { + if (_draw_pile.empty()) { BacktrackAction action = discard(index); - sum_of_probabilities += backtrack() * action.multiplicity; + const double probability_for_this_discard = backtrack(); revert(action); + UPDATE_PROBABILITY(probability_for_this_discard); + } else { + for (size_t i = 0; i < _draw_pile.size(); i++) { + BacktrackAction action = discard(index); + sum_of_probabilities += backtrack() * action.multiplicity; + revert(action); + } + const double probability_discard = sum_of_probabilities / _weighted_draw_pile_size; + UPDATE_PROBABILITY(probability_discard); } - const double probability_discard = sum_of_probabilities / _weighted_draw_pile_size; - UPDATE_PROBABILITY(probability_discard); // All discards are equivalent, do not continue searching for different trash break; diff --git a/main.cpp b/main.cpp index e4f72a2..85be21a 100644 --- a/main.cpp +++ b/main.cpp @@ -39,7 +39,7 @@ void test_game() { } void download() { - auto game = Download::get_game<6,3,5>("1004116.json", 40); + auto game = Download::get_game<4,3,5>("1004480.json", 36); std::cout << game << std::endl; auto res = game.backtrack(); std::cout << res << std::endl;