2020-03-09 15:09:22 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Till Mayer <till.mayer@web.de>
|
2023-01-05 17:38:50 +00:00
|
|
|
* Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
|
2022-02-16 18:44:57 +00:00
|
|
|
* Copyright (c) 2022, the SerenityOS developers.
|
2020-03-09 15:09:22 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-03-09 15:09:22 +00:00
|
|
|
*/
|
|
|
|
|
2021-05-05 13:42:59 +00:00
|
|
|
#include "Game.h"
|
2021-05-16 02:13:11 +00:00
|
|
|
#include <AK/Debug.h>
|
2021-07-23 23:09:23 +00:00
|
|
|
#include <AK/Random.h>
|
2020-03-09 15:09:22 +00:00
|
|
|
#include <LibGUI/Painter.h>
|
2021-05-05 16:19:10 +00:00
|
|
|
#include <LibGfx/Palette.h>
|
2020-03-09 15:09:22 +00:00
|
|
|
|
2021-05-05 13:54:52 +00:00
|
|
|
REGISTER_WIDGET(Solitaire, Game);
|
|
|
|
|
2021-05-05 13:42:59 +00:00
|
|
|
namespace Solitaire {
|
|
|
|
|
2021-05-05 18:16:02 +00:00
|
|
|
static constexpr uint8_t new_game_animation_delay = 2;
|
2021-05-05 03:00:53 +00:00
|
|
|
static constexpr int s_timer_interval_ms = 1000 / 60;
|
2020-03-09 15:09:22 +00:00
|
|
|
|
2023-01-05 17:38:50 +00:00
|
|
|
ErrorOr<NonnullRefPtr<Game>> Game::try_create()
|
2020-03-09 15:09:22 +00:00
|
|
|
{
|
2023-01-05 17:38:50 +00:00
|
|
|
auto game = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) Game()));
|
|
|
|
|
|
|
|
TRY(game->add_stack(Gfx::IntPoint { 10, 10 }, CardStack::Type::Stock));
|
|
|
|
TRY(game->add_stack(Gfx::IntPoint { 10 + Card::width + 10, 10 }, CardStack::Type::Waste));
|
|
|
|
TRY(game->add_stack(Gfx::IntPoint { 10 + Card::width + 10, 10 }, CardStack::Type::Play, game->stack_at_location(Waste)));
|
|
|
|
TRY(game->add_stack(Gfx::IntPoint { Game::width - 4 * Card::width - 40, 10 }, CardStack::Type::Foundation));
|
|
|
|
TRY(game->add_stack(Gfx::IntPoint { Game::width - 3 * Card::width - 30, 10 }, CardStack::Type::Foundation));
|
|
|
|
TRY(game->add_stack(Gfx::IntPoint { Game::width - 2 * Card::width - 20, 10 }, CardStack::Type::Foundation));
|
|
|
|
TRY(game->add_stack(Gfx::IntPoint { Game::width - Card::width - 10, 10 }, CardStack::Type::Foundation));
|
|
|
|
TRY(game->add_stack(Gfx::IntPoint { 10, 10 + Card::height + 10 }, CardStack::Type::Normal));
|
|
|
|
TRY(game->add_stack(Gfx::IntPoint { 10 + Card::width + 10, 10 + Card::height + 10 }, CardStack::Type::Normal));
|
|
|
|
TRY(game->add_stack(Gfx::IntPoint { 10 + 2 * Card::width + 20, 10 + Card::height + 10 }, CardStack::Type::Normal));
|
|
|
|
TRY(game->add_stack(Gfx::IntPoint { 10 + 3 * Card::width + 30, 10 + Card::height + 10 }, CardStack::Type::Normal));
|
|
|
|
TRY(game->add_stack(Gfx::IntPoint { 10 + 4 * Card::width + 40, 10 + Card::height + 10 }, CardStack::Type::Normal));
|
|
|
|
TRY(game->add_stack(Gfx::IntPoint { 10 + 5 * Card::width + 50, 10 + Card::height + 10 }, CardStack::Type::Normal));
|
|
|
|
TRY(game->add_stack(Gfx::IntPoint { 10 + 6 * Card::width + 60, 10 + Card::height + 10 }, CardStack::Type::Normal));
|
|
|
|
|
|
|
|
return game;
|
2020-03-09 15:09:22 +00:00
|
|
|
}
|
|
|
|
|
2023-01-05 17:38:50 +00:00
|
|
|
Game::Game() = default;
|
|
|
|
|
2020-03-09 15:09:22 +00:00
|
|
|
static float rand_float()
|
|
|
|
{
|
2021-07-23 23:09:23 +00:00
|
|
|
return get_random_uniform(RAND_MAX) / static_cast<float>(RAND_MAX);
|
2020-03-09 15:09:22 +00:00
|
|
|
}
|
|
|
|
|
2023-01-28 17:15:47 +00:00
|
|
|
void Game::deal_next_card()
|
|
|
|
{
|
|
|
|
VERIFY(m_state == State::NewGameAnimation);
|
|
|
|
|
|
|
|
auto& current_pile = stack_at_location(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).release_value_but_fixme_should_propagate_errors();
|
|
|
|
} else {
|
|
|
|
current_pile.push(m_new_deck.take_last()).release_value_but_fixme_should_propagate_errors();
|
|
|
|
++m_new_game_animation_pile;
|
|
|
|
}
|
|
|
|
|
|
|
|
update(current_pile.bounding_box());
|
|
|
|
|
|
|
|
if (m_new_game_animation_pile == piles.size()) {
|
|
|
|
auto& stock_pile = stack_at_location(Stock);
|
|
|
|
while (!m_new_deck.is_empty())
|
|
|
|
stock_pile.push(m_new_deck.take_last()).release_value_but_fixme_should_propagate_errors();
|
|
|
|
|
|
|
|
update(stock_pile.bounding_box());
|
|
|
|
|
|
|
|
m_state = State::WaitingForNewGame;
|
|
|
|
stop_timer();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-05 13:42:59 +00:00
|
|
|
void Game::timer_event(Core::TimerEvent&)
|
2020-03-09 15:09:22 +00:00
|
|
|
{
|
2023-01-28 17:00:30 +00:00
|
|
|
switch (m_state) {
|
|
|
|
case State::StartGameOverAnimationNextFrame: {
|
|
|
|
m_state = State::GameOverAnimation;
|
2021-06-12 20:11:54 +00:00
|
|
|
set_background_fill_enabled(false);
|
2023-01-28 17:00:30 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case State::GameOverAnimation: {
|
2023-01-06 13:00:58 +00:00
|
|
|
if (m_animation.position().x() >= Game::width || m_animation.card_rect().right() <= 0)
|
2020-03-09 15:09:22 +00:00
|
|
|
create_new_animation_card();
|
|
|
|
|
2021-05-05 16:19:10 +00:00
|
|
|
if (m_animation.tick())
|
2023-01-06 13:00:58 +00:00
|
|
|
update(m_animation.card_rect());
|
2023-01-28 17:00:30 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case State::NewGameAnimation: {
|
2021-07-15 15:02:42 +00:00
|
|
|
if (m_new_game_animation_delay < new_game_animation_delay) {
|
|
|
|
++m_new_game_animation_delay;
|
|
|
|
} else {
|
|
|
|
m_new_game_animation_delay = 0;
|
2023-01-28 17:15:47 +00:00
|
|
|
deal_next_card();
|
2021-07-15 15:02:42 +00:00
|
|
|
}
|
2023-01-28 17:00:30 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
2020-03-09 15:09:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-05 13:42:59 +00:00
|
|
|
void Game::create_new_animation_card()
|
2020-03-09 15:09:22 +00:00
|
|
|
{
|
2023-01-06 13:00:58 +00:00
|
|
|
auto suit = static_cast<Cards::Suit>(get_random_uniform(to_underlying(Cards::Suit::__Count)));
|
|
|
|
auto rank = static_cast<Cards::Rank>(get_random_uniform(to_underlying(Cards::Rank::__Count)));
|
|
|
|
Gfx::IntPoint position { get_random_uniform(Game::width - Card::width), get_random_uniform(Game::height / 8) };
|
2020-03-09 15:09:22 +00:00
|
|
|
|
2023-01-06 13:00:58 +00:00
|
|
|
int x_direction = position.x() > (Game::width / 2) ? -1 : 1;
|
|
|
|
m_animation = Animation(suit, rank, position, rand_float() + .4f, x_direction * (get_random_uniform(3) + 2), .6f + rand_float() * .4f);
|
2020-03-09 15:09:22 +00:00
|
|
|
}
|
|
|
|
|
2021-06-12 20:11:54 +00:00
|
|
|
void Game::set_background_fill_enabled(bool enabled)
|
|
|
|
{
|
|
|
|
Widget* widget = this;
|
|
|
|
while (widget) {
|
|
|
|
widget->set_fill_with_background_color(enabled);
|
|
|
|
widget = widget->parent_widget();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-05 13:42:59 +00:00
|
|
|
void Game::start_game_over_animation()
|
2020-03-09 15:09:22 +00:00
|
|
|
{
|
2023-01-28 17:00:30 +00:00
|
|
|
if (m_state == State::GameOverAnimation || m_state == State::StartGameOverAnimationNextFrame)
|
2020-03-09 15:09:22 +00:00
|
|
|
return;
|
|
|
|
|
2021-06-12 18:33:11 +00:00
|
|
|
m_last_move = {};
|
|
|
|
if (on_undo_availability_change)
|
|
|
|
on_undo_availability_change(false);
|
|
|
|
|
2020-03-09 15:09:22 +00:00
|
|
|
create_new_animation_card();
|
2021-06-12 16:47:11 +00:00
|
|
|
|
|
|
|
// We wait one frame, to make sure that the foundation stacks are repainted before we start.
|
|
|
|
// Otherwise, if the game ended from an attempt_to_move_card_to_foundations() move, the
|
|
|
|
// foundations could appear empty or otherwise incorrect.
|
2023-01-28 17:00:30 +00:00
|
|
|
m_state = State::StartGameOverAnimationNextFrame;
|
2021-05-05 16:36:18 +00:00
|
|
|
|
|
|
|
start_timer(s_timer_interval_ms);
|
2021-05-05 18:06:28 +00:00
|
|
|
|
|
|
|
if (on_game_end)
|
2021-05-25 15:44:25 +00:00
|
|
|
on_game_end(GameOverReason::Victory, m_score);
|
2020-03-09 15:09:22 +00:00
|
|
|
}
|
|
|
|
|
2021-05-05 13:42:59 +00:00
|
|
|
void Game::stop_game_over_animation()
|
2020-03-09 15:09:22 +00:00
|
|
|
{
|
2023-01-28 17:00:30 +00:00
|
|
|
if (m_state != State::GameOverAnimation)
|
2020-03-09 15:09:22 +00:00
|
|
|
return;
|
|
|
|
|
2021-06-12 20:11:54 +00:00
|
|
|
set_background_fill_enabled(true);
|
2023-01-28 17:00:30 +00:00
|
|
|
m_state = State::NewGameAnimation;
|
2020-03-09 15:09:22 +00:00
|
|
|
update();
|
2021-05-05 16:36:18 +00:00
|
|
|
|
|
|
|
stop_timer();
|
2020-03-09 15:09:22 +00:00
|
|
|
}
|
|
|
|
|
2021-05-15 03:59:23 +00:00
|
|
|
void Game::setup(Mode mode)
|
2020-03-09 15:09:22 +00:00
|
|
|
{
|
2023-01-28 17:00:30 +00:00
|
|
|
if (m_state == State::NewGameAnimation)
|
2021-05-18 12:59:49 +00:00
|
|
|
stop_timer();
|
|
|
|
|
2020-03-09 15:09:22 +00:00
|
|
|
stop_game_over_animation();
|
2021-05-15 03:59:23 +00:00
|
|
|
m_mode = mode;
|
2020-03-09 15:09:22 +00:00
|
|
|
|
2021-05-05 18:06:28 +00:00
|
|
|
if (on_game_end)
|
2021-05-25 15:44:25 +00:00
|
|
|
on_game_end(GameOverReason::NewGame, m_score);
|
2021-05-05 18:06:28 +00:00
|
|
|
|
2022-09-28 11:13:00 +00:00
|
|
|
for (auto& stack : stacks())
|
2023-03-06 13:17:01 +00:00
|
|
|
stack->clear();
|
2020-03-09 15:09:22 +00:00
|
|
|
|
2020-10-26 19:34:50 +00:00
|
|
|
m_new_deck.clear();
|
|
|
|
m_new_game_animation_pile = 0;
|
2021-05-25 16:27:53 +00:00
|
|
|
m_passes_left_before_punishment = recycle_rules().passes_allowed_before_punishment;
|
2020-10-26 19:34:50 +00:00
|
|
|
m_score = 0;
|
|
|
|
update_score(0);
|
2021-06-03 00:16:49 +00:00
|
|
|
if (on_undo_availability_change)
|
|
|
|
on_undo_availability_change(false);
|
2020-10-26 19:34:50 +00:00
|
|
|
|
2023-01-20 13:19:56 +00:00
|
|
|
m_new_deck = Cards::create_standard_deck(Cards::Shuffle::Yes).release_value_but_fixme_should_propagate_errors();
|
2020-03-09 15:09:22 +00:00
|
|
|
|
2022-09-28 16:16:47 +00:00
|
|
|
clear_moving_cards();
|
2021-08-05 10:12:18 +00:00
|
|
|
|
2023-01-28 17:00:30 +00:00
|
|
|
m_state = State::NewGameAnimation;
|
2021-05-05 03:00:53 +00:00
|
|
|
start_timer(s_timer_interval_ms);
|
2020-03-09 15:09:22 +00:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2021-06-03 07:43:28 +00:00
|
|
|
void Game::start_timer_if_necessary()
|
|
|
|
{
|
2023-01-28 17:00:30 +00:00
|
|
|
if (on_game_start && m_state == State::WaitingForNewGame) {
|
2021-06-03 07:43:28 +00:00
|
|
|
on_game_start();
|
2023-01-28 17:00:30 +00:00
|
|
|
m_state = State::GameInProgress;
|
2021-06-03 07:43:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-11 11:33:27 +00:00
|
|
|
void Game::score_move(CardStack& from, CardStack& to, bool inverse)
|
2021-06-03 00:16:49 +00:00
|
|
|
{
|
|
|
|
if (from.type() == CardStack::Type::Play && to.type() == CardStack::Type::Normal) {
|
|
|
|
update_score(5 * (inverse ? -1 : 1));
|
|
|
|
} else if (from.type() == CardStack::Type::Play && to.type() == CardStack::Type::Foundation) {
|
|
|
|
update_score(10 * (inverse ? -1 : 1));
|
|
|
|
} else if (from.type() == CardStack::Type::Normal && to.type() == CardStack::Type::Foundation) {
|
|
|
|
update_score(10 * (inverse ? -1 : 1));
|
|
|
|
} else if (from.type() == CardStack::Type::Foundation && to.type() == CardStack::Type::Normal) {
|
|
|
|
update_score(-15 * (inverse ? -1 : 1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-11 11:33:27 +00:00
|
|
|
void Game::score_flip(bool inverse)
|
|
|
|
{
|
|
|
|
update_score(5 * (inverse ? -1 : 1));
|
|
|
|
}
|
|
|
|
|
2021-05-05 13:42:59 +00:00
|
|
|
void Game::update_score(int to_add)
|
2020-03-09 15:09:22 +00:00
|
|
|
{
|
|
|
|
m_score = max(static_cast<int>(m_score) + to_add, 0);
|
2021-05-05 13:54:52 +00:00
|
|
|
|
|
|
|
if (on_score_update)
|
|
|
|
on_score_update(m_score);
|
2020-03-09 15:09:22 +00:00
|
|
|
}
|
|
|
|
|
2021-05-05 13:42:59 +00:00
|
|
|
void Game::keydown_event(GUI::KeyEvent& event)
|
2020-03-09 15:09:22 +00:00
|
|
|
{
|
2023-01-28 17:00:30 +00:00
|
|
|
if (is_moving_cards() || m_state == State::NewGameAnimation || m_state == State::GameOverAnimation) {
|
2022-12-14 15:36:06 +00:00
|
|
|
event.ignore();
|
2020-10-26 19:34:50 +00:00
|
|
|
return;
|
2022-12-14 15:36:06 +00:00
|
|
|
}
|
2020-10-26 19:34:50 +00:00
|
|
|
|
2021-06-03 07:43:28 +00:00
|
|
|
if (event.shift() && event.key() == KeyCode::Key_F12) {
|
2020-03-09 15:09:22 +00:00
|
|
|
start_game_over_animation();
|
2021-06-03 07:43:28 +00:00
|
|
|
} else if (event.key() == KeyCode::Key_Tab) {
|
2021-06-11 13:26:39 +00:00
|
|
|
auto_move_eligible_cards_to_foundations();
|
2022-09-29 10:33:12 +00:00
|
|
|
} else if (event.key() == KeyCode::Key_Space) {
|
2021-06-03 07:43:28 +00:00
|
|
|
draw_cards();
|
|
|
|
} else if (event.shift() && event.key() == KeyCode::Key_F11) {
|
2022-09-28 11:13:00 +00:00
|
|
|
if constexpr (SOLITAIRE_DEBUG) {
|
|
|
|
dump_layout();
|
|
|
|
}
|
2022-12-14 15:36:06 +00:00
|
|
|
} else {
|
|
|
|
event.ignore();
|
2021-06-03 07:43:28 +00:00
|
|
|
}
|
2020-03-09 15:09:22 +00:00
|
|
|
}
|
|
|
|
|
2021-05-05 13:42:59 +00:00
|
|
|
void Game::mousedown_event(GUI::MouseEvent& event)
|
2020-03-09 15:09:22 +00:00
|
|
|
{
|
2021-05-05 14:02:35 +00:00
|
|
|
GUI::Frame::mousedown_event(event);
|
2020-03-09 15:09:22 +00:00
|
|
|
|
2023-01-28 17:00:30 +00:00
|
|
|
if (m_state == State::NewGameAnimation || m_state == State::GameOverAnimation)
|
2020-03-09 15:09:22 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
auto click_location = event.position();
|
2022-09-28 11:13:00 +00:00
|
|
|
for (auto& to_check : stacks()) {
|
2023-03-06 13:17:01 +00:00
|
|
|
if (to_check->type() == CardStack::Type::Waste)
|
2021-05-15 03:09:41 +00:00
|
|
|
continue;
|
|
|
|
|
2023-03-06 13:17:01 +00:00
|
|
|
if (to_check->bounding_box().contains(click_location)) {
|
|
|
|
if (to_check->type() == CardStack::Type::Stock) {
|
2021-06-03 07:43:28 +00:00
|
|
|
draw_cards();
|
2023-03-06 13:17:01 +00:00
|
|
|
} else if (!to_check->is_empty()) {
|
|
|
|
auto& top_card = to_check->peek();
|
2020-03-09 15:09:22 +00:00
|
|
|
|
|
|
|
if (top_card.is_upside_down()) {
|
|
|
|
if (top_card.rect().contains(click_location)) {
|
|
|
|
top_card.set_upside_down(false);
|
2021-06-11 11:33:27 +00:00
|
|
|
score_flip();
|
2021-06-12 18:45:47 +00:00
|
|
|
start_timer_if_necessary();
|
2021-05-05 16:19:10 +00:00
|
|
|
update(top_card.rect());
|
2021-06-03 00:16:49 +00:00
|
|
|
remember_flip_for_undo(top_card);
|
2020-03-09 15:09:22 +00:00
|
|
|
}
|
2022-09-28 16:16:47 +00:00
|
|
|
} else if (!is_moving_cards()) {
|
2021-08-26 17:49:05 +00:00
|
|
|
if (is_auto_collecting() && attempt_to_move_card_to_foundations(to_check))
|
|
|
|
break;
|
|
|
|
|
2023-01-06 13:28:35 +00:00
|
|
|
if (event.button() == GUI::MouseButton::Secondary) {
|
|
|
|
preview_card(to_check, click_location);
|
|
|
|
} else {
|
2023-01-20 13:33:30 +00:00
|
|
|
pick_up_cards_from_stack(to_check, click_location, Cards::CardStack::MovementRule::Alternating).release_value_but_fixme_should_propagate_errors();
|
2023-01-06 13:28:35 +00:00
|
|
|
m_mouse_down_location = click_location;
|
|
|
|
m_mouse_down = true;
|
|
|
|
}
|
2022-09-28 16:16:47 +00:00
|
|
|
|
2021-06-12 18:45:47 +00:00
|
|
|
start_timer_if_necessary();
|
2020-03-09 15:09:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-05 13:42:59 +00:00
|
|
|
void Game::mouseup_event(GUI::MouseEvent& event)
|
2020-03-09 15:09:22 +00:00
|
|
|
{
|
2021-05-05 14:02:35 +00:00
|
|
|
GUI::Frame::mouseup_event(event);
|
2023-01-05 15:16:16 +00:00
|
|
|
clear_hovered_stack();
|
2020-03-09 15:09:22 +00:00
|
|
|
|
2023-01-06 13:28:35 +00:00
|
|
|
if (is_previewing_card()) {
|
|
|
|
clear_card_preview();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-01-28 17:00:30 +00:00
|
|
|
if (!is_moving_cards() || m_state == State::NewGameAnimation || m_state == State::GameOverAnimation)
|
2020-03-09 15:09:22 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
bool rebound = true;
|
2022-09-28 16:16:47 +00:00
|
|
|
if (auto target_stack = find_stack_to_drop_on(Cards::CardStack::MovementRule::Alternating); !target_stack.is_null()) {
|
|
|
|
auto& stack = *target_stack;
|
|
|
|
remember_move_for_undo(*moving_cards_source_stack(), stack, moving_cards());
|
2021-05-15 03:09:41 +00:00
|
|
|
|
2023-01-22 21:28:28 +00:00
|
|
|
drop_cards_on_stack(stack, Cards::CardStack::MovementRule::Alternating).release_value_but_fixme_should_propagate_errors();
|
2020-03-09 15:09:22 +00:00
|
|
|
|
2022-09-28 16:16:47 +00:00
|
|
|
if (moving_cards_source_stack()->type() == CardStack::Type::Play)
|
|
|
|
pop_waste_to_play_stack();
|
2020-03-09 15:09:22 +00:00
|
|
|
|
2022-09-28 16:16:47 +00:00
|
|
|
score_move(*moving_cards_source_stack(), stack);
|
|
|
|
rebound = false;
|
2020-03-09 15:09:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (rebound) {
|
2022-09-28 16:16:47 +00:00
|
|
|
for (auto& to_intersect : moving_cards())
|
2020-03-09 15:09:22 +00:00
|
|
|
mark_intersecting_stacks_dirty(to_intersect);
|
|
|
|
|
2022-09-28 16:16:47 +00:00
|
|
|
moving_cards_source_stack()->rebound_cards();
|
|
|
|
update(moving_cards_source_stack()->bounding_box());
|
2020-03-09 15:09:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m_mouse_down = false;
|
|
|
|
}
|
|
|
|
|
2021-05-05 13:42:59 +00:00
|
|
|
void Game::mousemove_event(GUI::MouseEvent& event)
|
2020-03-09 15:09:22 +00:00
|
|
|
{
|
2021-05-05 14:02:35 +00:00
|
|
|
GUI::Frame::mousemove_event(event);
|
2020-03-09 15:09:22 +00:00
|
|
|
|
2023-01-28 17:00:30 +00:00
|
|
|
if (!m_mouse_down || m_state == State::NewGameAnimation || m_state == State::GameOverAnimation)
|
2020-03-09 15:09:22 +00:00
|
|
|
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);
|
|
|
|
|
2023-01-05 15:16:16 +00:00
|
|
|
if (auto target_stack = find_stack_to_drop_on(Cards::CardStack::MovementRule::Alternating)) {
|
|
|
|
if (target_stack != m_hovered_stack) {
|
|
|
|
clear_hovered_stack();
|
2023-01-04 23:44:22 +00:00
|
|
|
|
2023-01-05 15:16:16 +00:00
|
|
|
m_hovered_stack = move(target_stack);
|
|
|
|
m_hovered_stack->set_highlighted(true);
|
|
|
|
update(m_hovered_stack->bounding_box());
|
2023-01-04 23:44:22 +00:00
|
|
|
}
|
|
|
|
} else {
|
2023-01-05 15:16:16 +00:00
|
|
|
clear_hovered_stack();
|
2023-01-04 23:44:22 +00:00
|
|
|
}
|
|
|
|
|
2022-09-28 16:16:47 +00:00
|
|
|
for (auto& to_intersect : moving_cards()) {
|
2020-03-09 15:09:22 +00:00
|
|
|
mark_intersecting_stacks_dirty(to_intersect);
|
2023-03-06 13:17:01 +00:00
|
|
|
to_intersect->rect().translate_by(dx, dy);
|
|
|
|
update(to_intersect->rect());
|
2020-03-09 15:09:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m_mouse_down_location = click_location;
|
|
|
|
}
|
|
|
|
|
2021-05-05 13:42:59 +00:00
|
|
|
void Game::doubleclick_event(GUI::MouseEvent& event)
|
2020-03-09 15:09:22 +00:00
|
|
|
{
|
2021-05-05 14:02:35 +00:00
|
|
|
GUI::Frame::doubleclick_event(event);
|
2020-03-09 15:09:22 +00:00
|
|
|
|
2023-01-28 17:00:30 +00:00
|
|
|
if (m_state == State::GameOverAnimation) {
|
2021-05-15 03:59:23 +00:00
|
|
|
setup(mode());
|
2020-03-09 15:09:22 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-01-28 17:15:47 +00:00
|
|
|
if (m_state == State::NewGameAnimation) {
|
|
|
|
while (m_state == State::NewGameAnimation)
|
|
|
|
deal_next_card();
|
2020-10-26 19:34:50 +00:00
|
|
|
return;
|
2023-01-28 17:15:47 +00:00
|
|
|
}
|
2020-10-26 19:34:50 +00:00
|
|
|
|
2020-03-10 13:47:30 +00:00
|
|
|
auto click_location = event.position();
|
2022-09-28 11:13:00 +00:00
|
|
|
for (auto& to_check : stacks()) {
|
2023-03-06 13:17:01 +00:00
|
|
|
if (to_check->type() != CardStack::Type::Normal && to_check->type() != CardStack::Type::Play)
|
2020-03-09 15:09:22 +00:00
|
|
|
continue;
|
|
|
|
|
2023-03-06 13:17:01 +00:00
|
|
|
if (to_check->bounding_box().contains(click_location) && !to_check->is_empty()) {
|
|
|
|
auto& top_card = to_check->peek();
|
2021-06-11 13:26:39 +00:00
|
|
|
if (!top_card.is_upside_down() && top_card.rect().contains(click_location))
|
|
|
|
attempt_to_move_card_to_foundations(to_check);
|
2020-03-09 15:09:22 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-05 13:42:59 +00:00
|
|
|
void Game::check_for_game_over()
|
2020-03-09 15:09:22 +00:00
|
|
|
{
|
2021-06-12 16:47:11 +00:00
|
|
|
for (auto foundationID : foundations) {
|
2022-09-28 11:13:00 +00:00
|
|
|
auto& foundation = stack_at_location(foundationID);
|
2021-06-12 16:47:11 +00:00
|
|
|
|
|
|
|
if (foundation.count() != Card::card_count)
|
2020-03-09 15:09:22 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
start_game_over_animation();
|
|
|
|
}
|
|
|
|
|
2021-06-03 07:43:28 +00:00
|
|
|
void Game::draw_cards()
|
|
|
|
{
|
2022-09-28 11:13:00 +00:00
|
|
|
auto& waste = stack_at_location(Waste);
|
|
|
|
auto& stock = stack_at_location(Stock);
|
|
|
|
auto& play = stack_at_location(Play);
|
2021-06-03 07:43:28 +00:00
|
|
|
|
|
|
|
if (stock.is_empty()) {
|
|
|
|
if (waste.is_empty() && play.is_empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
update(waste.bounding_box());
|
|
|
|
update(play.bounding_box());
|
|
|
|
|
2023-03-06 13:17:01 +00:00
|
|
|
Vector<NonnullRefPtr<Card>> moved_cards;
|
2021-06-03 07:43:28 +00:00
|
|
|
while (!play.is_empty()) {
|
|
|
|
auto card = play.pop();
|
2023-01-20 12:45:02 +00:00
|
|
|
stock.push(card).release_value_but_fixme_should_propagate_errors();
|
2021-06-03 20:55:48 +00:00
|
|
|
moved_cards.prepend(card);
|
2021-06-03 07:43:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
while (!waste.is_empty()) {
|
|
|
|
auto card = waste.pop();
|
2023-01-20 12:45:02 +00:00
|
|
|
stock.push(card).release_value_but_fixme_should_propagate_errors();
|
2021-06-03 20:55:48 +00:00
|
|
|
moved_cards.prepend(card);
|
2021-06-03 07:43:28 +00:00
|
|
|
}
|
|
|
|
|
2021-06-03 20:55:48 +00:00
|
|
|
remember_move_for_undo(waste, stock, moved_cards);
|
|
|
|
|
2021-06-03 07:43:28 +00:00
|
|
|
if (m_passes_left_before_punishment == 0)
|
|
|
|
update_score(recycle_rules().punishment);
|
|
|
|
else
|
|
|
|
--m_passes_left_before_punishment;
|
|
|
|
|
|
|
|
update(stock.bounding_box());
|
|
|
|
} else {
|
|
|
|
auto play_bounding_box = play.bounding_box();
|
2023-01-20 12:45:02 +00:00
|
|
|
play.take_all(waste).release_value_but_fixme_should_propagate_errors();
|
2021-06-03 07:43:28 +00:00
|
|
|
|
|
|
|
size_t cards_to_draw = 0;
|
|
|
|
switch (m_mode) {
|
|
|
|
case Mode::SingleCardDraw:
|
|
|
|
cards_to_draw = 1;
|
|
|
|
break;
|
|
|
|
case Mode::ThreeCardDraw:
|
|
|
|
cards_to_draw = 3;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
update(stock.bounding_box());
|
|
|
|
|
2023-03-06 13:17:01 +00:00
|
|
|
Vector<NonnullRefPtr<Card>> cards_drawn;
|
2021-06-03 07:43:28 +00:00
|
|
|
for (size_t i = 0; (i < cards_to_draw) && !stock.is_empty(); ++i) {
|
|
|
|
auto card = stock.pop();
|
2021-09-04 21:42:54 +00:00
|
|
|
cards_drawn.prepend(card);
|
2023-01-20 12:45:02 +00:00
|
|
|
play.push(move(card)).release_value_but_fixme_should_propagate_errors();
|
2021-06-03 07:43:28 +00:00
|
|
|
}
|
|
|
|
|
2021-06-03 20:55:48 +00:00
|
|
|
remember_move_for_undo(stock, play, cards_drawn);
|
|
|
|
|
2021-06-03 07:43:28 +00:00
|
|
|
if (play.bounding_box().size().width() > play_bounding_box.size().width())
|
|
|
|
update(play.bounding_box());
|
|
|
|
else
|
|
|
|
update(play_bounding_box);
|
|
|
|
}
|
2021-06-12 18:45:47 +00:00
|
|
|
|
|
|
|
start_timer_if_necessary();
|
2021-06-03 07:43:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Game::pop_waste_to_play_stack()
|
|
|
|
{
|
2022-09-28 11:13:00 +00:00
|
|
|
auto& waste = stack_at_location(Waste);
|
|
|
|
auto& play = stack_at_location(Play);
|
2021-06-03 07:43:28 +00:00
|
|
|
if (play.is_empty() && !waste.is_empty()) {
|
|
|
|
auto card = waste.pop();
|
2022-09-28 16:16:47 +00:00
|
|
|
moving_cards().append(card);
|
2023-01-20 12:45:02 +00:00
|
|
|
play.push(move(card)).release_value_but_fixme_should_propagate_errors();
|
2021-06-03 07:43:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-11 13:26:39 +00:00
|
|
|
bool Game::attempt_to_move_card_to_foundations(CardStack& from)
|
2021-06-03 07:43:28 +00:00
|
|
|
{
|
2021-06-11 13:26:39 +00:00
|
|
|
if (from.is_empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto& top_card = from.peek();
|
|
|
|
if (top_card.is_upside_down())
|
|
|
|
return false;
|
|
|
|
|
2021-06-03 07:43:28 +00:00
|
|
|
bool card_was_moved = false;
|
|
|
|
|
2021-06-11 19:05:53 +00:00
|
|
|
for (auto foundationID : foundations) {
|
2022-09-28 11:13:00 +00:00
|
|
|
auto& foundation = stack_at_location(foundationID);
|
2021-06-11 19:05:53 +00:00
|
|
|
|
|
|
|
if (foundation.is_allowed_to_push(top_card)) {
|
|
|
|
update(from.bounding_box());
|
|
|
|
|
|
|
|
auto card = from.pop();
|
|
|
|
|
|
|
|
mark_intersecting_stacks_dirty(card);
|
2023-01-20 12:45:02 +00:00
|
|
|
foundation.push(card).release_value_but_fixme_should_propagate_errors();
|
2021-06-11 19:05:53 +00:00
|
|
|
|
2023-03-06 13:17:01 +00:00
|
|
|
Vector<NonnullRefPtr<Card>> moved_card;
|
2021-06-11 19:05:53 +00:00
|
|
|
moved_card.append(card);
|
|
|
|
remember_move_for_undo(from, foundation, moved_card);
|
|
|
|
|
|
|
|
score_move(from, foundation);
|
|
|
|
|
|
|
|
update(foundation.bounding_box());
|
|
|
|
|
|
|
|
card_was_moved = true;
|
|
|
|
break;
|
|
|
|
}
|
2021-06-11 13:26:39 +00:00
|
|
|
}
|
2021-06-03 07:43:28 +00:00
|
|
|
|
2021-06-12 16:47:11 +00:00
|
|
|
if (card_was_moved) {
|
|
|
|
if (from.type() == CardStack::Type::Play)
|
|
|
|
pop_waste_to_play_stack();
|
|
|
|
|
2021-06-12 18:45:47 +00:00
|
|
|
start_timer_if_necessary();
|
2021-06-12 16:47:11 +00:00
|
|
|
check_for_game_over();
|
|
|
|
}
|
2021-06-03 07:43:28 +00:00
|
|
|
|
2021-06-11 13:26:39 +00:00
|
|
|
return card_was_moved;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Game::auto_move_eligible_cards_to_foundations()
|
|
|
|
{
|
2022-09-29 10:27:17 +00:00
|
|
|
while (true) {
|
|
|
|
bool card_was_moved = false;
|
|
|
|
for (auto& to_check : stacks()) {
|
2023-03-06 13:17:01 +00:00
|
|
|
if (to_check->type() != CardStack::Type::Normal && to_check->type() != CardStack::Type::Play)
|
2022-09-29 10:27:17 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (attempt_to_move_card_to_foundations(to_check))
|
|
|
|
card_was_moved = true;
|
|
|
|
}
|
2021-06-03 07:43:28 +00:00
|
|
|
|
2022-09-29 10:27:17 +00:00
|
|
|
if (!card_was_moved)
|
|
|
|
break;
|
2021-06-03 07:43:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-05 13:42:59 +00:00
|
|
|
void Game::paint_event(GUI::PaintEvent& event)
|
2020-03-09 15:09:22 +00:00
|
|
|
{
|
2022-08-20 13:17:37 +00:00
|
|
|
Gfx::Color background_color = this->background_color();
|
2020-03-09 15:09:22 +00:00
|
|
|
|
2021-05-05 16:19:10 +00:00
|
|
|
GUI::Frame::paint_event(event);
|
2020-03-09 15:09:22 +00:00
|
|
|
|
|
|
|
GUI::Painter painter(*this);
|
2021-05-05 14:02:35 +00:00
|
|
|
painter.add_clip_rect(frame_inner_rect());
|
|
|
|
painter.add_clip_rect(event.rect());
|
2020-03-09 15:09:22 +00:00
|
|
|
|
2023-01-28 17:00:30 +00:00
|
|
|
if (m_state == State::GameOverAnimation) {
|
2021-05-05 16:19:10 +00:00
|
|
|
m_animation.draw(painter);
|
|
|
|
return;
|
|
|
|
}
|
2020-03-09 15:09:22 +00:00
|
|
|
|
2022-09-28 16:16:47 +00:00
|
|
|
if (is_moving_cards()) {
|
|
|
|
for (auto& card : moving_cards())
|
2023-03-06 13:17:01 +00:00
|
|
|
card->clear(painter, background_color);
|
2021-05-05 16:19:10 +00:00
|
|
|
}
|
2020-03-09 15:09:22 +00:00
|
|
|
|
2022-09-28 11:13:00 +00:00
|
|
|
for (auto& stack : stacks()) {
|
2023-03-06 13:17:01 +00:00
|
|
|
stack->paint(painter, background_color);
|
2021-05-05 16:19:10 +00:00
|
|
|
}
|
2020-03-09 15:09:22 +00:00
|
|
|
|
2022-09-28 16:16:47 +00:00
|
|
|
if (is_moving_cards()) {
|
|
|
|
for (auto& card : moving_cards()) {
|
2023-03-06 13:17:01 +00:00
|
|
|
card->paint(painter);
|
|
|
|
card->save_old_position();
|
2020-03-09 15:09:22 +00:00
|
|
|
}
|
2020-10-26 19:34:50 +00:00
|
|
|
}
|
2020-03-09 15:09:22 +00:00
|
|
|
|
|
|
|
if (!m_mouse_down) {
|
2022-09-28 16:16:47 +00:00
|
|
|
if (is_moving_cards()) {
|
2020-03-09 15:09:22 +00:00
|
|
|
check_for_game_over();
|
2022-09-28 16:16:47 +00:00
|
|
|
for (auto& card : moving_cards())
|
2023-03-06 13:17:01 +00:00
|
|
|
card->set_moving(false);
|
2020-03-09 15:09:22 +00:00
|
|
|
}
|
|
|
|
|
2022-09-28 16:16:47 +00:00
|
|
|
clear_moving_cards();
|
2020-03-09 15:09:22 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-05 13:42:59 +00:00
|
|
|
|
2023-03-06 13:17:01 +00:00
|
|
|
void Game::remember_move_for_undo(CardStack& from, CardStack& to, Vector<NonnullRefPtr<Card>> moved_cards)
|
2021-06-03 00:16:49 +00:00
|
|
|
{
|
|
|
|
m_last_move.type = LastMove::Type::MoveCards;
|
|
|
|
m_last_move.from = &from;
|
|
|
|
m_last_move.cards = moved_cards;
|
|
|
|
m_last_move.to = &to;
|
|
|
|
if (on_undo_availability_change)
|
|
|
|
on_undo_availability_change(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Game::remember_flip_for_undo(Card& card)
|
|
|
|
{
|
2023-03-06 13:17:01 +00:00
|
|
|
Vector<NonnullRefPtr<Card>> cards;
|
2021-06-03 00:16:49 +00:00
|
|
|
cards.append(card);
|
|
|
|
m_last_move.type = LastMove::Type::FlipCard;
|
|
|
|
m_last_move.cards = cards;
|
|
|
|
if (on_undo_availability_change)
|
|
|
|
on_undo_availability_change(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Game::perform_undo()
|
|
|
|
{
|
|
|
|
if (m_last_move.type == LastMove::Type::Invalid)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (m_last_move.type == LastMove::Type::FlipCard) {
|
2023-03-06 13:17:01 +00:00
|
|
|
m_last_move.cards[0]->set_upside_down(true);
|
2021-06-03 00:16:49 +00:00
|
|
|
if (on_undo_availability_change)
|
|
|
|
on_undo_availability_change(false);
|
|
|
|
invalidate_layout();
|
2021-06-11 11:33:27 +00:00
|
|
|
score_flip(true);
|
2021-06-03 00:16:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_last_move.from->type() == CardStack::Type::Play && m_mode == Mode::SingleCardDraw) {
|
2022-09-28 11:13:00 +00:00
|
|
|
auto& waste = stack_at_location(Waste);
|
2021-06-03 00:16:49 +00:00
|
|
|
if (!m_last_move.from->is_empty())
|
2023-01-20 12:45:02 +00:00
|
|
|
waste.push(m_last_move.from->pop()).release_value_but_fixme_should_propagate_errors();
|
2021-06-03 00:16:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (auto& to_intersect : m_last_move.cards) {
|
|
|
|
mark_intersecting_stacks_dirty(to_intersect);
|
2023-01-20 12:45:02 +00:00
|
|
|
m_last_move.from->push(to_intersect).release_value_but_fixme_should_propagate_errors();
|
2021-12-02 12:53:33 +00:00
|
|
|
(void)m_last_move.to->pop();
|
2021-06-03 00:16:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (m_last_move.from->type() == CardStack::Type::Stock) {
|
2022-09-28 11:13:00 +00:00
|
|
|
auto& waste = stack_at_location(Waste);
|
|
|
|
auto& play = stack_at_location(Play);
|
2023-03-06 13:17:01 +00:00
|
|
|
Vector<NonnullRefPtr<Card>> cards_popped;
|
2021-06-03 00:16:49 +00:00
|
|
|
for (size_t i = 0; i < m_last_move.cards.size(); i++) {
|
|
|
|
if (!waste.is_empty()) {
|
|
|
|
auto card = waste.pop();
|
|
|
|
cards_popped.prepend(card);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (auto& card : cards_popped) {
|
2023-01-20 12:45:02 +00:00
|
|
|
play.push(card).release_value_but_fixme_should_propagate_errors();
|
2021-06-03 00:16:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-03 20:55:48 +00:00
|
|
|
if (m_last_move.from->type() == CardStack::Type::Waste && m_last_move.to->type() == CardStack::Type::Stock)
|
|
|
|
pop_waste_to_play_stack();
|
|
|
|
|
2021-06-03 00:16:49 +00:00
|
|
|
score_move(*m_last_move.from, *m_last_move.to, true);
|
|
|
|
|
|
|
|
m_last_move = {};
|
|
|
|
if (on_undo_availability_change)
|
|
|
|
on_undo_availability_change(false);
|
|
|
|
invalidate_layout();
|
|
|
|
}
|
|
|
|
|
2023-01-05 15:16:16 +00:00
|
|
|
void Game::clear_hovered_stack()
|
2023-01-04 23:44:22 +00:00
|
|
|
{
|
2023-01-05 15:16:16 +00:00
|
|
|
if (!m_hovered_stack)
|
2023-01-04 23:44:22 +00:00
|
|
|
return;
|
|
|
|
|
2023-01-05 15:16:16 +00:00
|
|
|
m_hovered_stack->set_highlighted(false);
|
|
|
|
update(m_hovered_stack->bounding_box());
|
|
|
|
m_hovered_stack = nullptr;
|
2023-01-04 23:44:22 +00:00
|
|
|
}
|
|
|
|
|
2021-05-05 13:42:59 +00:00
|
|
|
}
|