1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 06:47:35 +00:00

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
3 changed files with 4 additions and 18 deletions

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)