From 0a806837b557b289130b19cf26333bd39cf85608 Mon Sep 17 00:00:00 2001 From: david072 Date: Sun, 12 Nov 2023 11:37:52 +0100 Subject: [PATCH] LibCards/CardStack: Helper to update the cards' disabled flags This adds a little helper in Cards::CardStack that updates the disabled flags for its cards depending on a movement rule. It does this by searching from the bottom up for the last card that is valid. It then sets the disabled flag for all cards above that card, if it isn't upside down. --- Userland/Libraries/LibCards/CardStack.cpp | 52 +++++++++++++++++++++++ Userland/Libraries/LibCards/CardStack.h | 3 ++ 2 files changed, 55 insertions(+) diff --git a/Userland/Libraries/LibCards/CardStack.cpp b/Userland/Libraries/LibCards/CardStack.cpp index 11e44fc9600..9a6aa642972 100644 --- a/Userland/Libraries/LibCards/CardStack.cpp +++ b/Userland/Libraries/LibCards/CardStack.cpp @@ -1,5 +1,6 @@ /* * Copyright (c) 2020, Till Mayer + * Copyright (c) 2023, David Ganz * * SPDX-License-Identifier: BSD-2-Clause */ @@ -205,6 +206,57 @@ ErrorOr CardStack::add_all_grabbed_cards(Gfx::IntPoint click_location, Vec return {}; } +void CardStack::update_disabled_cards(MovementRule movement_rule) +{ + if (m_stack.is_empty()) + return; + + for (auto& card : m_stack) + card->set_disabled(false); + + Optional last_valid_card = {}; + uint8_t last_rank; + Color last_color; + for (size_t idx = m_stack.size(); idx > 0; idx--) { + auto i = idx - 1; + auto card = m_stack[i]; + if (card->is_upside_down()) { + if (!last_valid_card.has_value()) + last_valid_card = i + 1; + break; + } + + if (i != m_stack.size() - 1) { + bool color_valid; + switch (movement_rule) { + case MovementRule::Alternating: + color_valid = card->color() != last_color; + break; + case MovementRule::Same: + color_valid = card->color() == last_color; + break; + case MovementRule::Any: + color_valid = true; + break; + } + + if (!color_valid || to_underlying(card->rank()) != last_rank + 1) { + last_valid_card = i + 1; + break; + } + } + + last_rank = to_underlying(card->rank()); + last_color = card->color(); + } + + if (!last_valid_card.has_value()) + return; + + for (size_t i = 0; i < last_valid_card.value(); i++) + m_stack[i]->set_disabled(true); +} + bool CardStack::is_allowed_to_push(Card const& card, size_t stack_size, MovementRule movement_rule) const { if (m_type == Type::Stock || m_type == Type::Waste || m_type == Type::Play) diff --git a/Userland/Libraries/LibCards/CardStack.h b/Userland/Libraries/LibCards/CardStack.h index 4e3fdafee51..b65b7684d08 100644 --- a/Userland/Libraries/LibCards/CardStack.h +++ b/Userland/Libraries/LibCards/CardStack.h @@ -1,5 +1,6 @@ /* * Copyright (c) 2020, Till Mayer + * Copyright (c) 2023, David Ganz * * SPDX-License-Identifier: BSD-2-Clause */ @@ -51,6 +52,8 @@ public: bool is_allowed_to_push(Card const&, size_t stack_size = 1, MovementRule movement_rule = MovementRule::Alternating) const; ErrorOr add_all_grabbed_cards(Gfx::IntPoint click_location, Vector>& grabbed, MovementRule movement_rule = MovementRule::Alternating); + void update_disabled_cards(MovementRule); + bool preview_card(Gfx::IntPoint click_location); void clear_card_preview();