diff options
author | Sam Atkins <atkinssj@serenityos.org> | 2023-01-20 13:19:56 +0000 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2023-01-22 21:31:36 +0000 |
commit | c7c4d70f6ed9076745daf61236fdbc34cf4f2fce (patch) | |
tree | 858f16d8eee1b207b39e20a3b8774cc1abdea165 /Userland | |
parent | 0855e9f014bbf4cd1ada1ef8933c0632e4dea994 (diff) | |
download | serenity-c7c4d70f6ed9076745daf61236fdbc34cf4f2fce.zip |
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.
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/Games/Hearts/Game.cpp | 2 | ||||
-rw-r--r-- | Userland/Games/Solitaire/Game.cpp | 2 | ||||
-rw-r--r-- | Userland/Games/Spider/Game.cpp | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibCards/Card.cpp | 19 | ||||
-rw-r--r-- | 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<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); 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<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); 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<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>&); } |