diff options
author | Gunnar Beutner <gbeutner@serenityos.org> | 2021-06-01 00:46:01 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-06-01 08:52:08 +0200 |
commit | 45117a4134757989ef39df1ee7471946c57ec09d (patch) | |
tree | 9ca61d5990b9d0d3abf0cbc201956f17898d16b1 /Userland | |
parent | 63d3beb78cb28edf52ed8b3da04450d811093d70 (diff) | |
download | serenity-45117a4134757989ef39df1ee7471946c57ec09d.zip |
Hearts: Fix sorting for pick_low_points_high_value_card
Previously the function did not sort the hand at all which means we
wouldn't necessarily pick the card with the highest value.
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/Games/Hearts/Player.cpp | 12 |
1 files changed, 5 insertions, 7 deletions
diff --git a/Userland/Games/Hearts/Player.cpp b/Userland/Games/Hearts/Player.cpp index d304c5c94d..e223b8c865 100644 --- a/Userland/Games/Hearts/Player.cpp +++ b/Userland/Games/Hearts/Player.cpp @@ -73,18 +73,16 @@ size_t Player::pick_lead_card(Function<bool(Card&)> valid_play, Function<bool(Ca Optional<size_t> Player::pick_low_points_high_value_card(Optional<Card::Type> type) { + auto sorted_hand = hand_sorted_by_fn(compare_card_value); int min_points = -1; Optional<size_t> card_index; - for (ssize_t i = hand.size() - 1; i >= 0; i--) { - auto& card = hand[i]; - if (card.is_null()) - continue; - if (type.has_value() && card->type() != type.value()) + for (auto& cwi : sorted_hand) { + if (type.has_value() && cwi.card->type() != type.value()) continue; - auto points = hearts_card_points(*card); + auto points = hearts_card_points(*cwi.card); if (min_points == -1 || points < min_points) { min_points = points; - card_index = i; + card_index = cwi.index; } } VERIFY(card_index.has_value() || type.has_value()); |