Hearts: Pick better lead cards

Previously the AI would prefer playing a lead card for which no other
player had a card with a higher value even though it also had a card
for which a higher value card was still in play.
This commit is contained in:
Gunnar Beutner 2021-05-31 23:56:13 +02:00 committed by Andreas Kling
parent 40ddb734ee
commit 38f8a6aabb
Notes: sideshowbarker 2024-07-18 17:05:32 +09:00
3 changed files with 4 additions and 18 deletions

View file

@ -327,10 +327,7 @@ size_t Game::pick_card(Player& player)
auto prefer_card = [this, &player](Card& card) {
return !other_player_has_lower_value_card(player, card) && other_player_has_higher_value_card(player, card);
};
auto lower_value_card_in_play = [this, &player](Card& card) {
return other_player_has_lower_value_card(player, card);
};
return player.pick_lead_card(move(valid_card), move(prefer_card), move(lower_value_card_in_play));
return player.pick_lead_card(move(valid_card), move(prefer_card));
}
}
auto* high_card = &m_trick[0];

View file

@ -39,8 +39,7 @@ Vector<CardWithIndex> Player::hand_sorted_by_points_and_value() const
return sorted_hand;
}
size_t Player::pick_lead_card(Function<bool(Card&)> valid_play, Function<bool(Card&)> prefer_card,
Function<bool(Card&)> lower_value_card_in_play)
size_t Player::pick_lead_card(Function<bool(Card&)> valid_play, Function<bool(Card&)> prefer_card)
{
auto sorted_hand = hand_sorted_by_points_and_value();
@ -51,27 +50,17 @@ size_t Player::pick_lead_card(Function<bool(Card&)> valid_play, Function<bool(Ca
dbgln("----");
}
ssize_t fallback_index = -1;
ssize_t last_index = -1;
for (auto& cwi : sorted_hand) {
if (!valid_play(*cwi.card))
continue;
if (lower_value_card_in_play(*cwi.card)) {
// Allow falling back to this card if no other suitable card is in play.
fallback_index = cwi.index;
continue;
}
if (prefer_card(*cwi.card)) {
dbgln_if(HEARTS_DEBUG, "Preferring card {}", *cwi.card);
return cwi.index;
}
last_index = cwi.index;
}
if (last_index == -1) {
dbgln_if(HEARTS_DEBUG, "Falling back to card {}", *hand[fallback_index]);
return fallback_index;
} else
return last_index;
return last_index;
}
Optional<size_t> Player::pick_low_points_high_value_card(Optional<Card::Type> type)

View file

@ -34,7 +34,7 @@ public:
}
NonnullRefPtrVector<Card> pick_cards_to_pass(PassingDirection);
size_t pick_lead_card(Function<bool(Card&)>, Function<bool(Card&)>, Function<bool(Card&)>);
size_t pick_lead_card(Function<bool(Card&)>, Function<bool(Card&)>);
Optional<size_t> pick_low_points_high_value_card(Optional<Card::Type> type = {});
Optional<size_t> pick_lower_value_card(Card& other_card);
Optional<size_t> pick_slightly_higher_value_card(Card& other_card);