1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-06-01 03:08:13 +00:00

LibCards+Games: Replace card "value" int with a Rank enum

Because `card->value() == 11` is a lot less clear than `card->rank() ==
Cards::Rank::Queen`, and also safer.

Put this, along with the `Suit` enum, in the `Cards` namespace directly
instead of inside `Cards::Card`. Slightly less typing that way.
This commit is contained in:
Sam Atkins 2022-08-20 20:21:33 +01:00 committed by Andreas Kling
parent 163a74e3e2
commit aac2488d5c
9 changed files with 121 additions and 75 deletions

View file

@ -71,7 +71,7 @@ size_t Player::pick_lead_card(Function<bool(Card&)> valid_play, Function<bool(Ca
return last_index;
}
Optional<size_t> Player::pick_low_points_high_value_card(Optional<Card::Suit> suit)
Optional<size_t> Player::pick_low_points_high_value_card(Optional<Cards::Suit> suit)
{
auto sorted_hand = hand_sorted_by_fn(compare_card_value);
int min_points = -1;
@ -115,10 +115,10 @@ Optional<size_t> Player::pick_slightly_higher_value_card(Card& other_card)
size_t Player::pick_max_points_card(Function<bool(Card&)> ignore_card)
{
auto queen_of_spades_maybe = pick_specific_card(Card::Suit::Spades, CardValue::Queen);
auto queen_of_spades_maybe = pick_specific_card(Cards::Suit::Spades, CardValue::Queen);
if (queen_of_spades_maybe.has_value())
return queen_of_spades_maybe.value();
if (has_card_of_suit(Card::Suit::Hearts)) {
if (has_card_of_suit(Cards::Suit::Hearts)) {
auto highest_hearts_card_index = pick_last_card();
auto& card = hand[highest_hearts_card_index];
if (!ignore_card(*card))
@ -127,7 +127,7 @@ size_t Player::pick_max_points_card(Function<bool(Card&)> ignore_card)
return pick_low_points_high_value_card().value();
}
Optional<size_t> Player::pick_specific_card(Card::Suit suit, CardValue value)
Optional<size_t> Player::pick_specific_card(Cards::Suit suit, CardValue value)
{
for (size_t i = 0; i < hand.size(); i++) {
auto& card = hand[i];
@ -150,7 +150,7 @@ size_t Player::pick_last_card()
VERIFY_NOT_REACHED();
}
bool Player::has_card_of_suit(Card::Suit suit)
bool Player::has_card_of_suit(Cards::Suit suit)
{
auto matching_card = hand.first_matching([&](auto const& other_card) {
return !other_card.is_null() && other_card->suit() == suit;