diff options
author | Gunnar Beutner <gbeutner@serenityos.org> | 2021-06-01 00:45:26 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-06-01 08:52:08 +0200 |
commit | 63d3beb78cb28edf52ed8b3da04450d811093d70 (patch) | |
tree | a657990d1cd2435a98c9cfc1adb7ea3b29e38805 /Userland/Games | |
parent | 8b9da08d5aaf1d38956ee6ad6efc00abe364685b (diff) | |
download | serenity-63d3beb78cb28edf52ed8b3da04450d811093d70.zip |
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.
Diffstat (limited to 'Userland/Games')
-rw-r--r-- | Userland/Games/Hearts/Player.cpp | 28 | ||||
-rw-r--r-- | 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<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:"); 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<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); |