From 63d3beb78cb28edf52ed8b3da04450d811093d70 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Tue, 1 Jun 2021 00:45:26 +0200 Subject: [PATCH] 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. --- Userland/Games/Hearts/Player.cpp | 28 ++++++++++++++++++---------- Userland/Games/Hearts/Player.h | 2 +- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/Userland/Games/Hearts/Player.cpp b/Userland/Games/Hearts/Player.cpp index d8eecf7e4e..d304c5c94d 100644 --- a/Userland/Games/Hearts/Player.cpp +++ b/Userland/Games/Hearts/Player.cpp @@ -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 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 cards; cards.append(*sorted_hand[0].card); cards.append(*sorted_hand[1].card); @@ -21,7 +35,7 @@ NonnullRefPtrVector Player::pick_cards_to_pass(PassingDirection) return cards; } -Vector Player::hand_sorted_by_points_and_value() const +Vector Player::hand_sorted_by_fn(bool (*fn)(CardWithIndex&, CardWithIndex&)) const { Vector sorted_hand; for (size_t i = 0; i < hand.size(); i++) { @@ -29,19 +43,13 @@ Vector 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 valid_play, Function 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:"); diff --git a/Userland/Games/Hearts/Player.h b/Userland/Games/Hearts/Player.h index 8b4528f329..328c5016bc 100644 --- a/Userland/Games/Hearts/Player.h +++ b/Userland/Games/Hearts/Player.h @@ -42,7 +42,7 @@ public: Optional pick_specific_card(Card::Type type, CardValue value); size_t pick_last_card(); bool has_card_of_type(Card::Type type); - Vector hand_sorted_by_points_and_value() const; + Vector hand_sorted_by_fn(bool (*)(CardWithIndex&, CardWithIndex&)) const; void sort_hand() { quick_sort(hand, hearts_card_less); } void remove_cards(NonnullRefPtrVector const& cards);