fix keeping track of card positions

This commit is contained in:
Maximilian Keßler 2023-08-07 11:04:53 +02:00
parent 704913b64c
commit 1484c9e428
Signed by: max
GPG key ID: BCC5A619923C0BA5

View file

@ -214,6 +214,21 @@ namespace Hanabi {
std::uint8_t HanabiState<num_suits, num_players, hand_size>::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;
}
}