mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 09:07:45 +00:00
LibCards+Games: Replace card "value" int with a Rank enum
Because `card->value() == 11` is a lot less clear than `card->rank() == Cards::Rank::Queen`, and also safer. Put this, along with the `Suit` enum, in the `Cards` namespace directly instead of inside `Cards::Card`. Slightly less typing that way.
This commit is contained in:
parent
163a74e3e2
commit
aac2488d5c
9 changed files with 121 additions and 75 deletions
|
@ -67,13 +67,13 @@ static constexpr Gfx::CharacterBitmap s_club {
|
|||
static RefPtr<Gfx::Bitmap> s_background;
|
||||
static RefPtr<Gfx::Bitmap> s_background_inverted;
|
||||
|
||||
Card::Card(Suit suit, uint8_t value)
|
||||
Card::Card(Suit suit, Rank rank)
|
||||
: m_rect(Gfx::IntRect({}, { width, height }))
|
||||
, m_front(Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, { width, height }).release_value_but_fixme_should_propagate_errors())
|
||||
, m_suit(suit)
|
||||
, m_value(value)
|
||||
, m_rank(rank)
|
||||
{
|
||||
VERIFY(value < card_count);
|
||||
VERIFY(to_underlying(rank) < card_count);
|
||||
Gfx::IntRect paint_rect({ 0, 0 }, { width, height });
|
||||
|
||||
if (s_background.is_null()) {
|
||||
|
@ -99,7 +99,6 @@ Card::Card(Suit suit, uint8_t value)
|
|||
Gfx::Painter painter(m_front);
|
||||
auto& font = Gfx::FontDatabase::default_font().bold_variant();
|
||||
|
||||
auto label = labels[value];
|
||||
painter.fill_rect_with_rounded_corners(paint_rect, Color::Black, card_radius);
|
||||
paint_rect.shrink(2, 2);
|
||||
painter.fill_rect_with_rounded_corners(paint_rect, Color::White, card_radius - 1);
|
||||
|
@ -108,7 +107,7 @@ Card::Card(Suit suit, uint8_t value)
|
|||
paint_rect.shrink(10, 6);
|
||||
|
||||
auto text_rect = Gfx::IntRect { 4, 6, font.width("10"sv), font.glyph_height() };
|
||||
painter.draw_text(text_rect, label, font, Gfx::TextAlignment::Center, color());
|
||||
painter.draw_text(text_rect, card_rank_label(m_rank), font, Gfx::TextAlignment::Center, color());
|
||||
|
||||
auto const& symbol = [&]() -> Gfx::CharacterBitmap const& {
|
||||
switch (m_suit) {
|
||||
|
|
|
@ -1,48 +1,96 @@
|
|||
/*
|
||||
* 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/Array.h>
|
||||
#include <AK/Format.h>
|
||||
#include <LibCore/Object.h>
|
||||
#include <LibGUI/Painter.h>
|
||||
#include <LibGfx/Bitmap.h>
|
||||
#include <LibGfx/CharacterBitmap.h>
|
||||
#include <LibGfx/Rect.h>
|
||||
#include <ctype.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 = 13;
|
||||
static constexpr int card_count = to_underlying(Rank::__Count);
|
||||
static constexpr int card_radius = 5;
|
||||
static constexpr Array<StringView, card_count> labels = {
|
||||
"A"sv, "2"sv, "3"sv, "4"sv, "5"sv, "6"sv, "7"sv, "8"sv, "9"sv, "10"sv, "J"sv, "Q"sv, "K"sv
|
||||
};
|
||||
|
||||
enum class Suit {
|
||||
Clubs,
|
||||
Diamonds,
|
||||
Spades,
|
||||
Hearts,
|
||||
__Count
|
||||
};
|
||||
|
||||
virtual ~Card() override = default;
|
||||
|
||||
Gfx::IntRect& rect() { return m_rect; }
|
||||
Gfx::IntPoint position() const { return m_rect.location(); }
|
||||
Gfx::IntPoint const& old_position() const { return m_old_position; }
|
||||
uint8_t value() const { return m_value; };
|
||||
Rank rank() const { return m_rank; };
|
||||
Suit suit() const { return m_suit; }
|
||||
|
||||
bool is_old_position_valid() const { return m_old_position_valid; }
|
||||
|
@ -63,7 +111,7 @@ public:
|
|||
void clear_and_draw(GUI::Painter&, Color const& background_color);
|
||||
|
||||
private:
|
||||
Card(Suit suit, uint8_t value);
|
||||
Card(Suit, Rank);
|
||||
|
||||
static NonnullRefPtr<Gfx::Bitmap> invert_bitmap(Gfx::Bitmap&);
|
||||
|
||||
|
@ -72,7 +120,7 @@ private:
|
|||
RefPtr<Gfx::Bitmap> m_front_inverted;
|
||||
Gfx::IntPoint m_old_position;
|
||||
Suit m_suit;
|
||||
uint8_t m_value;
|
||||
Rank m_rank;
|
||||
bool m_old_position_valid { false };
|
||||
bool m_moving { false };
|
||||
bool m_upside_down { false };
|
||||
|
@ -88,22 +136,22 @@ struct AK::Formatter<Cards::Card> : Formatter<FormatString> {
|
|||
StringView suit;
|
||||
|
||||
switch (card.suit()) {
|
||||
case Cards::Card::Suit::Clubs:
|
||||
case Cards::Suit::Clubs:
|
||||
suit = "C"sv;
|
||||
break;
|
||||
case Cards::Card::Suit::Diamonds:
|
||||
case Cards::Suit::Diamonds:
|
||||
suit = "D"sv;
|
||||
break;
|
||||
case Cards::Card::Suit::Hearts:
|
||||
case Cards::Suit::Hearts:
|
||||
suit = "H"sv;
|
||||
break;
|
||||
case Cards::Card::Suit::Spades:
|
||||
case Cards::Suit::Spades:
|
||||
suit = "S"sv;
|
||||
break;
|
||||
default:
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
return Formatter<FormatString>::format(builder, "{:>2}{}"sv, Cards::Card::labels[card.value()], suit);
|
||||
return Formatter<FormatString>::format(builder, "{:>2}{}"sv, Cards::card_rank_label(card.rank()), suit);
|
||||
}
|
||||
};
|
||||
|
|
|
@ -170,12 +170,12 @@ void CardStack::add_all_grabbed_cards(Gfx::IntPoint const& click_location, Nonnu
|
|||
break;
|
||||
}
|
||||
|
||||
if (!color_match || card.value() != last_value - 1) {
|
||||
if (!color_match || to_underlying(card.rank()) != last_value - 1) {
|
||||
valid_stack = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
last_value = card.value();
|
||||
last_value = to_underlying(card.rank());
|
||||
last_color = card.color();
|
||||
}
|
||||
|
||||
|
@ -195,13 +195,13 @@ bool CardStack::is_allowed_to_push(Card const& card, size_t stack_size, Movement
|
|||
if (m_type == Type::Normal && is_empty()) {
|
||||
// FIXME: proper solution for this
|
||||
if (movement_rule == MovementRule::Alternating) {
|
||||
return card.value() == 12;
|
||||
return card.rank() == Rank::King;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
if (m_type == Type::Foundation && is_empty())
|
||||
return card.value() == 0;
|
||||
return card.rank() == Rank::Ace;
|
||||
|
||||
if (!is_empty()) {
|
||||
auto& top_card = peek();
|
||||
|
@ -212,7 +212,7 @@ bool CardStack::is_allowed_to_push(Card const& card, size_t stack_size, Movement
|
|||
// Prevent player from dragging an entire stack of cards to the foundation stack
|
||||
if (stack_size > 1)
|
||||
return false;
|
||||
return top_card.suit() == card.suit() && m_stack.size() == card.value();
|
||||
return top_card.suit() == card.suit() && m_stack.size() == to_underlying(card.rank());
|
||||
} else if (m_type == Type::Normal) {
|
||||
bool color_match;
|
||||
switch (movement_rule) {
|
||||
|
@ -227,7 +227,7 @@ bool CardStack::is_allowed_to_push(Card const& card, size_t stack_size, Movement
|
|||
break;
|
||||
}
|
||||
|
||||
return color_match && top_card.value() == card.value() + 1;
|
||||
return color_match && to_underlying(top_card.rank()) == to_underlying(card.rank()) + 1;
|
||||
}
|
||||
|
||||
VERIFY_NOT_REACHED();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue