From 1484c9e428ee31e1a482422349816e72913cba4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Mon, 7 Aug 2023 11:04:53 +0200 Subject: [PATCH] fix keeping track of card positions --- game_state.hpp | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/game_state.hpp b/game_state.hpp index 298ab2c..0d79594 100644 --- a/game_state.hpp +++ b/game_state.hpp @@ -214,6 +214,21 @@ namespace Hanabi { std::uint8_t HanabiState::draw(uint8_t index) { ASSERT(index < _hands[_turn].size()); + // update card position of the card we are about to discard + if constexpr(update_card_positions) { + const Card discarded = _hands[_turn][index]; + if (!discarded.initial_trash) { + if (discarded.was_in_initial_hand) { + ASSERT(_card_positions_hands[_turn][index] == true); + _card_positions_hands[_turn][index] = false; + } else { + auto replaced_card_it = std::ranges::find(_card_positions_draw[discarded], _turn); + ASSERT(replaced_card_it != _card_positions_draw[discarded].end()); + *replaced_card_it = trash_or_play_stack; + } + } + } + // draw a new card if the draw pile is not empty if (!_draw_pile.empty()) { --_weighted_draw_pile_size; @@ -228,18 +243,7 @@ namespace Hanabi { } if constexpr(update_card_positions) { - const Card discarded = _hands[_turn][index]; - if (!discarded.initial_trash) { - if (discarded.was_in_initial_hand) { - ASSERT(_card_positions_hands[_turn][index] == true); - _card_positions_hands[_turn][index] = false; - } else { - auto replaced_card_it = std::ranges::find(_card_positions_draw[discarded], _turn); - ASSERT(replaced_card_it != _card_positions_draw[discarded].end()); - *replaced_card_it = trash_or_play_stack; - } - } - + // update card position of the drawn card if (!draw.card.initial_trash) { auto new_card_it = std::ranges::find(_card_positions_draw[draw.card], draw_pile); ASSERT(new_card_it != _card_positions_draw[draw.card].end()); @@ -281,6 +285,8 @@ namespace Hanabi { _weighted_draw_pile_size++; _endgame_turns_left = no_endgame; + } else { + ASSERT(_hands[_turn][index] == discarded_card); } if (!discarded_card.initial_trash) { @@ -290,6 +296,7 @@ namespace Hanabi { } else { auto hand_card_it = std::ranges::find(_card_positions_draw[discarded_card], trash_or_play_stack); ASSERT(hand_card_it != _card_positions_draw[discarded_card].end()); + *hand_card_it = _turn; } }