LibCards+Games: Return ErrorOr from deck-creation factory functions :^)

Also, be smarter about appending cards to the deck: we can
unchecked_append them to the deck, since we already ensured enough
capacity earlier.
This commit is contained in:
Sam Atkins 2023-01-20 13:19:56 +00:00 committed by Linus Groh
parent 0855e9f014
commit c7c4d70f6e
5 changed files with 15 additions and 14 deletions

View file

@ -199,7 +199,7 @@ void Game::setup(DeprecatedString player_name, int hand_number)
m_passing_button->set_focus(false);
}
NonnullRefPtrVector<Card> deck = Cards::create_standard_deck(Cards::Shuffle::Yes);
NonnullRefPtrVector<Card> deck = Cards::create_standard_deck(Cards::Shuffle::Yes).release_value_but_fixme_should_propagate_errors();
for (auto& player : m_players) {
player.hand.ensure_capacity(Card::card_count);

View file

@ -168,7 +168,7 @@ void Game::setup(Mode mode)
if (on_undo_availability_change)
on_undo_availability_change(false);
m_new_deck = Cards::create_standard_deck(Cards::Shuffle::Yes);
m_new_deck = Cards::create_standard_deck(Cards::Shuffle::Yes).release_value_but_fixme_should_propagate_errors();
clear_moving_cards();

View file

@ -74,7 +74,7 @@ void Game::setup(Mode mode)
break;
}
m_new_deck = Cards::create_deck(0, 0, heart_suits, spade_suits, Cards::Shuffle::Yes);
m_new_deck = Cards::create_deck(0, 0, heart_suits, spade_suits, Cards::Shuffle::Yes).release_value_but_fixme_should_propagate_errors();
clear_moving_cards();

View file

@ -55,28 +55,29 @@ void Card::clear_and_paint(GUI::Painter& painter, Color background_color, bool h
save_old_position();
}
NonnullRefPtrVector<Card> create_standard_deck(Shuffle shuffle)
ErrorOr<NonnullRefPtrVector<Card>> create_standard_deck(Shuffle shuffle)
{
return create_deck(1, 1, 1, 1, shuffle);
}
NonnullRefPtrVector<Card> create_deck(unsigned full_club_suit_count, unsigned full_diamond_suit_count, unsigned full_heart_suit_count, unsigned full_spade_suit_count, Shuffle shuffle)
ErrorOr<NonnullRefPtrVector<Card>> create_deck(unsigned full_club_suit_count, unsigned full_diamond_suit_count, unsigned full_heart_suit_count, unsigned full_spade_suit_count, Shuffle shuffle)
{
NonnullRefPtrVector<Card> deck;
deck.ensure_capacity(Card::card_count * (full_club_suit_count + full_diamond_suit_count + full_heart_suit_count + full_spade_suit_count));
TRY(deck.try_ensure_capacity(Card::card_count * (full_club_suit_count + full_diamond_suit_count + full_heart_suit_count + full_spade_suit_count)));
auto add_cards_for_suit = [&deck](Cards::Suit suit, unsigned number_of_suits) {
auto add_cards_for_suit = [&deck](Cards::Suit suit, unsigned number_of_suits) -> ErrorOr<void> {
for (auto i = 0u; i < number_of_suits; ++i) {
for (auto rank = 0; rank < Card::card_count; ++rank) {
deck.append(Card::construct(suit, static_cast<Cards::Rank>(rank)));
deck.unchecked_append(TRY(Card::try_create(suit, static_cast<Cards::Rank>(rank))));
}
}
return {};
};
add_cards_for_suit(Cards::Suit::Clubs, full_club_suit_count);
add_cards_for_suit(Cards::Suit::Diamonds, full_diamond_suit_count);
add_cards_for_suit(Cards::Suit::Hearts, full_heart_suit_count);
add_cards_for_suit(Cards::Suit::Spades, full_spade_suit_count);
TRY(add_cards_for_suit(Cards::Suit::Clubs, full_club_suit_count));
TRY(add_cards_for_suit(Cards::Suit::Diamonds, full_diamond_suit_count));
TRY(add_cards_for_suit(Cards::Suit::Hearts, full_heart_suit_count));
TRY(add_cards_for_suit(Cards::Suit::Spades, full_spade_suit_count));
if (shuffle == Shuffle::Yes)
shuffle_deck(deck);

View file

@ -131,8 +131,8 @@ enum class Shuffle {
No,
Yes,
};
NonnullRefPtrVector<Card> create_standard_deck(Shuffle);
NonnullRefPtrVector<Card> create_deck(unsigned full_club_suit_count, unsigned full_diamond_suit_count, unsigned full_heart_suit_count, unsigned full_spade_suit_count, Shuffle);
ErrorOr<NonnullRefPtrVector<Card>> create_standard_deck(Shuffle);
ErrorOr<NonnullRefPtrVector<Card>> create_deck(unsigned full_club_suit_count, unsigned full_diamond_suit_count, unsigned full_heart_suit_count, unsigned full_spade_suit_count, Shuffle);
void shuffle_deck(NonnullRefPtrVector<Card>&);
}