1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 23:07:35 +00:00

Hearts: Move sorting helper from Player::pick_lead_card into a method

This commit is contained in:
Gunnar Beutner 2021-05-24 14:15:52 +02:00 committed by Andreas Kling
parent 4ba9cc82c0
commit 89d38b7e94
2 changed files with 16 additions and 7 deletions

View file

@ -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:");

View file

@ -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); }