summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorGunnar Beutner <gbeutner@serenityos.org>2021-05-24 14:15:52 +0200
committerAndreas Kling <kling@serenityos.org>2021-05-25 21:05:35 +0200
commit89d38b7e94a3d05aaa4092b4dc5bf1e31984d471 (patch)
tree35ca0fdf07f6c702231360a54517dbbe9acedeb2 /Userland
parent4ba9cc82c00b62e3a440a798596d90b4af44b633 (diff)
downloadserenity-89d38b7e94a3d05aaa4092b4dc5bf1e31984d471.zip
Hearts: Move sorting helper from Player::pick_lead_card into a method
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Games/Hearts/Player.cpp16
-rw-r--r--Userland/Games/Hearts/Player.h7
2 files changed, 16 insertions, 7 deletions
diff --git a/Userland/Games/Hearts/Player.cpp b/Userland/Games/Hearts/Player.cpp
index c36ee8e3cf..92c8706845 100644
--- a/Userland/Games/Hearts/Player.cpp
+++ b/Userland/Games/Hearts/Player.cpp
@@ -11,18 +11,13 @@
namespace Hearts {
-size_t Player::pick_lead_card(Function<bool(Card&)> valid_play, Function<bool(Card&)> prefer_card,
- Function<bool(Card&)> lower_value_card_in_play)
+Vector<CardWithIndex> Player::hand_sorted_by_points_and_value() const
{
- struct CardWithIndex {
- RefPtr<Card> card;
- size_t index;
- };
Vector<CardWithIndex> sorted_hand;
for (size_t i = 0; i < hand.size(); i++) {
auto& card = hand[i];
if (card)
- sorted_hand.empend(card, i);
+ sorted_hand.empend(*card, i);
}
quick_sort(sorted_hand, [](auto& cwi1, auto& cwi2) {
if (hearts_card_points(*cwi2.card) < hearts_card_points(*cwi1.card))
@@ -31,6 +26,13 @@ size_t Player::pick_lead_card(Function<bool(Card&)> valid_play, Function<bool(Ca
return true;
return false;
});
+ return sorted_hand;
+}
+
+size_t Player::pick_lead_card(Function<bool(Card&)> valid_play, Function<bool(Card&)> prefer_card,
+ Function<bool(Card&)> lower_value_card_in_play)
+{
+ auto sorted_hand = hand_sorted_by_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 11d33c5e53..73435d5109 100644
--- a/Userland/Games/Hearts/Player.h
+++ b/Userland/Games/Hearts/Player.h
@@ -7,12 +7,18 @@
#pragma once
#include "Helpers.h"
+#include <AK/QuickSort.h>
#include <LibCards/Card.h>
using Cards::Card;
namespace Hearts {
+struct CardWithIndex {
+ NonnullRefPtr<Card> card;
+ size_t index;
+};
+
struct Player {
AK_MAKE_NONMOVABLE(Player);
@@ -29,6 +35,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;
void sort_hand() { quick_sort(hand, hearts_card_less); }