From c7c4d70f6ed9076745daf61236fdbc34cf4f2fce Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Fri, 20 Jan 2023 13:19:56 +0000 Subject: [PATCH] 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. --- Userland/Games/Hearts/Game.cpp | 2 +- Userland/Games/Solitaire/Game.cpp | 2 +- Userland/Games/Spider/Game.cpp | 2 +- Userland/Libraries/LibCards/Card.cpp | 19 ++++++++++--------- Userland/Libraries/LibCards/Card.h | 4 ++-- 5 files changed, 15 insertions(+), 14 deletions(-) diff --git a/Userland/Games/Hearts/Game.cpp b/Userland/Games/Hearts/Game.cpp index 79b774286c..d349b52d9a 100644 --- a/Userland/Games/Hearts/Game.cpp +++ b/Userland/Games/Hearts/Game.cpp @@ -199,7 +199,7 @@ void Game::setup(DeprecatedString player_name, int hand_number) m_passing_button->set_focus(false); } - NonnullRefPtrVector deck = Cards::create_standard_deck(Cards::Shuffle::Yes); + NonnullRefPtrVector 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); diff --git a/Userland/Games/Solitaire/Game.cpp b/Userland/Games/Solitaire/Game.cpp index e33cb902b9..8fbb91e3af 100644 --- a/Userland/Games/Solitaire/Game.cpp +++ b/Userland/Games/Solitaire/Game.cpp @@ -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(); diff --git a/Userland/Games/Spider/Game.cpp b/Userland/Games/Spider/Game.cpp index f4fc5e5276..7229e65ea9 100644 --- a/Userland/Games/Spider/Game.cpp +++ b/Userland/Games/Spider/Game.cpp @@ -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(); diff --git a/Userland/Libraries/LibCards/Card.cpp b/Userland/Libraries/LibCards/Card.cpp index 00f85894d0..90ecf84830 100644 --- a/Userland/Libraries/LibCards/Card.cpp +++ b/Userland/Libraries/LibCards/Card.cpp @@ -55,28 +55,29 @@ void Card::clear_and_paint(GUI::Painter& painter, Color background_color, bool h save_old_position(); } -NonnullRefPtrVector create_standard_deck(Shuffle shuffle) +ErrorOr> create_standard_deck(Shuffle shuffle) { return create_deck(1, 1, 1, 1, shuffle); } -NonnullRefPtrVector 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> 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 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 { 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(rank))); + deck.unchecked_append(TRY(Card::try_create(suit, static_cast(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); diff --git a/Userland/Libraries/LibCards/Card.h b/Userland/Libraries/LibCards/Card.h index e1d05acad2..5e86170939 100644 --- a/Userland/Libraries/LibCards/Card.h +++ b/Userland/Libraries/LibCards/Card.h @@ -131,8 +131,8 @@ enum class Shuffle { No, Yes, }; -NonnullRefPtrVector create_standard_deck(Shuffle); -NonnullRefPtrVector create_deck(unsigned full_club_suit_count, unsigned full_diamond_suit_count, unsigned full_heart_suit_count, unsigned full_spade_suit_count, Shuffle); +ErrorOr> create_standard_deck(Shuffle); +ErrorOr> 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&); }