mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-12-04 13:30:31 +00:00
Hearts: Fix sorting function for lead cards
The pick_lead_card() function sometimes picks the incorrect card because the sorted_hand vector wasn't being sorted properly.
This commit is contained in:
parent
efef77a154
commit
647d0f9f8a
Notes:
sideshowbarker
2024-07-18 17:30:05 +09:00
Author: https://github.com/gunnarbeutner Commit: https://github.com/SerenityOS/serenity/commit/647d0f9f8a2 Pull-request: https://github.com/SerenityOS/serenity/pull/7410
1 changed files with 13 additions and 3 deletions
|
@ -6,6 +6,7 @@
|
|||
|
||||
#include "Player.h"
|
||||
#include "Helpers.h"
|
||||
#include <AK/Debug.h>
|
||||
#include <AK/QuickSort.h>
|
||||
|
||||
namespace Hearts {
|
||||
|
@ -23,19 +24,28 @@ size_t Player::pick_lead_card(Function<bool(Card&)> valid_play, Function<bool(Ca
|
|||
sorted_hand.empend(card, i);
|
||||
}
|
||||
quick_sort(sorted_hand, [](auto& cwi1, auto& cwi2) {
|
||||
if (hearts_card_points(*cwi1.card) >= hearts_card_points(*cwi2.card))
|
||||
if (hearts_card_points(*cwi2.card) < hearts_card_points(*cwi1.card))
|
||||
return true;
|
||||
if (hearts_card_value(*cwi1.card) >= hearts_card_value(*cwi2.card))
|
||||
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;
|
||||
});
|
||||
|
||||
if constexpr (HEARTS_DEBUG) {
|
||||
dbgln("Sorted hand:");
|
||||
for (auto& cwi : sorted_hand)
|
||||
dbgln("{}", *cwi.card);
|
||||
dbgln("----");
|
||||
}
|
||||
|
||||
size_t last_index = -1;
|
||||
for (auto& cwi : sorted_hand) {
|
||||
if (!valid_play(*cwi.card))
|
||||
continue;
|
||||
if (prefer_card(*cwi.card))
|
||||
if (prefer_card(*cwi.card)) {
|
||||
dbgln_if(HEARTS_DEBUG, "Preferring card {}", *cwi.card);
|
||||
return cwi.index;
|
||||
}
|
||||
last_index = cwi.index;
|
||||
}
|
||||
return last_index;
|
||||
|
|
Loading…
Reference in a new issue