summaryrefslogtreecommitdiff
path: root/Userland/Games
diff options
context:
space:
mode:
authorGunnar Beutner <gbeutner@serenityos.org>2021-05-23 14:21:33 +0200
committerAndreas Kling <kling@serenityos.org>2021-05-23 18:15:36 +0200
commit647d0f9f8a2b64bb6d0d273b71d1b4474e48576d (patch)
tree15e5b5c98fa5902f0cc9b7b50536a575d37a0432 /Userland/Games
parentefef77a154c32766251e6b0c2ae9c3d9e1f40382 (diff)
downloadserenity-647d0f9f8a2b64bb6d0d273b71d1b4474e48576d.zip
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.
Diffstat (limited to 'Userland/Games')
-rw-r--r--Userland/Games/Hearts/Player.cpp16
1 files changed, 13 insertions, 3 deletions
diff --git a/Userland/Games/Hearts/Player.cpp b/Userland/Games/Hearts/Player.cpp
index 021d79001f..4e3bd7839f 100644
--- a/Userland/Games/Hearts/Player.cpp
+++ b/Userland/Games/Hearts/Player.cpp
@@ -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;