mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 06:17:35 +00:00
Hearts: Pick better lead cards
Previously the AI would prefer playing a lead card for which no other player had a card with a higher value even though it also had a card for which a higher value card was still in play.
This commit is contained in:
parent
40ddb734ee
commit
38f8a6aabb
3 changed files with 4 additions and 18 deletions
|
@ -327,10 +327,7 @@ size_t Game::pick_card(Player& player)
|
||||||
auto prefer_card = [this, &player](Card& card) {
|
auto prefer_card = [this, &player](Card& card) {
|
||||||
return !other_player_has_lower_value_card(player, card) && other_player_has_higher_value_card(player, card);
|
return !other_player_has_lower_value_card(player, card) && other_player_has_higher_value_card(player, card);
|
||||||
};
|
};
|
||||||
auto lower_value_card_in_play = [this, &player](Card& card) {
|
return player.pick_lead_card(move(valid_card), move(prefer_card));
|
||||||
return other_player_has_lower_value_card(player, card);
|
|
||||||
};
|
|
||||||
return player.pick_lead_card(move(valid_card), move(prefer_card), move(lower_value_card_in_play));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
auto* high_card = &m_trick[0];
|
auto* high_card = &m_trick[0];
|
||||||
|
|
|
@ -39,8 +39,7 @@ Vector<CardWithIndex> Player::hand_sorted_by_points_and_value() const
|
||||||
return sorted_hand;
|
return sorted_hand;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t Player::pick_lead_card(Function<bool(Card&)> valid_play, Function<bool(Card&)> prefer_card,
|
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();
|
auto sorted_hand = hand_sorted_by_points_and_value();
|
||||||
|
|
||||||
|
@ -51,27 +50,17 @@ size_t Player::pick_lead_card(Function<bool(Card&)> valid_play, Function<bool(Ca
|
||||||
dbgln("----");
|
dbgln("----");
|
||||||
}
|
}
|
||||||
|
|
||||||
ssize_t fallback_index = -1;
|
|
||||||
ssize_t last_index = -1;
|
ssize_t last_index = -1;
|
||||||
for (auto& cwi : sorted_hand) {
|
for (auto& cwi : sorted_hand) {
|
||||||
if (!valid_play(*cwi.card))
|
if (!valid_play(*cwi.card))
|
||||||
continue;
|
continue;
|
||||||
if (lower_value_card_in_play(*cwi.card)) {
|
|
||||||
// Allow falling back to this card if no other suitable card is in play.
|
|
||||||
fallback_index = cwi.index;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (prefer_card(*cwi.card)) {
|
if (prefer_card(*cwi.card)) {
|
||||||
dbgln_if(HEARTS_DEBUG, "Preferring card {}", *cwi.card);
|
dbgln_if(HEARTS_DEBUG, "Preferring card {}", *cwi.card);
|
||||||
return cwi.index;
|
return cwi.index;
|
||||||
}
|
}
|
||||||
last_index = cwi.index;
|
last_index = cwi.index;
|
||||||
}
|
}
|
||||||
if (last_index == -1) {
|
return last_index;
|
||||||
dbgln_if(HEARTS_DEBUG, "Falling back to card {}", *hand[fallback_index]);
|
|
||||||
return fallback_index;
|
|
||||||
} else
|
|
||||||
return last_index;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Optional<size_t> Player::pick_low_points_high_value_card(Optional<Card::Type> type)
|
Optional<size_t> Player::pick_low_points_high_value_card(Optional<Card::Type> type)
|
||||||
|
|
|
@ -34,7 +34,7 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
NonnullRefPtrVector<Card> pick_cards_to_pass(PassingDirection);
|
NonnullRefPtrVector<Card> pick_cards_to_pass(PassingDirection);
|
||||||
size_t pick_lead_card(Function<bool(Card&)>, Function<bool(Card&)>, Function<bool(Card&)>);
|
size_t pick_lead_card(Function<bool(Card&)>, Function<bool(Card&)>);
|
||||||
Optional<size_t> pick_low_points_high_value_card(Optional<Card::Type> type = {});
|
Optional<size_t> pick_low_points_high_value_card(Optional<Card::Type> type = {});
|
||||||
Optional<size_t> pick_lower_value_card(Card& other_card);
|
Optional<size_t> pick_lower_value_card(Card& other_card);
|
||||||
Optional<size_t> pick_slightly_higher_value_card(Card& other_card);
|
Optional<size_t> pick_slightly_higher_value_card(Card& other_card);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue