mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 21:27:34 +00:00
Hearts: Move sorting helper from Player::pick_lead_card into a method
This commit is contained in:
parent
4ba9cc82c0
commit
89d38b7e94
2 changed files with 16 additions and 7 deletions
|
@ -11,18 +11,13 @@
|
||||||
|
|
||||||
namespace Hearts {
|
namespace Hearts {
|
||||||
|
|
||||||
size_t Player::pick_lead_card(Function<bool(Card&)> valid_play, Function<bool(Card&)> prefer_card,
|
Vector<CardWithIndex> Player::hand_sorted_by_points_and_value() const
|
||||||
Function<bool(Card&)> lower_value_card_in_play)
|
|
||||||
{
|
{
|
||||||
struct CardWithIndex {
|
|
||||||
RefPtr<Card> card;
|
|
||||||
size_t index;
|
|
||||||
};
|
|
||||||
Vector<CardWithIndex> sorted_hand;
|
Vector<CardWithIndex> sorted_hand;
|
||||||
for (size_t i = 0; i < hand.size(); i++) {
|
for (size_t i = 0; i < hand.size(); i++) {
|
||||||
auto& card = hand[i];
|
auto& card = hand[i];
|
||||||
if (card)
|
if (card)
|
||||||
sorted_hand.empend(card, i);
|
sorted_hand.empend(*card, i);
|
||||||
}
|
}
|
||||||
quick_sort(sorted_hand, [](auto& cwi1, auto& cwi2) {
|
quick_sort(sorted_hand, [](auto& cwi1, auto& cwi2) {
|
||||||
if (hearts_card_points(*cwi2.card) < hearts_card_points(*cwi1.card))
|
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 true;
|
||||||
return false;
|
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) {
|
if constexpr (HEARTS_DEBUG) {
|
||||||
dbgln("Sorted hand:");
|
dbgln("Sorted hand:");
|
||||||
|
|
|
@ -7,12 +7,18 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "Helpers.h"
|
#include "Helpers.h"
|
||||||
|
#include <AK/QuickSort.h>
|
||||||
#include <LibCards/Card.h>
|
#include <LibCards/Card.h>
|
||||||
|
|
||||||
using Cards::Card;
|
using Cards::Card;
|
||||||
|
|
||||||
namespace Hearts {
|
namespace Hearts {
|
||||||
|
|
||||||
|
struct CardWithIndex {
|
||||||
|
NonnullRefPtr<Card> card;
|
||||||
|
size_t index;
|
||||||
|
};
|
||||||
|
|
||||||
struct Player {
|
struct Player {
|
||||||
AK_MAKE_NONMOVABLE(Player);
|
AK_MAKE_NONMOVABLE(Player);
|
||||||
|
|
||||||
|
@ -29,6 +35,7 @@ public:
|
||||||
Optional<size_t> pick_specific_card(Card::Type type, CardValue value);
|
Optional<size_t> pick_specific_card(Card::Type type, CardValue value);
|
||||||
size_t pick_last_card();
|
size_t pick_last_card();
|
||||||
bool has_card_of_type(Card::Type type);
|
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); }
|
void sort_hand() { quick_sort(hand, hearts_card_less); }
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue