summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibCards/Card.h
blob: e1d05acad240554421d7305a6e8d1a5522df2a47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
 * 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
 */

#pragma once

#include <AK/Format.h>
#include <LibCore/Object.h>
#include <LibGUI/Painter.h>
#include <LibGfx/Bitmap.h>
#include <LibGfx/CharacterBitmap.h>
#include <LibGfx/Rect.h>

namespace Cards {

enum class Rank : u8 {
    Ace,
    Two,
    Three,
    Four,
    Five,
    Six,
    Seven,
    Eight,
    Nine,
    Ten,
    Jack,
    Queen,
    King,
    __Count
};

constexpr StringView card_rank_label(Rank rank)
{
    switch (rank) {
    case Rank::Ace:
        return "A"sv;
    case Rank::Two:
        return "2"sv;
    case Rank::Three:
        return "3"sv;
    case Rank::Four:
        return "4"sv;
    case Rank::Five:
        return "5"sv;
    case Rank::Six:
        return "6"sv;
    case Rank::Seven:
        return "7"sv;
    case Rank::Eight:
        return "8"sv;
    case Rank::Nine:
        return "9"sv;
    case Rank::Ten:
        return "10"sv;
    case Rank::Jack:
        return "J"sv;
    case Rank::Queen:
        return "Q"sv;
    case Rank::King:
        return "K"sv;
    case Rank::__Count:
        VERIFY_NOT_REACHED();
    }
    VERIFY_NOT_REACHED();
}

enum class Suit : u8 {
    Clubs,
    Diamonds,
    Spades,
    Hearts,
    __Count
};

class Card final : public Core::Object {
    C_OBJECT(Card)
public:
    static constexpr int width = 80;
    static constexpr int height = 100;
    static constexpr int card_count = to_underlying(Rank::__Count);
    static constexpr int card_radius = 5;

    virtual ~Card() override = default;

    Gfx::IntRect& rect() { return m_rect; }
    Gfx::IntRect const& rect() const { return m_rect; }
    Gfx::IntPoint position() const { return m_rect.location(); }
    Gfx::IntPoint old_position() const { return m_old_position; }
    Rank rank() const { return m_rank; };
    Suit suit() const { return m_suit; }

    bool is_old_position_valid() const { return m_old_position_valid; }
    bool is_moving() const { return m_moving; }
    bool is_upside_down() const { return m_upside_down; }
    bool is_inverted() const { return m_inverted; }
    bool is_previewed() const { return m_previewed; }
    Gfx::Color color() const { return (m_suit == Suit::Diamonds || m_suit == Suit::Hearts) ? Color::Red : Color::Black; }

    void set_position(const Gfx::IntPoint p) { m_rect.set_location(p); }
    void set_moving(bool moving) { m_moving = moving; }
    void set_upside_down(bool upside_down) { m_upside_down = upside_down; }
    void set_inverted(bool inverted) { m_inverted = inverted; }
    void set_previewed(bool previewed) { m_previewed = previewed; }

    void save_old_position();

    void paint(GUI::Painter&, bool highlighted = false) const;
    void clear(GUI::Painter&, Color background_color) const;
    void clear_and_paint(GUI::Painter& painter, Color background_color, bool highlighted);

private:
    Card(Suit, Rank);

    Gfx::IntRect m_rect;
    Gfx::IntPoint m_old_position;
    Suit m_suit;
    Rank m_rank;
    bool m_old_position_valid { false };
    bool m_moving { false };
    bool m_upside_down { false };
    bool m_inverted { false };
    bool m_previewed { 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<>
struct AK::Formatter<Cards::Card> : Formatter<FormatString> {
    ErrorOr<void> format(FormatBuilder& builder, Cards::Card const& card)
    {
        StringView suit;

        switch (card.suit()) {
        case Cards::Suit::Clubs:
            suit = "C"sv;
            break;
        case Cards::Suit::Diamonds:
            suit = "D"sv;
            break;
        case Cards::Suit::Hearts:
            suit = "H"sv;
            break;
        case Cards::Suit::Spades:
            suit = "S"sv;
            break;
        default:
            VERIFY_NOT_REACHED();
        }

        return Formatter<FormatString>::format(builder, "{:>2}{}"sv, Cards::card_rank_label(card.rank()), suit);
    }
};