Skip to content

Commit

Permalink
Libraries: Change enums to enum classes in LibCards
Browse files Browse the repository at this point in the history
  • Loading branch information
ldm5180 authored and awesomekling committed Mar 18, 2022
1 parent d3893a7 commit 56046d3
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 43 deletions.
2 changes: 1 addition & 1 deletion Userland/Games/Solitaire/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ void Game::timer_event(Core::TimerEvent&)

void Game::create_new_animation_card()
{
auto card = Card::construct(static_cast<Card::Type>(get_random_uniform(Card::Type::__Count)), get_random_uniform(Card::card_count));
auto card = Card::construct(static_cast<Card::Type>(get_random_uniform(to_underlying(Card::Type::__Count))), get_random_uniform(Card::card_count));
card->set_position({ get_random_uniform(Game::width - Card::width), get_random_uniform(Game::height / 8) });

int x_sgn = card->position().x() > (Game::width / 2) ? -1 : 1;
Expand Down
6 changes: 3 additions & 3 deletions Userland/Games/Spider/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ void Game::mousedown_event(GUI::MouseEvent& event)
update(top_card.rect());
}
} else if (m_focused_cards.is_empty()) {
to_check.add_all_grabbed_cards(click_location, m_focused_cards, Cards::CardStack::Same);
to_check.add_all_grabbed_cards(click_location, m_focused_cards, Cards::CardStack::MovementRule::Same);
m_mouse_down_location = click_location;
if (m_focused_stack)
m_focused_stack->set_focused(false);
Expand Down Expand Up @@ -310,7 +310,7 @@ void Game::mouseup_event(GUI::MouseEvent& event)
if (stack.is_focused())
continue;

if (stack.is_allowed_to_push(m_focused_cards.at(0), m_focused_cards.size(), Cards::CardStack::Any) && !stack.is_empty()) {
if (stack.is_allowed_to_push(m_focused_cards.at(0), m_focused_cards.size(), Cards::CardStack::MovementRule::Any) && !stack.is_empty()) {
move_focused_cards(stack);

rebound = false;
Expand All @@ -324,7 +324,7 @@ void Game::mouseup_event(GUI::MouseEvent& event)

for (auto& focused_card : m_focused_cards) {
if (stack.bounding_box().intersects(focused_card.rect())) {
if (stack.is_allowed_to_push(m_focused_cards.at(0), m_focused_cards.size(), Cards::CardStack::Any)) {
if (stack.is_allowed_to_push(m_focused_cards.at(0), m_focused_cards.size(), Cards::CardStack::MovementRule::Any)) {
move_focused_cards(stack);

rebound = false;
Expand Down
8 changes: 4 additions & 4 deletions Userland/Libraries/LibCards/Card.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,14 @@ Card::Card(Type type, uint8_t value)

auto const& symbol = [&]() -> Gfx::CharacterBitmap const& {
switch (m_type) {
case Diamonds:
case Type::Diamonds:
return s_diamond;
case Clubs:
case Type::Clubs:
return s_club;
break;
case Spades:
case Type::Spades:
return s_spade;
case Hearts:
case Type::Hearts:
return s_heart;
default:
VERIFY_NOT_REACHED();
Expand Down
4 changes: 2 additions & 2 deletions Userland/Libraries/LibCards/Card.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Card final : public Core::Object {
"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"
};

enum Type {
enum class Type {
Clubs,
Diamonds,
Spades,
Expand All @@ -49,7 +49,7 @@ class Card final : public Core::Object {
bool is_moving() const { return m_moving; }
bool is_upside_down() const { return m_upside_down; }
bool is_inverted() const { return m_inverted; }
Gfx::Color color() const { return (m_type == Diamonds || m_type == Hearts) ? Color::Red : Color::Black; }
Gfx::Color color() const { return (m_type == Type::Diamonds || m_type == Type::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; }
Expand Down
46 changes: 23 additions & 23 deletions Userland/Libraries/LibCards/CardStack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Cards {

CardStack::CardStack()
: m_position({ 0, 0 })
, m_type(Invalid)
, m_type(Type::Invalid)
, m_base(m_position, { Card::width, Card::height })
{
}
Expand All @@ -21,7 +21,7 @@ CardStack::CardStack(const Gfx::IntPoint& position, Type type)
, m_rules(rules_for_type(type))
, m_base(m_position, { Card::width, Card::height })
{
VERIFY(type != Invalid);
VERIFY(type != Type::Invalid);
calculate_bounding_box();
}

Expand All @@ -32,7 +32,7 @@ CardStack::CardStack(const Gfx::IntPoint& position, Type type, NonnullRefPtr<Car
, m_rules(rules_for_type(type))
, m_base(m_position, { Card::width, Card::height })
{
VERIFY(type != Invalid);
VERIFY(type != Type::Invalid);
calculate_bounding_box();
}

Expand All @@ -59,13 +59,13 @@ void CardStack::draw(GUI::Painter& painter, const Gfx::Color& background_color)
};

switch (m_type) {
case Stock:
case Type::Stock:
if (draw_background_if_empty()) {
painter.fill_rect(m_base.shrunken(Card::width / 4, Card::height / 4), background_color.lightened(1.5));
painter.fill_rect(m_base.shrunken(Card::width / 2, Card::height / 2), background_color);
}
break;
case Foundation:
case Type::Foundation:
if (draw_background_if_empty()) {
for (int y = 0; y < (m_base.height() - 4) / 8; ++y) {
for (int x = 0; x < (m_base.width() - 4) / 5; ++x) {
Expand All @@ -74,11 +74,11 @@ void CardStack::draw(GUI::Painter& painter, const Gfx::Color& background_color)
}
}
break;
case Play:
case Normal:
case Type::Play:
case Type::Normal:
draw_background_if_empty();
break;
case Waste:
case Type::Waste:
break;
default:
VERIFY_NOT_REACHED();
Expand Down Expand Up @@ -112,7 +112,7 @@ void CardStack::add_all_grabbed_cards(const Gfx::IntPoint& click_location, Nonnu
{
VERIFY(grabbed.is_empty());

if (m_type != Normal) {
if (m_type != Type::Normal) {
auto& top_card = peek();
if (top_card.rect().contains(click_location)) {
top_card.set_moving(true);
Expand Down Expand Up @@ -159,13 +159,13 @@ void CardStack::add_all_grabbed_cards(const Gfx::IntPoint& click_location, Nonnu
if (i != 0) {
bool color_match;
switch (movement_rule) {
case Alternating:
case MovementRule::Alternating:
color_match = card.color() != last_color;
break;
case Same:
case MovementRule::Same:
color_match = card.color() == last_color;
break;
case Any:
case MovementRule::Any:
color_match = true;
break;
}
Expand All @@ -189,40 +189,40 @@ void CardStack::add_all_grabbed_cards(const Gfx::IntPoint& click_location, Nonnu

bool CardStack::is_allowed_to_push(const Card& card, size_t stack_size, MovementRule movement_rule) const
{
if (m_type == Stock || m_type == Waste || m_type == Play)
if (m_type == Type::Stock || m_type == Type::Waste || m_type == Type::Play)
return false;

if (m_type == Normal && is_empty()) {
if (m_type == Type::Normal && is_empty()) {
// FIXME: proper solution for this
if (movement_rule == Alternating) {
if (movement_rule == MovementRule::Alternating) {
return card.value() == 12;
}
return true;
}

if (m_type == Foundation && is_empty())
if (m_type == Type::Foundation && is_empty())
return card.value() == 0;

if (!is_empty()) {
auto& top_card = peek();
if (top_card.is_upside_down())
return false;

if (m_type == Foundation) {
if (m_type == Type::Foundation) {
// Prevent player from dragging an entire stack of cards to the foundation stack
if (stack_size > 1)
return false;
return top_card.type() == card.type() && m_stack.size() == card.value();
} else if (m_type == Normal) {
} else if (m_type == Type::Normal) {
bool color_match;
switch (movement_rule) {
case Alternating:
case MovementRule::Alternating:
color_match = card.color() != top_card.color();
break;
case Same:
case MovementRule::Same:
color_match = card.color() == top_card.color();
break;
case Any:
case MovementRule::Any:
color_match = true;
break;
}
Expand All @@ -248,7 +248,7 @@ void CardStack::push(NonnullRefPtr<Card> card)
top_most_position.translate_by(m_rules.shift_x, m_rules.shift_y);
}

if (m_type == Stock)
if (m_type == Type::Stock)
card->set_upside_down(true);

card->set_position(top_most_position);
Expand All @@ -263,7 +263,7 @@ NonnullRefPtr<Card> CardStack::pop()
auto card = m_stack.take_last();

calculate_bounding_box();
if (m_type == Stock)
if (m_type == Type::Stock)
card->set_upside_down(false);

m_stack_positions.take_last();
Expand Down
20 changes: 10 additions & 10 deletions Userland/Libraries/LibCards/CardStack.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Cards {

class CardStack final : public RefCounted<CardStack> {
public:
enum Type {
enum class Type {
Invalid,
Stock,
Normal,
Expand All @@ -24,7 +24,7 @@ class CardStack final : public RefCounted<CardStack> {
Foundation
};

enum MovementRule {
enum class MovementRule {
Alternating,
Same,
Any,
Expand All @@ -50,8 +50,8 @@ class CardStack final : public RefCounted<CardStack> {
void move_to_stack(CardStack&);
void rebound_cards();

bool is_allowed_to_push(const Card&, size_t stack_size = 1, MovementRule movement_rule = Alternating) const;
void add_all_grabbed_cards(const Gfx::IntPoint& click_location, NonnullRefPtrVector<Card>& grabbed, MovementRule movement_rule = Alternating);
bool is_allowed_to_push(const Card&, size_t stack_size = 1, MovementRule movement_rule = MovementRule::Alternating) const;
void add_all_grabbed_cards(const Gfx::IntPoint& click_location, NonnullRefPtrVector<Card>& grabbed, MovementRule movement_rule = MovementRule::Alternating);
void draw(GUI::Painter&, const Gfx::Color& background_color);
void clear();

Expand All @@ -66,15 +66,15 @@ class CardStack final : public RefCounted<CardStack> {
constexpr StackRules rules_for_type(Type stack_type)
{
switch (stack_type) {
case Foundation:
case Type::Foundation:
return { 2, 1, 4, 1 };
case Normal:
case Type::Normal:
return { 0, 20, 1, 3 };
case Stock:
case Type::Stock:
return { 2, 1, 8, 1 };
case Waste:
case Type::Waste:
return { 0, 0, 1, 0 };
case Play:
case Type::Play:
return { 20, 0, 1, 0 };
default:
return {};
Expand All @@ -88,7 +88,7 @@ class CardStack final : public RefCounted<CardStack> {
Vector<Gfx::IntPoint> m_stack_positions;
Gfx::IntPoint m_position;
Gfx::IntRect m_bounding_box;
Type m_type { Invalid };
Type m_type { Type::Invalid };
StackRules m_rules;
bool m_focused { false };
Gfx::IntRect m_base;
Expand Down

0 comments on commit 56046d3

Please sign in to comment.