simplify code

This commit is contained in:
Maximilian Keßler 2024-01-12 18:05:15 +01:00
parent 3c1fa0d0e4
commit 4f1e41a80a
Signed by: max
GPG key ID: BCC5A619923C0BA5
2 changed files with 18 additions and 28 deletions

View file

@ -219,6 +219,8 @@ namespace Hanabi
void check_draw_pile_integrity() const; void check_draw_pile_integrity() const;
probability_t check_play_or_discard(hand_index_t index, bool play);
static constexpr uint8_t no_endgame = std::numeric_limits<uint8_t>::max(); static constexpr uint8_t no_endgame = std::numeric_limits<uint8_t>::max();
// Usual game state // Usual game state

View file

@ -940,14 +940,7 @@ namespace Hanabi
{ {
if (is_playable(hand[index])) if (is_playable(hand[index]))
{ {
probability_t sum_of_probabilities = 0; probability_t const probability_play = check_play_or_discard(index, true);
do_for_each_potential_draw(index, true, [this, &sum_of_probabilities](const unsigned long multiplicity) {
sum_of_probabilities += evaluate_state() * multiplicity;
});
const unsigned long total_weight = std::max(static_cast<unsigned long>(_weighted_draw_pile_size), 1ul);
const probability_t probability_play = sum_of_probabilities / total_weight;
best_probability = std::max(best_probability, probability_play); best_probability = std::max(best_probability, probability_play);
if (best_probability == 1) if (best_probability == 1)
@ -994,15 +987,7 @@ namespace Hanabi
// Discard if we found trash now // Discard if we found trash now
if (discard_index != invalid_index) { if (discard_index != invalid_index) {
probability_t sum_of_probabilities = 0; probability_t const probability_discard = check_play_or_discard(discard_index, false);
do_for_each_potential_draw(discard_index, false, [this, &sum_of_probabilities](const unsigned long multiplicity) {
sum_of_probabilities += evaluate_state() * multiplicity;
});
const unsigned long total_weight = std::max(static_cast<unsigned long>(_weighted_draw_pile_size), 1ul);
const probability_t probability_discard = sum_of_probabilities / total_weight;
best_probability = std::max(best_probability, probability_discard);
best_probability = std::max(best_probability, probability_discard); best_probability = std::max(best_probability, probability_discard);
if (best_probability == 1) if (best_probability == 1)
@ -1015,18 +1000,9 @@ namespace Hanabi
// sacrifice cards in hand // sacrifice cards in hand
for(hand_index_t index = 0; index < hand_size; ++index) { for(hand_index_t index = 0; index < hand_size; ++index) {
if(!is_critical(hand[index])) { if(!is_critical(hand[index])) {
// consider discarding this probability_t const probability_sacrifice = check_play_or_discard(index, false);
probability_t sum_of_probabilities = 0;
do_for_each_potential_draw(index, false, [this, &sum_of_probabilities](const unsigned long multiplicity) { best_probability = std::max(best_probability, probability_sacrifice);
sum_of_probabilities += evaluate_state() * multiplicity;
});
const unsigned long total_weight = std::max(static_cast<unsigned long>(_weighted_draw_pile_size), 1ul);
const probability_t probability_discard = sum_of_probabilities / total_weight;
best_probability = std::max(best_probability, probability_discard);
best_probability = std::max(best_probability, probability_discard);
if (best_probability == 1) if (best_probability == 1)
{ {
update_tablebase(id_of_state, best_probability); update_tablebase(id_of_state, best_probability);
@ -1055,6 +1031,18 @@ namespace Hanabi
return best_probability; return best_probability;
} }
template<suit_t num_suits, player_t num_players, hand_index_t hand_size>
probability_t HanabiState<num_suits, num_players, hand_size>::check_play_or_discard(hand_index_t index, bool play) {
probability_t sum_of_probabilities = 0;
do_for_each_potential_draw(index, play, [this, &sum_of_probabilities](const unsigned long multiplicity) {
sum_of_probabilities += evaluate_state() * multiplicity;
});
const unsigned long total_weight = std::max(static_cast<unsigned long>(_weighted_draw_pile_size), 1ul);
return sum_of_probabilities / total_weight;
}
template<suit_t num_suits, player_t num_players, hand_index_t hand_size> template<suit_t num_suits, player_t num_players, hand_index_t hand_size>
template<class Function> template<class Function>
void void