From 228eac7af1c9abcb84184a36128ea10e62c41d89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Mon, 7 Aug 2023 00:06:50 +0200 Subject: [PATCH] support striking of cards --- download.h | 2 ++ game_state.h | 2 ++ game_state.hpp | 20 +++++++++++++++----- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/download.h b/download.h index 668c314..e9d49d9 100644 --- a/download.h +++ b/download.h @@ -113,6 +113,8 @@ namespace Download { const std::vector& actions, size_t num_turns_to_replicate ) { + + std::cout << "Initialising game with " << +num_players << " players, " << +num_suits << " suits and hand size " << +hand_size << std::endl; auto game = std::unique_ptr(new Hanabi::HanabiState(deck)); std::uint8_t index; for (size_t i = 0; i < num_turns_to_replicate; i++) { diff --git a/game_state.h b/game_state.h index fa42c28..a6f5abd 100644 --- a/game_state.h +++ b/game_state.h @@ -177,6 +177,8 @@ protected: void print(std::ostream& os) const final; private: + BacktrackAction play_no_strike(hand_index_t index); + hand_index_t draw(hand_index_t index); void revert_draw(hand_index_t index, Card discarded_card); diff --git a/game_state.hpp b/game_state.hpp index 8375a76..9320866 100644 --- a/game_state.hpp +++ b/game_state.hpp @@ -119,11 +119,21 @@ namespace Hanabi { } template - BacktrackAction HanabiState::play( - std::uint8_t index) { + BacktrackAction HanabiState::play(Hanabi::hand_index_t index) { + const Card card = _hands[_turn][index]; + if (!is_playable(card)) { + BacktrackAction ret{card, index, draw(index)}; + incr_turn(); + return ret; + } + return play_no_strike(index); + } + + template + BacktrackAction HanabiState::play_no_strike(Hanabi::hand_index_t index) { ASSERT(index < _hands[_turn].size()); const Card card = _hands[_turn][index]; - ASSERT(card.rank == _stacks[card.suit] - 1); + ASSERT(is_playable(card)); --_stacks[card.suit]; _score++; @@ -323,7 +333,7 @@ namespace Hanabi { if(is_playable(hand[index])) { if (_draw_pile.empty()) { bool on_8_clues = _num_clues == 8; - BacktrackAction action = play(index); + BacktrackAction action = play_no_strike(index); const double probability_for_this_play = backtrack(depth + 1); revert_play(action, on_8_clues); UPDATE_PROBABILITY(probability_for_this_play); @@ -332,7 +342,7 @@ namespace Hanabi { 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); + BacktrackAction action = play_no_strike(index); sum_of_probabilities += backtrack(depth + 1) * action.multiplicity; sum_of_mults += action.multiplicity; revert_play(action, on_8_clues);