diff options
author | Andreas Kling <kling@serenityos.org> | 2021-01-12 12:03:28 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-01-12 12:04:23 +0100 |
commit | aa939c4b4b8a7eb1d22b166ebb5fb737d6e66714 (patch) | |
tree | c87472f6751c0fbb731858c8b0a4643a451a7f00 /Userland/Games/Solitaire | |
parent | b8d6a56fa354a268e3fbfa4fb6d146e9bf60103e (diff) | |
download | serenity-aa939c4b4b8a7eb1d22b166ebb5fb737d6e66714.zip |
Games: Move to Userland/Games/
Diffstat (limited to 'Userland/Games/Solitaire')
-rw-r--r-- | Userland/Games/Solitaire/CMakeLists.txt | 9 | ||||
-rw-r--r-- | Userland/Games/Solitaire/Card.cpp | 177 | ||||
-rw-r--r-- | Userland/Games/Solitaire/Card.h | 85 | ||||
-rw-r--r-- | Userland/Games/Solitaire/CardStack.cpp | 238 | ||||
-rw-r--r-- | Userland/Games/Solitaire/CardStack.h | 100 | ||||
-rw-r--r-- | Userland/Games/Solitaire/SolitaireWidget.cpp | 446 | ||||
-rw-r--r-- | Userland/Games/Solitaire/SolitaireWidget.h | 142 | ||||
-rw-r--r-- | Userland/Games/Solitaire/main.cpp | 85 |
8 files changed, 1282 insertions, 0 deletions
diff --git a/Userland/Games/Solitaire/CMakeLists.txt b/Userland/Games/Solitaire/CMakeLists.txt new file mode 100644 index 0000000000..4a9d4e6c71 --- /dev/null +++ b/Userland/Games/Solitaire/CMakeLists.txt @@ -0,0 +1,9 @@ +set(SOURCES + Card.cpp + CardStack.cpp + main.cpp + SolitaireWidget.cpp +) + +serenity_app(Solitaire ICON app-solitaire) +target_link_libraries(Solitaire LibGUI LibGfx LibCore) diff --git a/Userland/Games/Solitaire/Card.cpp b/Userland/Games/Solitaire/Card.cpp new file mode 100644 index 0000000000..23c3473632 --- /dev/null +++ b/Userland/Games/Solitaire/Card.cpp @@ -0,0 +1,177 @@ +/* + * Copyright (c) 2020, Till Mayer <till.mayer@web.de> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "Card.h" +#include <LibGUI/Widget.h> +#include <LibGfx/Font.h> +#include <LibGfx/FontDatabase.h> + +static const NonnullRefPtr<Gfx::CharacterBitmap> s_diamond = Gfx::CharacterBitmap::create_from_ascii( + " # " + " ### " + " ##### " + " ####### " + "#########" + " ####### " + " ##### " + " ### " + " # ", + 9, 9); + +static const NonnullRefPtr<Gfx::CharacterBitmap> s_heart = Gfx::CharacterBitmap::create_from_ascii( + " # # " + " ### ### " + "#########" + "#########" + "#########" + " ####### " + " ##### " + " ### " + " # ", + 9, 9); + +static const NonnullRefPtr<Gfx::CharacterBitmap> s_spade = Gfx::CharacterBitmap::create_from_ascii( + " # " + " ### " + " ##### " + " ####### " + "#########" + "#########" + " ## # ## " + " ### " + " ### ", + 9, 9); + +static const NonnullRefPtr<Gfx::CharacterBitmap> s_club = Gfx::CharacterBitmap::create_from_ascii( + " ### " + " ##### " + " ##### " + " ## ### ## " + "###########" + "###########" + "#### # ####" + " ## ### ## " + " ### ", + 11, 9); + +static RefPtr<Gfx::Bitmap> s_background; + +Card::Card(Type type, uint8_t value) + : m_rect(Gfx::IntRect({}, { width, height })) + , m_front(*Gfx::Bitmap::create(Gfx::BitmapFormat::RGB32, { width, height })) + , m_type(type) + , m_value(value) +{ + ASSERT(value < card_count); + Gfx::IntRect paint_rect({ 0, 0 }, { width, height }); + + if (s_background.is_null()) { + s_background = Gfx::Bitmap::create(Gfx::BitmapFormat::RGB32, { width, height }); + Gfx::Painter bg_painter(*s_background); + + s_background->fill(Color::White); + auto image = Gfx::Bitmap::load_from_file("/res/icons/solitaire/buggie-deck.png"); + ASSERT(!image.is_null()); + + float aspect_ratio = image->width() / static_cast<float>(image->height()); + auto target_size = Gfx::IntSize(static_cast<int>(aspect_ratio * (height - 5)), height - 5); + + bg_painter.draw_scaled_bitmap( + { { (width - target_size.width()) / 2, (height - target_size.height()) / 2 }, target_size }, + *image, image->rect()); + bg_painter.draw_rect(paint_rect, Color::Black); + } + + Gfx::Painter painter(m_front); + auto& font = Gfx::FontDatabase::default_bold_font(); + static const String labels[] = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" }; + + auto label = labels[value]; + m_front->fill(Color::White); + painter.draw_rect(paint_rect, Color::Black); + paint_rect.set_height(paint_rect.height() / 2); + paint_rect.shrink(10, 6); + + painter.draw_text(paint_rect, label, font, Gfx::TextAlignment::TopLeft, color()); + + NonnullRefPtr<Gfx::CharacterBitmap> symbol = s_diamond; + switch (m_type) { + case Diamonds: + symbol = s_diamond; + break; + case Clubs: + symbol = s_club; + break; + case Spades: + symbol = s_spade; + break; + case Hearts: + symbol = s_heart; + break; + default: + ASSERT_NOT_REACHED(); + } + + painter.draw_bitmap( + { paint_rect.x() + (font.width(label) - symbol->size().width()) / 2, font.glyph_height() + paint_rect.y() + 3 }, + symbol, color()); + + for (int y = height / 2; y < height; ++y) { + for (int x = 0; x < width; ++x) { + m_front->set_pixel(x, y, m_front->get_pixel(width - x - 1, height - y - 1)); + } + } +} + +Card::~Card() +{ +} + +void Card::draw(GUI::Painter& painter) const +{ + ASSERT(!s_background.is_null()); + painter.blit(position(), m_upside_down ? *s_background : *m_front, m_front->rect()); +} + +void Card::clear(GUI::Painter& painter, const Color& background_color) const +{ + painter.fill_rect({ old_positon(), { width, height } }, background_color); +} + +void Card::save_old_position() +{ + m_old_position = m_rect.location(); + m_old_position_valid = true; +} + +void Card::clear_and_draw(GUI::Painter& painter, const Color& background_color) +{ + if (is_old_position_valid()) + clear(painter, background_color); + + draw(painter); + save_old_position(); +} diff --git a/Userland/Games/Solitaire/Card.h b/Userland/Games/Solitaire/Card.h new file mode 100644 index 0000000000..8a5652d481 --- /dev/null +++ b/Userland/Games/Solitaire/Card.h @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2020, Till Mayer <till.mayer@web.de> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#include <LibCore/Object.h> +#include <LibGUI/Painter.h> +#include <LibGfx/Bitmap.h> +#include <LibGfx/CharacterBitmap.h> +#include <LibGfx/Rect.h> +#include <ctype.h> + +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; + + enum Type { + Clubs, + Diamonds, + Hearts, + Spades, + __Count + }; + + virtual ~Card() override; + + Gfx::IntRect& rect() { return m_rect; } + Gfx::IntPoint position() const { return m_rect.location(); } + const Gfx::IntPoint& old_positon() const { return m_old_position; } + uint8_t value() const { return m_value; }; + Type type() const { return m_type; } + + 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; } + Gfx::Color color() const { return (m_type == Diamonds || m_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; } + void set_upside_down(bool upside_down) { m_upside_down = upside_down; } + + void save_old_position(); + + void draw(GUI::Painter&) const; + void clear(GUI::Painter&, const Color& background_color) const; + void clear_and_draw(GUI::Painter&, const Color& background_color); + +private: + Card(Type type, uint8_t value); + + Gfx::IntRect m_rect; + NonnullRefPtr<Gfx::Bitmap> m_front; + Gfx::IntPoint m_old_position; + Type m_type; + uint8_t m_value; + bool m_old_position_valid { false }; + bool m_moving { false }; + bool m_upside_down { false }; +}; diff --git a/Userland/Games/Solitaire/CardStack.cpp b/Userland/Games/Solitaire/CardStack.cpp new file mode 100644 index 0000000000..bb2b1bade5 --- /dev/null +++ b/Userland/Games/Solitaire/CardStack.cpp @@ -0,0 +1,238 @@ +/* + * Copyright (c) 2020, Till Mayer <till.mayer@web.de> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "CardStack.h" + +CardStack::CardStack() + : m_position({ 0, 0 }) + , m_type(Invalid) + , m_base(m_position, { Card::width, Card::height }) +{ +} + +CardStack::CardStack(const Gfx::IntPoint& position, Type type) + : m_position(position) + , m_type(type) + , m_rules(rules_for_type(type)) + , m_base(m_position, { Card::width, Card::height }) +{ + ASSERT(type != Invalid); + calculate_bounding_box(); +} + +void CardStack::clear() +{ + m_stack.clear(); + m_stack_positions.clear(); +} + +void CardStack::draw(GUI::Painter& painter, const Gfx::Color& background_color) +{ + switch (m_type) { + case Stock: + if (is_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); + painter.draw_rect(m_base, background_color.darkened(0.5)); + } + break; + case Foundation: + if (is_empty() || (m_stack.size() == 1 && peek().is_moving())) { + painter.draw_rect(m_base, background_color.darkened(0.5)); + for (int y = 0; y < (m_base.height() - 4) / 8; ++y) { + for (int x = 0; x < (m_base.width() - 4) / 5; ++x) { + painter.draw_rect({ 4 + m_base.x() + x * 5, 4 + m_base.y() + y * 8, 1, 1 }, background_color.darkened(0.5)); + } + } + } + break; + case Waste: + if (is_empty() || (m_stack.size() == 1 && peek().is_moving())) + painter.draw_rect(m_base, background_color.darkened(0.5)); + break; + case Normal: + painter.draw_rect(m_base, background_color.darkened(0.5)); + break; + default: + ASSERT_NOT_REACHED(); + } + + if (is_empty()) + return; + + if (m_rules.shift_x == 0 && m_rules.shift_y == 0) { + auto& card = peek(); + card.draw(painter); + return; + } + + for (auto& card : m_stack) { + if (!card.is_moving()) + card.clear_and_draw(painter, background_color); + } + + m_dirty = false; +} + +void CardStack::rebound_cards() +{ + ASSERT(m_stack_positions.size() == m_stack.size()); + + size_t card_index = 0; + for (auto& card : m_stack) + card.set_position(m_stack_positions.at(card_index++)); +} + +void CardStack::add_all_grabbed_cards(const Gfx::IntPoint& click_location, NonnullRefPtrVector<Card>& grabbed) +{ + ASSERT(grabbed.is_empty()); + + if (m_type != Normal) { + auto& top_card = peek(); + if (top_card.rect().contains(click_location)) { + top_card.set_moving(true); + grabbed.append(top_card); + } + return; + } + + RefPtr<Card> last_intersect; + + for (auto& card : m_stack) { + if (card.rect().contains(click_location)) { + if (card.is_upside_down()) + continue; + + last_intersect = card; + } else if (!last_intersect.is_null()) { + if (grabbed.is_empty()) { + grabbed.append(*last_intersect); + last_intersect->set_moving(true); + } + + if (card.is_upside_down()) { + grabbed.clear(); + return; + } + + card.set_moving(true); + grabbed.append(card); + } + } + + if (grabbed.is_empty() && !last_intersect.is_null()) { + grabbed.append(*last_intersect); + last_intersect->set_moving(true); + } +} + +bool CardStack::is_allowed_to_push(const Card& card) const +{ + if (m_type == Stock || m_type == Waste) + return false; + + if (m_type == Normal && is_empty()) + return card.value() == 12; + + if (m_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) { + return top_card.type() == card.type() && m_stack.size() == card.value(); + } else if (m_type == Normal) { + return top_card.color() != card.color() && top_card.value() == card.value() + 1; + } + + ASSERT_NOT_REACHED(); + } + + return true; +} + +void CardStack::push(NonnullRefPtr<Card> card) +{ + auto size = m_stack.size(); + auto top_most_position = m_stack_positions.is_empty() ? m_position : m_stack_positions.last(); + + if (size && size % m_rules.step == 0) { + if (peek().is_upside_down()) + top_most_position.move_by(m_rules.shift_x, m_rules.shift_y_upside_down); + else + top_most_position.move_by(m_rules.shift_x, m_rules.shift_y); + } + + if (m_type == Stock) + card->set_upside_down(true); + + card->set_position(top_most_position); + + m_stack.append(card); + m_stack_positions.append(top_most_position); + calculate_bounding_box(); +} + +NonnullRefPtr<Card> CardStack::pop() +{ + auto card = m_stack.take_last(); + + calculate_bounding_box(); + if (m_type == Stock) + card->set_upside_down(false); + + m_stack_positions.take_last(); + return card; +} + +void CardStack::calculate_bounding_box() +{ + m_bounding_box = Gfx::IntRect(m_position, { Card::width, Card::height }); + + if (m_stack.is_empty()) + return; + + uint16_t width = 0; + uint16_t height = 0; + size_t card_position = 0; + for (auto& card : m_stack) { + if (card_position % m_rules.step == 0 && card_position) { + if (card.is_upside_down()) { + width += m_rules.shift_x; + height += m_rules.shift_y_upside_down; + } else { + width += m_rules.shift_x; + height += m_rules.shift_y; + } + } + ++card_position; + } + + m_bounding_box.set_size(Card::width + width, Card::height + height); +} diff --git a/Userland/Games/Solitaire/CardStack.h b/Userland/Games/Solitaire/CardStack.h new file mode 100644 index 0000000000..18bac7a027 --- /dev/null +++ b/Userland/Games/Solitaire/CardStack.h @@ -0,0 +1,100 @@ +/* + * Copyright (c) 2020, Till Mayer <till.mayer@web.de> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#include "Card.h" +#include <AK/Vector.h> + +class CardStack final { +public: + enum Type { + Invalid, + Stock, + Normal, + Waste, + Foundation + }; + + CardStack(); + CardStack(const Gfx::IntPoint& position, Type type); + + bool is_dirty() const { return m_dirty; } + bool is_empty() const { return m_stack.is_empty(); } + bool is_focused() const { return m_focused; } + Type type() const { return m_type; } + size_t count() const { return m_stack.size(); } + const Card& peek() const { return m_stack.last(); } + Card& peek() { return m_stack.last(); } + const Gfx::IntRect& bounding_box() const { return m_bounding_box; } + + void set_focused(bool focused) { m_focused = focused; } + void set_dirty() { m_dirty = true; }; + + void push(NonnullRefPtr<Card> card); + NonnullRefPtr<Card> pop(); + void rebound_cards(); + + bool is_allowed_to_push(const Card&) const; + void add_all_grabbed_cards(const Gfx::IntPoint& click_location, NonnullRefPtrVector<Card>& grabbed); + void draw(GUI::Painter&, const Gfx::Color& background_color); + void clear(); + +private: + struct StackRules { + uint8_t shift_x { 0 }; + uint8_t shift_y { 0 }; + uint8_t step { 1 }; + uint8_t shift_y_upside_down { 0 }; + }; + + constexpr StackRules rules_for_type(Type stack_type) + { + switch (stack_type) { + case Foundation: + return { 2, 1, 4, 1 }; + case Normal: + return { 0, 15, 1, 3 }; + case Stock: + case Waste: + return { 2, 1, 8, 1 }; + default: + return {}; + } + } + + void calculate_bounding_box(); + + NonnullRefPtrVector<Card> m_stack; + Vector<Gfx::IntPoint> m_stack_positions; + Gfx::IntPoint m_position; + Gfx::IntRect m_bounding_box; + Type m_type { Invalid }; + StackRules m_rules; + bool m_focused { false }; + bool m_dirty { false }; + Gfx::IntRect m_base; +}; diff --git a/Userland/Games/Solitaire/SolitaireWidget.cpp b/Userland/Games/Solitaire/SolitaireWidget.cpp new file mode 100644 index 0000000000..d6497db677 --- /dev/null +++ b/Userland/Games/Solitaire/SolitaireWidget.cpp @@ -0,0 +1,446 @@ +/* + * Copyright (c) 2020, Till Mayer <till.mayer@web.de> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "SolitaireWidget.h" +#include <LibCore/Timer.h> +#include <LibGUI/Painter.h> +#include <LibGUI/Window.h> +#include <time.h> + +static const Color s_background_color { Color::from_rgb(0x008000) }; +static constexpr uint8_t new_game_animation_delay = 5; + +SolitaireWidget::SolitaireWidget(GUI::Window& window, Function<void(uint32_t)>&& on_score_update) + : m_on_score_update(move(on_score_update)) +{ + set_fill_with_background_color(false); + + m_stacks[Stock] = CardStack({ 10, 10 }, CardStack::Type::Stock); + m_stacks[Waste] = CardStack({ 10 + Card::width + 10, 10 }, CardStack::Type::Waste); + m_stacks[Foundation4] = CardStack({ SolitaireWidget::width - Card::width - 10, 10 }, CardStack::Type::Foundation); + m_stacks[Foundation3] = CardStack({ SolitaireWidget::width - 2 * Card::width - 20, 10 }, CardStack::Type::Foundation); + m_stacks[Foundation2] = CardStack({ SolitaireWidget::width - 3 * Card::width - 30, 10 }, CardStack::Type::Foundation); + m_stacks[Foundation1] = CardStack({ SolitaireWidget::width - 4 * Card::width - 40, 10 }, CardStack::Type::Foundation); + m_stacks[Pile1] = CardStack({ 10, 10 + Card::height + 10 }, CardStack::Type::Normal); + m_stacks[Pile2] = CardStack({ 10 + Card::width + 10, 10 + Card::height + 10 }, CardStack::Type::Normal); + m_stacks[Pile3] = CardStack({ 10 + 2 * Card::width + 20, 10 + Card::height + 10 }, CardStack::Type::Normal); + m_stacks[Pile4] = CardStack({ 10 + 3 * Card::width + 30, 10 + Card::height + 10 }, CardStack::Type::Normal); + m_stacks[Pile5] = CardStack({ 10 + 4 * Card::width + 40, 10 + Card::height + 10 }, CardStack::Type::Normal); + m_stacks[Pile6] = CardStack({ 10 + 5 * Card::width + 50, 10 + Card::height + 10 }, CardStack::Type::Normal); + m_stacks[Pile7] = CardStack({ 10 + 6 * Card::width + 60, 10 + Card::height + 10 }, CardStack::Type::Normal); + + m_timer = Core::Timer::construct(1000 / 60, [&]() { tick(window); }); + m_timer->stop(); +} + +SolitaireWidget::~SolitaireWidget() +{ +} + +static float rand_float() +{ + return rand() / static_cast<float>(RAND_MAX); +} + +void SolitaireWidget::tick(GUI::Window& window) +{ + if (!is_visible() || !updates_enabled() || !window.is_visible_for_timer_purposes()) + return; + + if (m_game_over_animation) { + ASSERT(!m_animation.card().is_null()); + if (m_animation.card()->position().x() > SolitaireWidget::width || m_animation.card()->rect().right() < 0) + create_new_animation_card(); + + m_animation.tick(); + } + + if (m_has_to_repaint || m_game_over_animation || m_new_game_animation) { + m_repaint_all = false; + update(); + } +} + +void SolitaireWidget::create_new_animation_card() +{ + srand(time(nullptr)); + + auto card = Card::construct(static_cast<Card::Type>(rand() % Card::Type::__Count), rand() % Card::card_count); + card->set_position({ rand() % (SolitaireWidget::width - Card::width), rand() % (SolitaireWidget::height / 8) }); + + int x_sgn = card->position().x() > (SolitaireWidget::width / 2) ? -1 : 1; + m_animation = Animation(card, rand_float() + .4, x_sgn * ((rand() % 3) + 2), .6 + rand_float() * .4); +} + +void SolitaireWidget::start_game_over_animation() +{ + if (m_game_over_animation) + return; + + create_new_animation_card(); + m_game_over_animation = true; +} + +void SolitaireWidget::stop_game_over_animation() +{ + if (!m_game_over_animation) + return; + + m_game_over_animation = false; + update(); +} + +void SolitaireWidget::setup() +{ + stop_game_over_animation(); + m_timer->stop(); + + for (auto& stack : m_stacks) + stack.clear(); + + m_new_deck.clear(); + m_new_game_animation_pile = 0; + m_score = 0; + update_score(0); + + for (int i = 0; i < Card::card_count; ++i) { + m_new_deck.append(Card::construct(Card::Type::Clubs, i)); + m_new_deck.append(Card::construct(Card::Type::Spades, i)); + m_new_deck.append(Card::construct(Card::Type::Hearts, i)); + m_new_deck.append(Card::construct(Card::Type::Diamonds, i)); + } + + srand(time(nullptr)); + for (uint8_t i = 0; i < 200; ++i) + m_new_deck.append(m_new_deck.take(rand() % m_new_deck.size())); + + m_new_game_animation = true; + m_timer->start(); + update(); +} + +void SolitaireWidget::update_score(int to_add) +{ + m_score = max(static_cast<int>(m_score) + to_add, 0); + m_on_score_update(m_score); +} + +void SolitaireWidget::keydown_event(GUI::KeyEvent& event) +{ + if (m_new_game_animation || m_game_over_animation) + return; + + if (event.key() == KeyCode::Key_F12) + start_game_over_animation(); +} + +void SolitaireWidget::mousedown_event(GUI::MouseEvent& event) +{ + GUI::Widget::mousedown_event(event); + + if (m_new_game_animation || m_game_over_animation) + return; + + auto click_location = event.position(); + for (auto& to_check : m_stacks) { + if (to_check.bounding_box().contains(click_location)) { + if (to_check.type() == CardStack::Type::Stock) { + auto& waste = stack(Waste); + auto& stock = stack(Stock); + + if (stock.is_empty()) { + if (waste.is_empty()) + return; + + while (!waste.is_empty()) { + auto card = waste.pop(); + stock.push(card); + } + + stock.set_dirty(); + waste.set_dirty(); + m_has_to_repaint = true; + update_score(-100); + } else { + move_card(stock, waste); + } + } else if (!to_check.is_empty()) { + auto& top_card = to_check.peek(); + + if (top_card.is_upside_down()) { + if (top_card.rect().contains(click_location)) { + top_card.set_upside_down(false); + to_check.set_dirty(); + update_score(5); + m_has_to_repaint = true; + } + } else if (m_focused_cards.is_empty()) { + to_check.add_all_grabbed_cards(click_location, m_focused_cards); + m_mouse_down_location = click_location; + to_check.set_focused(true); + m_focused_stack = &to_check; + m_mouse_down = true; + } + } + break; + } + } +} + +void SolitaireWidget::mouseup_event(GUI::MouseEvent& event) +{ + GUI::Widget::mouseup_event(event); + + if (!m_focused_stack || m_focused_cards.is_empty() || m_game_over_animation || m_new_game_animation) + return; + + bool rebound = true; + for (auto& stack : m_stacks) { + if (stack.is_focused()) + continue; + + 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))) { + for (auto& to_intersect : m_focused_cards) { + mark_intersecting_stacks_dirty(to_intersect); + stack.push(to_intersect); + m_focused_stack->pop(); + } + + m_focused_stack->set_dirty(); + stack.set_dirty(); + + if (m_focused_stack->type() == CardStack::Type::Waste && stack.type() == CardStack::Type::Normal) { + update_score(5); + } else if (m_focused_stack->type() == CardStack::Type::Waste && stack.type() == CardStack::Type::Foundation) { + update_score(10); + } else if (m_focused_stack->type() == CardStack::Type::Normal && stack.type() == CardStack::Type::Foundation) { + update_score(10); + } else if (m_focused_stack->type() == CardStack::Type::Foundation && stack.type() == CardStack::Type::Normal) { + update_score(-15); + } + + rebound = false; + break; + } + } + } + } + + if (rebound) { + for (auto& to_intersect : m_focused_cards) + mark_intersecting_stacks_dirty(to_intersect); + + m_focused_stack->rebound_cards(); + m_focused_stack->set_dirty(); + } + + m_mouse_down = false; + m_has_to_repaint = true; +} + +void SolitaireWidget::mousemove_event(GUI::MouseEvent& event) +{ + GUI::Widget::mousemove_event(event); + + if (!m_mouse_down || m_game_over_animation || m_new_game_animation) + return; + + auto click_location = event.position(); + int dx = click_location.dx_relative_to(m_mouse_down_location); + int dy = click_location.dy_relative_to(m_mouse_down_location); + + for (auto& to_intersect : m_focused_cards) { + mark_intersecting_stacks_dirty(to_intersect); + to_intersect.rect().move_by(dx, dy); + } + + m_mouse_down_location = click_location; + m_has_to_repaint = true; +} + +void SolitaireWidget::doubleclick_event(GUI::MouseEvent& event) +{ + GUI::Widget::doubleclick_event(event); + + if (m_game_over_animation) { + start_game_over_animation(); + setup(); + return; + } + + if (m_new_game_animation) + return; + + auto click_location = event.position(); + for (auto& to_check : m_stacks) { + if (to_check.type() == CardStack::Type::Foundation || to_check.type() == CardStack::Type::Stock) + continue; + + if (to_check.bounding_box().contains(click_location) && !to_check.is_empty()) { + auto& top_card = to_check.peek(); + if (!top_card.is_upside_down() && top_card.rect().contains(click_location)) { + if (stack(Foundation1).is_allowed_to_push(top_card)) + move_card(to_check, stack(Foundation1)); + else if (stack(Foundation2).is_allowed_to_push(top_card)) + move_card(to_check, stack(Foundation2)); + else if (stack(Foundation3).is_allowed_to_push(top_card)) + move_card(to_check, stack(Foundation3)); + else if (stack(Foundation4).is_allowed_to_push(top_card)) + move_card(to_check, stack(Foundation4)); + else + break; + + update_score(10); + } + break; + } + } + + m_has_to_repaint = true; +} + +void SolitaireWidget::check_for_game_over() +{ + for (auto& stack : m_stacks) { + if (stack.type() != CardStack::Type::Foundation) + continue; + if (stack.count() != Card::card_count) + return; + } + + start_game_over_animation(); +} + +void SolitaireWidget::move_card(CardStack& from, CardStack& to) +{ + auto card = from.pop(); + + card->set_moving(true); + m_focused_cards.clear(); + m_focused_cards.append(card); + mark_intersecting_stacks_dirty(card); + to.push(card); + + from.set_dirty(); + to.set_dirty(); + + m_has_to_repaint = true; +} + +void SolitaireWidget::mark_intersecting_stacks_dirty(Card& intersecting_card) +{ + for (auto& stack : m_stacks) { + if (intersecting_card.rect().intersects(stack.bounding_box())) { + stack.set_dirty(); + m_has_to_repaint = true; + } + } +} + +void SolitaireWidget::paint_event(GUI::PaintEvent& event) +{ + GUI::Widget::paint_event(event); + + m_has_to_repaint = false; + if (m_game_over_animation && m_repaint_all) + return; + + GUI::Painter painter(*this); + + if (m_repaint_all) { + /* Only start the timer when update() got called from the + window manager, or else we might end up with a blank screen */ + if (!m_timer->is_active()) + m_timer->start(); + + painter.fill_rect(event.rect(), s_background_color); + + for (auto& stack : m_stacks) + stack.draw(painter, s_background_color); + } else if (m_game_over_animation && !m_animation.card().is_null()) { + m_animation.card()->draw(painter); + } else if (m_new_game_animation) { + if (m_new_game_animation_delay < new_game_animation_delay) { + ++m_new_game_animation_delay; + } else { + m_new_game_animation_delay = 0; + auto& current_pile = stack(piles.at(m_new_game_animation_pile)); + + if (current_pile.count() < m_new_game_animation_pile) { + auto card = m_new_deck.take_last(); + card->set_upside_down(true); + current_pile.push(card); + } else { + current_pile.push(m_new_deck.take_last()); + ++m_new_game_animation_pile; + } + current_pile.set_dirty(); + + if (m_new_game_animation_pile == piles.size()) { + while (!m_new_deck.is_empty()) + stack(Stock).push(m_new_deck.take_last()); + stack(Stock).set_dirty(); + m_new_game_animation = false; + } + } + } + + if (!m_game_over_animation && !m_repaint_all) { + if (!m_focused_cards.is_empty()) { + for (auto& focused_card : m_focused_cards) + focused_card.clear(painter, s_background_color); + } + + for (auto& stack : m_stacks) { + if (stack.is_dirty()) + stack.draw(painter, s_background_color); + } + + if (!m_focused_cards.is_empty()) { + for (auto& focused_card : m_focused_cards) { + focused_card.draw(painter); + focused_card.save_old_position(); + } + } + } + + m_repaint_all = true; + if (!m_mouse_down) { + if (!m_focused_cards.is_empty()) { + check_for_game_over(); + for (auto& card : m_focused_cards) + card.set_moving(false); + m_focused_cards.clear(); + } + + if (m_focused_stack) { + m_focused_stack->set_focused(false); + m_focused_stack = nullptr; + } + } +} diff --git a/Userland/Games/Solitaire/SolitaireWidget.h b/Userland/Games/Solitaire/SolitaireWidget.h new file mode 100644 index 0000000000..213251715d --- /dev/null +++ b/Userland/Games/Solitaire/SolitaireWidget.h @@ -0,0 +1,142 @@ +/* + * Copyright (c) 2020, Till Mayer <till.mayer@web.de> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#include "CardStack.h" +#include <LibGUI/Painter.h> +#include <LibGUI/Widget.h> + +class SolitaireWidget final : public GUI::Widget { + C_OBJECT(SolitaireWidget) +public: + static constexpr int width = 640; + static constexpr int height = 480; + + virtual ~SolitaireWidget() override; + void setup(); + +private: + SolitaireWidget(GUI::Window&, Function<void(uint32_t)>&& on_score_update); + + class Animation { + public: + Animation() + { + } + + Animation(RefPtr<Card> animation_card, float gravity, int x_vel, float bouncyness) + : m_animation_card(animation_card) + , m_gravity(gravity) + , m_x_velocity(x_vel) + , m_bouncyness(bouncyness) + { + } + + RefPtr<Card> card() { return m_animation_card; } + + void tick() + { + ASSERT(!m_animation_card.is_null()); + m_y_velocity += m_gravity; + + if (m_animation_card->position().y() + Card::height + m_y_velocity > SolitaireWidget::height + 1 && m_y_velocity > 0) { + m_y_velocity = min((m_y_velocity * -m_bouncyness), -8.f); + m_animation_card->rect().set_y(SolitaireWidget::height - Card::height); + m_animation_card->rect().move_by(m_x_velocity, 0); + } else { + m_animation_card->rect().move_by(m_x_velocity, m_y_velocity); + } + } + + private: + RefPtr<Card> m_animation_card; + float m_gravity { 0 }; + int m_x_velocity { 0 }; + float m_y_velocity { 0 }; + float m_bouncyness { 0 }; + }; + + enum StackLocation { + Stock, + Waste, + Foundation1, + Foundation2, + Foundation3, + Foundation4, + Pile1, + Pile2, + Pile3, + Pile4, + Pile5, + Pile6, + Pile7, + __Count + }; + static constexpr Array piles = { Pile1, Pile2, Pile3, Pile4, Pile5, Pile6, Pile7 }; + + void mark_intersecting_stacks_dirty(Card& intersecting_card); + void update_score(int to_add); + void move_card(CardStack& from, CardStack& to); + void start_game_over_animation(); + void stop_game_over_animation(); + void create_new_animation_card(); + void check_for_game_over(); + void tick(GUI::Window&); + + ALWAYS_INLINE CardStack& stack(StackLocation location) + { + return m_stacks[location]; + } + + virtual void paint_event(GUI::PaintEvent&) override; + virtual void mousedown_event(GUI::MouseEvent&) override; + virtual void mouseup_event(GUI::MouseEvent&) override; + virtual void mousemove_event(GUI::MouseEvent&) override; + virtual void doubleclick_event(GUI::MouseEvent&) override; + virtual void keydown_event(GUI::KeyEvent&) override; + + RefPtr<Core::Timer> m_timer; + NonnullRefPtrVector<Card> m_focused_cards; + NonnullRefPtrVector<Card> m_new_deck; + CardStack m_stacks[StackLocation::__Count]; + CardStack* m_focused_stack { nullptr }; + Gfx::IntPoint m_mouse_down_location; + + bool m_mouse_down { false }; + bool m_repaint_all { true }; + bool m_has_to_repaint { true }; + + Animation m_animation; + bool m_game_over_animation { false }; + + bool m_new_game_animation { false }; + uint8_t m_new_game_animation_pile { 0 }; + uint8_t m_new_game_animation_delay { 0 }; + + uint32_t m_score { 0 }; + Function<void(uint32_t)> m_on_score_update; +}; diff --git a/Userland/Games/Solitaire/main.cpp b/Userland/Games/Solitaire/main.cpp new file mode 100644 index 0000000000..de04b18218 --- /dev/null +++ b/Userland/Games/Solitaire/main.cpp @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2020, Till Mayer <till.mayer@web.de> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "SolitaireWidget.h" +#include <LibGUI/Action.h> +#include <LibGUI/Application.h> +#include <LibGUI/Icon.h> +#include <LibGUI/Menu.h> +#include <LibGUI/MenuBar.h> +#include <LibGUI/Window.h> +#include <stdio.h> +#include <unistd.h> + +int main(int argc, char** argv) +{ + auto app = GUI::Application::construct(argc, argv); + auto app_icon = GUI::Icon::default_icon("app-solitaire"); + + if (pledge("stdio rpath shared_buffer", nullptr) < 0) { + perror("pledge"); + return 1; + } + + if (unveil("/res", "r") < 0) { + perror("unveil"); + return 1; + } + + if (unveil(nullptr, nullptr) < 0) { + perror("unveil"); + return 1; + } + + auto window = GUI::Window::construct(); + + window->set_resizable(false); + window->resize(SolitaireWidget::width, SolitaireWidget::height); + + auto widget = SolitaireWidget::construct(window, [&](uint32_t score) { + window->set_title(String::formatted("Score: {} - Solitaire", score)); + }); + + auto menubar = GUI::MenuBar::construct(); + auto& app_menu = menubar->add_menu("Solitaire"); + + app_menu.add_action(GUI::Action::create("New game", { Mod_None, Key_F2 }, [&](auto&) { + widget->setup(); + })); + app_menu.add_separator(); + app_menu.add_action(GUI::CommonActions::make_quit_action([&](auto&) { app->quit(); })); + + auto& help_menu = menubar->add_menu("Help"); + help_menu.add_action(GUI::CommonActions::make_about_action("Solitaire", app_icon, window)); + + app->set_menubar(move(menubar)); + window->set_main_widget(widget); + window->set_icon(app_icon.bitmap_for_size(16)); + window->show(); + widget->setup(); + + return app->exec(); +} |