Hearts: Prefer to pass high value cards

Previously we'd prefer to pass high points cards. Instead we should
prefer to pass high value cards first.
This commit is contained in:
Gunnar Beutner 2021-06-01 00:45:26 +02:00 committed by Andreas Kling
parent 8b9da08d5a
commit 63d3beb78c
Notes: sideshowbarker 2024-07-18 17:05:18 +09:00
2 changed files with 19 additions and 11 deletions

View file

@ -11,9 +11,23 @@
namespace Hearts {
static bool compare_card_value(CardWithIndex& cwi1, CardWithIndex& cwi2)
{
return hearts_card_value(*cwi2.card) < hearts_card_value(*cwi1.card);
}
static bool compare_card_points_and_value(CardWithIndex& cwi1, CardWithIndex& cwi2)
{
if (hearts_card_points(*cwi2.card) < hearts_card_points(*cwi1.card))
return true;
if (hearts_card_points(*cwi1.card) == hearts_card_points(*cwi2.card) && hearts_card_value(*cwi2.card) < hearts_card_value(*cwi1.card))
return true;
return false;
}
NonnullRefPtrVector<Card> Player::pick_cards_to_pass(PassingDirection)
{
auto sorted_hand = hand_sorted_by_points_and_value();
auto sorted_hand = hand_sorted_by_fn(compare_card_value);
NonnullRefPtrVector<Card> cards;
cards.append(*sorted_hand[0].card);
cards.append(*sorted_hand[1].card);
@ -21,7 +35,7 @@ NonnullRefPtrVector<Card> Player::pick_cards_to_pass(PassingDirection)
return cards;
}
Vector<CardWithIndex> Player::hand_sorted_by_points_and_value() const
Vector<CardWithIndex> Player::hand_sorted_by_fn(bool (*fn)(CardWithIndex&, CardWithIndex&)) const
{
Vector<CardWithIndex> sorted_hand;
for (size_t i = 0; i < hand.size(); i++) {
@ -29,19 +43,13 @@ Vector<CardWithIndex> Player::hand_sorted_by_points_and_value() const
if (card)
sorted_hand.empend(*card, i);
}
quick_sort(sorted_hand, [](auto& cwi1, auto& cwi2) {
if (hearts_card_points(*cwi2.card) < hearts_card_points(*cwi1.card))
return true;
if (hearts_card_points(*cwi1.card) == hearts_card_points(*cwi2.card) && hearts_card_value(*cwi2.card) < hearts_card_value(*cwi1.card))
return true;
return false;
});
quick_sort(sorted_hand, fn);
return sorted_hand;
}
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();
auto sorted_hand = hand_sorted_by_fn(compare_card_points_and_value);
if constexpr (HEARTS_DEBUG) {
dbgln("Sorted hand:");

View file

@ -42,7 +42,7 @@ public:
Optional<size_t> pick_specific_card(Card::Type type, CardValue value);
size_t pick_last_card();
bool has_card_of_type(Card::Type type);
Vector<CardWithIndex> hand_sorted_by_points_and_value() const;
Vector<CardWithIndex> hand_sorted_by_fn(bool (*)(CardWithIndex&, CardWithIndex&)) const;
void sort_hand() { quick_sort(hand, hearts_card_less); }
void remove_cards(NonnullRefPtrVector<Card> const& cards);