diff options
author | Till Mayer <till.mayer@web.de> | 2020-03-09 16:09:22 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-03-09 21:36:59 +0100 |
commit | fe5cc7ce68267e1c846eb11839a6609bd03b505c (patch) | |
tree | f40901ab44e3db8c83ba7939e83b7944256ecf85 /Games/Solitaire | |
parent | c6e54d2a49cf70ffebbeced28dba34405c6d7e41 (diff) | |
download | serenity-fe5cc7ce68267e1c846eb11839a6609bd03b505c.zip |
Games: Added solitaire
Added a solitaire game. Currently there are graphics missing on some
of the cards, but the game is fully functional.
Press F12 to show the game-over animation manually.
Diffstat (limited to 'Games/Solitaire')
-rw-r--r-- | Games/Solitaire/Card.cpp | 176 | ||||
-rw-r--r-- | Games/Solitaire/Card.h | 85 | ||||
-rw-r--r-- | Games/Solitaire/CardStack.cpp | 244 | ||||
-rw-r--r-- | Games/Solitaire/CardStack.h | 80 | ||||
-rw-r--r-- | Games/Solitaire/Makefile | 11 | ||||
-rw-r--r-- | Games/Solitaire/SolitaireWidget.cpp | 436 | ||||
-rw-r--r-- | Games/Solitaire/SolitaireWidget.h | 133 | ||||
-rw-r--r-- | Games/Solitaire/main.cpp | 68 |
8 files changed, 1233 insertions, 0 deletions
diff --git a/Games/Solitaire/Card.cpp b/Games/Solitaire/Card.cpp new file mode 100644 index 0000000000..de0c3ca370 --- /dev/null +++ b/Games/Solitaire/Card.cpp @@ -0,0 +1,176 @@ +/* + * 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> + +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::Rect({}, { width, height })) + , m_front(Gfx::Bitmap::create(Gfx::BitmapFormat::RGB32, { width, height })) + , m_type(type) + , m_value(value) +{ + ASSERT(value < card_count); + Gfx::Rect 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/buggie.png"); + ASSERT(!image.is_null()); + + float aspect_ratio = image->width() / static_cast<float>(image->height()); + auto target_size = Gfx::Size(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::Font::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::draw_complete(GUI::Painter& painter, const Color& background_color) +{ + if (is_old_position_valid()) + clear(painter, background_color); + + draw(painter); + save_old_position(); +} diff --git a/Games/Solitaire/Card.h b/Games/Solitaire/Card.h new file mode 100644 index 0000000000..a2dcab197a --- /dev/null +++ b/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::Rect& rect() { return m_rect; } + Gfx::Point position() const { return m_rect.location(); } + const Gfx::Point& 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::Point 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 draw_complete(GUI::Painter&, const Color& background_color); + +private: + Card(Type type, uint8_t value); + + Gfx::Rect m_rect; + NonnullRefPtr<Gfx::Bitmap> m_front; + Gfx::Point 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/Games/Solitaire/CardStack.cpp b/Games/Solitaire/CardStack.cpp new file mode 100644 index 0000000000..354c0d1b65 --- /dev/null +++ b/Games/Solitaire/CardStack.cpp @@ -0,0 +1,244 @@ +/* + * 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_shift_x(0) + , m_shift_y(0) + , m_step(1) + , m_base(m_position, { Card::width, Card::height }) +{ +} + +CardStack::CardStack(const Gfx::Point& position, Type type, uint8_t shift_x, uint8_t shift_y, uint8_t step) + : m_position(position) + , m_type(type) + , m_shift_x(shift_x) + , m_shift_y(shift_y) + , m_step(step) + , m_base(m_position, { Card::width, Card::height }) +{ + ASSERT(step && 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, Color::Black); + } + break; + case Foundation: + if (is_empty() || (m_stack.size() == 1 && peek().is_moving())) { + painter.draw_rect(m_base, Color::DarkGray); + 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 }, Color::DarkGray); + } + } + } + break; + case Waste: + if (is_empty() || (m_stack.size() == 1 && peek().is_moving())) + painter.draw_rect(m_base, Color::DarkGray); + break; + case Normal: + painter.draw_rect(m_base, Color::DarkGray); + break; + default: + ASSERT_NOT_REACHED(); + } + + if (is_empty()) + return; + + if (m_shift_x == 0 && m_shift_y == 0) { + auto& card = peek(); + card.draw(painter); + return; + } + + for (auto& card : m_stack) { + if (!card.is_moving()) + card.draw_complete(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::Point& 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) +{ + int size = m_stack.size(); + int ud_shift = (m_type == Normal) ? 3 : 1; + auto top_most_position = m_stack_positions.is_empty() ? m_position : m_stack_positions.last(); + + if (size && size % m_step == 0) { + if (peek().is_upside_down()) + top_most_position.move_by(m_shift_x, ((m_shift_y == 0) ? 0 : ud_shift)); + else + top_most_position.move_by(m_shift_x, m_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::Rect(m_position, { Card::width, Card::height }); + + if (m_stack.is_empty()) + return; + + uint16_t width = 0; + uint16_t height = 0; + int card_position = 0; + for (auto& card : m_stack) { + if (card_position % m_step == 0 && card_position) { + if (card.is_upside_down() && m_type != Stock) { + int ud_shift = (m_type == Normal) ? 3 : 1; + width += m_shift_x; + height += (m_shift_y == 0) ? 0 : ud_shift; + } else { + width += m_shift_x; + height += m_shift_y; + } + } + ++card_position; + } + + m_bounding_box.set_size(Card::width + width, Card::height + height); +} diff --git a/Games/Solitaire/CardStack.h b/Games/Solitaire/CardStack.h new file mode 100644 index 0000000000..f8721a219c --- /dev/null +++ b/Games/Solitaire/CardStack.h @@ -0,0 +1,80 @@ +/* + * 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::Point& position, Type type, uint8_t shift_x, uint8_t shift_y, uint8_t step = 1); + + 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::Rect& 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::Point& click_location, NonnullRefPtrVector<Card>& grabbed); + void draw(GUI::Painter&, const Gfx::Color& background_color); + void clear(); + +private: + void calculate_bounding_box(); + + NonnullRefPtrVector<Card> m_stack; + Vector<Gfx::Point> m_stack_positions; + Gfx::Point m_position; + Gfx::Rect m_bounding_box; + Type m_type { Invalid }; + uint8_t m_shift_x { 0 }; + uint8_t m_shift_y { 0 }; + uint8_t m_step {}; + bool m_focused { false }; + bool m_dirty { false }; + Gfx::Rect m_base; +}; diff --git a/Games/Solitaire/Makefile b/Games/Solitaire/Makefile new file mode 100644 index 0000000000..209d3f64cf --- /dev/null +++ b/Games/Solitaire/Makefile @@ -0,0 +1,11 @@ +OBJS = \ + SolitaireWidget.o\ + CardStack.o\ + Card.o\ + main.o + +PROGRAM = Solitaire + +LIB_DEPS = GUI Gfx IPC Core + +include ../../Makefile.common diff --git a/Games/Solitaire/SolitaireWidget.cpp b/Games/Solitaire/SolitaireWidget.cpp new file mode 100644 index 0000000000..7865be88f4 --- /dev/null +++ b/Games/Solitaire/SolitaireWidget.cpp @@ -0,0 +1,436 @@ +/* + * 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) }; + +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, 2, 1, 8); + m_stacks[Waste] = CardStack({ 10 + Card::width + 10, 10 }, CardStack::Type::Waste, 2, 1, 8); + m_stacks[Foundation4] = CardStack({ SolitaireWidget::width - Card::width - 10, 10 }, CardStack::Type::Foundation, 2, 1, 4); + m_stacks[Foundation3] = CardStack({ SolitaireWidget::width - 2 * Card::width - 20, 10 }, CardStack::Type::Foundation, 2, 1, 4); + m_stacks[Foundation2] = CardStack({ SolitaireWidget::width - 3 * Card::width - 30, 10 }, CardStack::Type::Foundation, 2, 1, 4); + m_stacks[Foundation1] = CardStack({ SolitaireWidget::width - 4 * Card::width - 40, 10 }, CardStack::Type::Foundation, 2, 1, 4); + m_stacks[Pile1] = CardStack({ 10, 10 + Card::height + 10 }, CardStack::Type::Normal, 0, 15); + m_stacks[Pile2] = CardStack({ 10 + Card::width + 10, 10 + Card::height + 10 }, CardStack::Type::Normal, 0, 15); + m_stacks[Pile3] = CardStack({ 10 + 2 * Card::width + 20, 10 + Card::height + 10 }, CardStack::Type::Normal, 0, 15); + m_stacks[Pile4] = CardStack({ 10 + 3 * Card::width + 30, 10 + Card::height + 10 }, CardStack::Type::Normal, 0, 15); + m_stacks[Pile5] = CardStack({ 10 + 4 * Card::width + 40, 10 + Card::height + 10 }, CardStack::Type::Normal, 0, 15); + m_stacks[Pile6] = CardStack({ 10 + 5 * Card::width + 50, 10 + Card::height + 10 }, CardStack::Type::Normal, 0, 15); + m_stacks[Pile7] = CardStack({ 10 + 6 * Card::width + 60, 10 + Card::height + 10 }, CardStack::Type::Normal, 0, 15); + + 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); +} + +static void make_pile(NonnullRefPtrVector<Card>& cards, CardStack& stack, uint8_t count) +{ + for (int i = 1; i < count; ++i) { + auto card = cards.take_last(); + card->set_upside_down(true); + stack.push(card); + } + + stack.push(cards.take_last()); +} + +void SolitaireWidget::tick(GUI::Window& window) +{ + if (!is_visible() || !updates_enabled() || !window.is_visible_for_timer_purposes()) + return; + + if (m_game_over_animation) { + 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_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) + 3), .4 + rand_float() * .6); +} + +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; + m_repaint_all = true; + update(); +} + +void SolitaireWidget::setup() +{ + stop_game_over_animation(); + + for (auto& stack : m_stacks) + stack.clear(); + + NonnullRefPtrVector<Card> cards; + for (int i = 0; i < Card::card_count; ++i) { + cards.append(Card::construct(Card::Type::Clubs, i)); + cards.append(Card::construct(Card::Type::Spades, i)); + cards.append(Card::construct(Card::Type::Hearts, i)); + cards.append(Card::construct(Card::Type::Diamonds, i)); + } + + srand(time(nullptr)); + for (int i = 0; i < 200; ++i) + cards.append(cards.take(rand() % cards.size())); + + make_pile(cards, stack(Pile1), 1); + make_pile(cards, stack(Pile2), 2); + make_pile(cards, stack(Pile3), 3); + make_pile(cards, stack(Pile4), 4); + make_pile(cards, stack(Pile5), 5); + make_pile(cards, stack(Pile6), 6); + make_pile(cards, stack(Pile7), 7); + + while (!cards.is_empty()) + stack(Stock).push(cards.take_last()); + + m_score = 0; + update_score(0); + 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 (event.key() == KeyCode::Key_F12) + start_game_over_animation(); +} + +void SolitaireWidget::mousedown_event(GUI::MouseEvent& event) +{ + GUI::Widget::mousedown_event(event); + + if (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 { + 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) + 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()) + || stack.bounding_box().contains(event.position())) { + 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) + 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; + } + + auto click_location = event.position(); + + for (auto& to_check : m_stacks) { + if (to_check.type() == CardStack::Type::Foundation) + 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.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) { + 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(); + } + } + } else if (m_animation.card() != nullptr) + m_animation.card()->draw(painter); + + 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/Games/Solitaire/SolitaireWidget.h b/Games/Solitaire/SolitaireWidget.h new file mode 100644 index 0000000000..cd1d8135ca --- /dev/null +++ b/Games/Solitaire/SolitaireWidget.h @@ -0,0 +1,133 @@ +/* + * 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) + { + } + + 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 + }; + + 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&); + + 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; + Animation m_animation; + CardStack* m_focused_stack { nullptr }; + CardStack m_stacks[StackLocation::__Count]; + Gfx::Point m_mouse_down_location; + bool m_mouse_down { false }; + bool m_repaint_all { true }; + bool m_has_to_repaint { true }; + bool m_game_over_animation { false }; + uint32_t m_score { 0 }; + Function<void(uint32_t)> m_on_score_update; +}; diff --git a/Games/Solitaire/main.cpp b/Games/Solitaire/main.cpp new file mode 100644 index 0000000000..4ce35678c1 --- /dev/null +++ b/Games/Solitaire/main.cpp @@ -0,0 +1,68 @@ +/* + * 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/Menu.h> +#include <LibGUI/MenuBar.h> +#include <LibGUI/Window.h> +#include <stdio.h> + +int main(int argc, char** argv) +{ + GUI::Application app(argc, argv); + + if (pledge("stdio rpath shared_buffer", nullptr) < 0) { + perror("pledge"); + return 1; + } + + auto window = GUI::Window::construct(); + + window->set_resizable(false); + window->set_rect(300, 100, SolitaireWidget::width, SolitaireWidget::height); + + auto widget = SolitaireWidget::construct(window, [&](uint32_t score) { + window->set_title(String::format("Solitaire - Score: %u", score)); + }); + + auto menu_bar = make<GUI::MenuBar>(); + auto app_menu = GUI::Menu::construct("Solitaire"); + + app_menu->add_action(GUI::Action::create("Restart game", [&](auto&) { widget->setup(); })); + app_menu->add_separator(); + app_menu->add_action(GUI::CommonActions::make_quit_action([&](auto&) { app.quit(); })); + menu_bar->add_menu(move(app_menu)); + + app.set_menubar(move(menu_bar)); + window->set_main_widget(widget); + window->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/app-solitaire.png")); + window->show(); + widget->setup(); + + return app.exec(); +} |