Hearts: Pick better non-matching cards

When we don't have a matching card for the lead card rather than
always preferring to play hearts we should try to get rid of our
high value cards first if no other player has hearts cards higher
than what we have.
This commit is contained in:
Gunnar Beutner 2021-06-01 00:40:37 +02:00 committed by Andreas Kling
parent 4a8d8da46c
commit 8b9da08d5a
Notes: sideshowbarker 2024-07-18 17:05:21 +09:00
3 changed files with 18 additions and 8 deletions

View File

@ -356,8 +356,12 @@ size_t Game::pick_card(Player& player)
RETURN_CARD_IF_VALID(player.pick_low_points_high_value_card(m_trick[0].type()));
if (is_first_trick)
return player.pick_low_points_high_value_card().value();
else
return player.pick_max_points_card();
else {
auto ignore_card = [this, &player](Card& card) {
return !other_player_has_higher_value_card(player, card);
};
return player.pick_max_points_card(move(ignore_card));
}
}
RETURN_CARD_IF_VALID(player.pick_lower_value_card(*high_card));
bool is_third_player = m_trick.size() == 2;
@ -382,8 +386,10 @@ size_t Game::pick_card(Player& player)
RETURN_CARD_IF_VALID(player.pick_slightly_higher_value_card(*high_card));
if (is_first_trick)
return player.pick_low_points_high_value_card().value();
else
return player.pick_max_points_card();
auto ignore_card = [this, &player](Card& card) {
return !other_player_has_higher_value_card(player, card);
};
return player.pick_max_points_card(move(ignore_card));
}
void Game::let_player_play_card()

View File

@ -107,13 +107,17 @@ Optional<size_t> Player::pick_slightly_higher_value_card(Card& other_card)
return {};
}
size_t Player::pick_max_points_card()
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))
return pick_last_card();
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();
}

View File

@ -38,7 +38,7 @@ public:
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_slightly_higher_value_card(Card& other_card);
size_t pick_max_points_card();
size_t pick_max_points_card(Function<bool(Card&)>);
Optional<size_t> pick_specific_card(Card::Type type, CardValue value);
size_t pick_last_card();
bool has_card_of_type(Card::Type type);