1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 07:47:35 +00:00

Solitaire: Ability to automatically solve the end of the game

In Solitaire, when the stock stack is empty and all cards have been
revealed, finishing the game is trivial, since you only need to sort the
already visible cards onto the foundation stacks. To simplify this, the
game will now give you the option to finish the game automatically if
these conditions are met. It then sorts the cards with a delay of 100ms
between each card.
This commit is contained in:
david072 2023-11-11 13:41:49 +01:00 committed by Andreas Kling
parent 999a44969d
commit 6a4e3d9002
4 changed files with 108 additions and 2 deletions

View file

@ -2,6 +2,7 @@
* Copyright (c) 2020, Till Mayer <till.mayer@web.de>
* Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
* Copyright (c) 2022, the SerenityOS developers.
* Copyright (c) 2023, David Ganz <david.g.ganz@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -16,6 +17,7 @@ namespace Solitaire {
static constexpr uint8_t new_game_animation_delay = 2;
static constexpr int s_timer_interval_ms = 1000 / 60;
static constexpr int s_timer_solving_interval_ms = 100;
ErrorOr<NonnullRefPtr<Game>> Game::try_create()
{
@ -100,6 +102,10 @@ void Game::timer_event(Core::TimerEvent&)
}
break;
}
case State::Solving: {
step_solve();
break;
}
default:
break;
}
@ -270,10 +276,16 @@ void Game::mousedown_event(GUI::MouseEvent& event)
start_timer_if_necessary();
update(top_card.rect());
remember_flip_for_undo(top_card);
if (on_move)
on_move();
}
} else if (!is_moving_cards()) {
if (is_auto_collecting() && attempt_to_move_card_to_foundations(to_check))
if (is_auto_collecting() && attempt_to_move_card_to_foundations(to_check)) {
if (on_move)
on_move();
break;
}
if (event.button() == GUI::MouseButton::Secondary) {
preview_card(to_check, click_location);
@ -316,6 +328,9 @@ void Game::mouseup_event(GUI::MouseEvent& event)
score_move(*moving_cards_source_stack(), stack);
rebound = false;
if (on_move)
on_move();
}
if (rebound) {
@ -400,6 +415,8 @@ void Game::check_for_game_over()
return;
}
if (has_timer())
stop_timer();
start_game_over_animation();
}
@ -662,6 +679,50 @@ void Game::perform_undo()
invalidate_layout();
}
bool Game::can_solve()
{
if (m_state != State::GameInProgress)
return false;
for (auto const& stack : stacks()) {
switch (stack->type()) {
case Cards::CardStack::Type::Waste:
case Cards::CardStack::Type::Stock:
if (!stack->is_empty())
return false;
break;
case Cards::CardStack::Type::Normal:
if (!stack->is_empty() && stack->stack().first()->is_upside_down())
return false;
break;
default:
break;
}
}
return true;
}
void Game::start_solving()
{
if (!can_solve())
return;
m_state = State::Solving;
start_timer(s_timer_solving_interval_ms);
}
void Game::step_solve()
{
for (auto& stack : stacks()) {
if (stack->type() != Cards::CardStack::Type::Normal)
continue;
if (attempt_to_move_card_to_foundations(stack))
break;
}
}
void Game::clear_hovered_stack()
{
if (!m_hovered_stack)