diff options
author | Sam Atkins <atkinssj@serenityos.org> | 2022-09-28 11:35:12 +0100 |
---|---|---|
committer | Sam Atkins <atkinssj@gmail.com> | 2022-10-10 16:16:01 +0100 |
commit | 46299f385314196d9e527068c46a7ce3712bb115 (patch) | |
tree | 1d69b65448607dfe7e53312bab3049d5edca633e /Userland/Libraries | |
parent | 1d533acbc0c87a7c5084af066677217b16d1334b (diff) | |
download | serenity-46299f385314196d9e527068c46a7ce3712bb115.zip |
LibCards+Games: Move "create a deck" logic to LibCards
`create_standard_deck()` is the usual 52-card deck, but more custom
setups (such as Spider's multiples-of-one-suit) can be created by
passing suit counts to `create_deck()`.
Diffstat (limited to 'Userland/Libraries')
-rw-r--r-- | Userland/Libraries/LibCards/Card.cpp | 38 | ||||
-rw-r--r-- | Userland/Libraries/LibCards/Card.h | 8 |
2 files changed, 46 insertions, 0 deletions
diff --git a/Userland/Libraries/LibCards/Card.cpp b/Userland/Libraries/LibCards/Card.cpp index a42d0279e6..8bb98e2d55 100644 --- a/Userland/Libraries/LibCards/Card.cpp +++ b/Userland/Libraries/LibCards/Card.cpp @@ -1,11 +1,13 @@ /* * Copyright (c) 2020, Till Mayer <till.mayer@web.de> * Copyright (c) 2022, the SerenityOS developers. + * Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org> * * SPDX-License-Identifier: BSD-2-Clause */ #include "Card.h" +#include <AK/Random.h> #include <LibCards/CardPainter.h> namespace Cards { @@ -50,4 +52,40 @@ void Card::clear_and_draw(GUI::Painter& painter, Color const& background_color) save_old_position(); } +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) +{ + 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)); + + auto add_cards_for_suit = [&deck](Cards::Suit suit, unsigned number_of_suits) { + 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))); + } + } + }; + + 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); + + if (shuffle == Shuffle::Yes) + shuffle_deck(deck); + + return deck; +} + +void shuffle_deck(NonnullRefPtrVector<Card>& deck) +{ + auto iteration_count = deck.size() * 4; + for (auto i = 0u; i < iteration_count; ++i) + deck.append(deck.take(get_random_uniform(deck.size()))); +} + } diff --git a/Userland/Libraries/LibCards/Card.h b/Userland/Libraries/LibCards/Card.h index b3644013a4..ec4255807b 100644 --- a/Userland/Libraries/LibCards/Card.h +++ b/Userland/Libraries/LibCards/Card.h @@ -123,6 +123,14 @@ private: bool m_inverted { false }; }; +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); +void shuffle_deck(NonnullRefPtrVector<Card>&); + } template<> |