2020-03-09 18:09:22 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Till Mayer <till.mayer@web.de>
|
2022-03-01 05:21:12 +03:00
|
|
|
* Copyright (c) 2022, the SerenityOS developers.
|
2022-09-28 13:35:12 +03:00
|
|
|
* Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
|
2020-03-09 18:09:22 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-03-09 18:09:22 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Card.h"
|
2022-09-28 13:35:12 +03:00
|
|
|
#include <AK/Random.h>
|
2022-08-21 14:53:20 +03:00
|
|
|
#include <LibCards/CardPainter.h>
|
2020-03-09 18:09:22 +03:00
|
|
|
|
2021-05-20 14:01:14 +03:00
|
|
|
namespace Cards {
|
2021-05-05 16:42:59 +03:00
|
|
|
|
2022-08-20 22:21:33 +03:00
|
|
|
Card::Card(Suit suit, Rank rank)
|
2020-06-10 11:57:59 +03:00
|
|
|
: m_rect(Gfx::IntRect({}, { width, height }))
|
2022-03-18 22:53:23 +03:00
|
|
|
, m_suit(suit)
|
2022-08-20 22:21:33 +03:00
|
|
|
, m_rank(rank)
|
2020-03-09 18:09:22 +03:00
|
|
|
{
|
2022-08-20 22:21:33 +03:00
|
|
|
VERIFY(to_underlying(rank) < card_count);
|
2020-03-09 18:09:22 +03:00
|
|
|
}
|
|
|
|
|
2023-01-05 18:16:16 +03:00
|
|
|
void Card::paint(GUI::Painter& painter, bool highlighted) const
|
2020-03-09 18:09:22 +03:00
|
|
|
{
|
2022-08-21 14:53:20 +03:00
|
|
|
auto& card_painter = CardPainter::the();
|
|
|
|
auto bitmap = [&]() {
|
|
|
|
if (m_inverted)
|
|
|
|
return m_upside_down ? card_painter.card_back_inverted() : card_painter.card_front_inverted(m_suit, m_rank);
|
2023-01-05 18:16:16 +03:00
|
|
|
if (highlighted) {
|
2023-01-05 02:25:23 +03:00
|
|
|
VERIFY(!m_upside_down);
|
|
|
|
return card_painter.card_front_highlighted(m_suit, m_rank);
|
|
|
|
}
|
2022-08-21 14:53:20 +03:00
|
|
|
return m_upside_down ? card_painter.card_back() : card_painter.card_front(m_suit, m_rank);
|
|
|
|
}();
|
|
|
|
painter.blit(position(), bitmap, bitmap->rect());
|
2020-03-09 18:09:22 +03:00
|
|
|
}
|
|
|
|
|
2022-12-06 22:43:46 +03:00
|
|
|
void Card::clear(GUI::Painter& painter, Color background_color) const
|
2020-03-09 18:09:22 +03:00
|
|
|
{
|
2021-05-22 08:13:31 +03:00
|
|
|
painter.fill_rect({ old_position(), { width, height } }, background_color);
|
2020-03-09 18:09:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Card::save_old_position()
|
|
|
|
{
|
|
|
|
m_old_position = m_rect.location();
|
|
|
|
m_old_position_valid = true;
|
|
|
|
}
|
|
|
|
|
2023-01-05 18:16:16 +03:00
|
|
|
void Card::clear_and_paint(GUI::Painter& painter, Color background_color, bool highlighted)
|
2020-03-09 18:09:22 +03:00
|
|
|
{
|
|
|
|
if (is_old_position_valid())
|
|
|
|
clear(painter, background_color);
|
|
|
|
|
2023-01-05 18:16:16 +03:00
|
|
|
paint(painter, highlighted);
|
2020-03-09 18:09:22 +03:00
|
|
|
save_old_position();
|
|
|
|
}
|
2021-05-05 16:42:59 +03:00
|
|
|
|
2023-03-06 16:17:01 +03:00
|
|
|
ErrorOr<Vector<NonnullRefPtr<Card>>> create_standard_deck(Shuffle shuffle)
|
2022-09-28 13:35:12 +03:00
|
|
|
{
|
|
|
|
return create_deck(1, 1, 1, 1, shuffle);
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:17:01 +03:00
|
|
|
ErrorOr<Vector<NonnullRefPtr<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)
|
2022-09-28 13:35:12 +03:00
|
|
|
{
|
2023-03-06 16:17:01 +03:00
|
|
|
Vector<NonnullRefPtr<Card>> deck;
|
2023-01-20 16:19:56 +03:00
|
|
|
TRY(deck.try_ensure_capacity(Card::card_count * (full_club_suit_count + full_diamond_suit_count + full_heart_suit_count + full_spade_suit_count)));
|
2022-09-28 13:35:12 +03:00
|
|
|
|
2023-01-20 16:19:56 +03:00
|
|
|
auto add_cards_for_suit = [&deck](Cards::Suit suit, unsigned number_of_suits) -> ErrorOr<void> {
|
2022-09-28 13:35:12 +03:00
|
|
|
for (auto i = 0u; i < number_of_suits; ++i) {
|
|
|
|
for (auto rank = 0; rank < Card::card_count; ++rank) {
|
2023-01-20 16:19:56 +03:00
|
|
|
deck.unchecked_append(TRY(Card::try_create(suit, static_cast<Cards::Rank>(rank))));
|
2022-09-28 13:35:12 +03:00
|
|
|
}
|
|
|
|
}
|
2023-01-20 16:19:56 +03:00
|
|
|
return {};
|
2022-09-28 13:35:12 +03:00
|
|
|
};
|
|
|
|
|
2023-01-20 16:19:56 +03:00
|
|
|
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));
|
2022-09-28 13:35:12 +03:00
|
|
|
|
|
|
|
if (shuffle == Shuffle::Yes)
|
2023-01-20 16:24:41 +03:00
|
|
|
AK::shuffle(deck);
|
2022-09-28 13:35:12 +03:00
|
|
|
|
|
|
|
return deck;
|
|
|
|
}
|
|
|
|
|
2021-05-05 16:42:59 +03:00
|
|
|
}
|