1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
/*
* Copyright (c) 2021, Gunnar Beutner <gbeutner@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "Player.h"
#include "Helpers.h"
#include <AK/Debug.h>
#include <AK/QuickSort.h>
namespace Hearts {
static bool compare_card_value(CardWithIndex& cwi1, CardWithIndex& cwi2)
{
return hearts_card_value(*cwi2.card) < hearts_card_value(*cwi1.card);
}
static bool compare_card_points_and_value(CardWithIndex& cwi1, CardWithIndex& cwi2)
{
if (hearts_card_points(*cwi2.card) < hearts_card_points(*cwi1.card))
return true;
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;
}
NonnullRefPtrVector<Card> Player::pick_cards_to_pass(PassingDirection)
{
auto sorted_hand = hand_sorted_by_fn(compare_card_value);
NonnullRefPtrVector<Card> cards;
cards.append(*sorted_hand[0].card);
cards.append(*sorted_hand[1].card);
cards.append(*sorted_hand[2].card);
return cards;
}
Vector<CardWithIndex> Player::hand_sorted_by_fn(bool (*fn)(CardWithIndex&, CardWithIndex&)) const
{
Vector<CardWithIndex> sorted_hand;
for (size_t i = 0; i < hand.size(); i++) {
auto& card = hand[i];
if (card)
sorted_hand.empend(*card, i);
}
quick_sort(sorted_hand, fn);
return sorted_hand;
}
size_t Player::pick_lead_card(Function<bool(Card&)> valid_play, Function<bool(Card&)> prefer_card)
{
auto sorted_hand = hand_sorted_by_fn(compare_card_points_and_value);
if constexpr (HEARTS_DEBUG) {
dbgln("Sorted hand:");
for (auto& cwi : sorted_hand)
dbgln("{}", *cwi.card);
dbgln("----");
}
ssize_t last_index = -1;
for (auto& cwi : sorted_hand) {
if (!valid_play(*cwi.card))
continue;
if (prefer_card(*cwi.card)) {
dbgln_if(HEARTS_DEBUG, "Preferring card {}", *cwi.card);
return cwi.index;
}
last_index = cwi.index;
}
return last_index;
}
Optional<size_t> Player::pick_low_points_high_value_card(Optional<Card::Type> type)
{
auto sorted_hand = hand_sorted_by_fn(compare_card_value);
int min_points = -1;
Optional<size_t> card_index;
for (auto& cwi : sorted_hand) {
if (type.has_value() && cwi.card->type() != type.value())
continue;
auto points = hearts_card_points(*cwi.card);
if (min_points == -1 || points < min_points) {
min_points = points;
card_index = cwi.index;
}
}
VERIFY(card_index.has_value() || type.has_value());
return card_index;
}
Optional<size_t> Player::pick_lower_value_card(Card& other_card)
{
for (ssize_t i = hand.size() - 1; i >= 0; i--) {
auto& card = hand[i];
if (card.is_null())
continue;
if (card->type() == other_card.type() && hearts_card_value(*card) < hearts_card_value(other_card))
return i;
}
return {};
}
Optional<size_t> Player::pick_slightly_higher_value_card(Card& other_card)
{
for (size_t i = 0; i < hand.size(); i++) {
auto& card = hand[i];
if (card.is_null())
continue;
if (card->type() == other_card.type() && hearts_card_value(*card) > hearts_card_value(other_card))
return i;
}
return {};
}
size_t Player::pick_max_points_card(Function<bool(Card&)> ignore_card)
{
auto queen_of_spades_maybe = pick_specific_card(Card::Type::Spades, CardValue::Queen);
if (queen_of_spades_maybe.has_value())
return queen_of_spades_maybe.value();
if (has_card_of_type(Card::Type::Hearts)) {
auto highest_hearts_card_index = pick_last_card();
auto& card = hand[highest_hearts_card_index];
if (!ignore_card(*card))
return highest_hearts_card_index;
}
return pick_low_points_high_value_card().value();
}
Optional<size_t> Player::pick_specific_card(Card::Type type, CardValue value)
{
for (size_t i = 0; i < hand.size(); i++) {
auto& card = hand[i];
if (card.is_null())
continue;
if (card->type() == type && hearts_card_value(*card) == value)
return i;
}
return {};
}
size_t Player::pick_last_card()
{
for (ssize_t i = hand.size() - 1; i >= 0; i--) {
auto& card = hand[i];
if (card.is_null())
continue;
return i;
}
VERIFY_NOT_REACHED();
}
bool Player::has_card_of_type(Card::Type type)
{
auto matching_card = hand.first_matching([&](auto const& other_card) {
return !other_card.is_null() && other_card->type() == type;
});
return matching_card.has_value();
}
void Player::remove_cards(const NonnullRefPtrVector<Card>& cards)
{
for (auto& card : cards) {
hand.remove_first_matching([&card](auto& other_card) {
return other_card == card;
});
}
}
}
|