mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 20:47:45 +00:00
Solitaire: Add key combo to dump the state of the layout
Useful for chasing down bugs.
This commit is contained in:
parent
ce030ca584
commit
ddb278ab85
5 changed files with 85 additions and 1 deletions
|
@ -89,7 +89,6 @@ Card::Card(Type type, uint8_t value)
|
||||||
|
|
||||||
Gfx::Painter painter(m_front);
|
Gfx::Painter painter(m_front);
|
||||||
auto& font = Gfx::FontDatabase::default_bold_font();
|
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];
|
auto label = labels[value];
|
||||||
m_front->fill(Color::White);
|
m_front->fill(Color::White);
|
||||||
|
|
|
@ -6,6 +6,8 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <AK/Array.h>
|
||||||
|
#include <AK/Format.h>
|
||||||
#include <LibCore/Object.h>
|
#include <LibCore/Object.h>
|
||||||
#include <LibGUI/Painter.h>
|
#include <LibGUI/Painter.h>
|
||||||
#include <LibGfx/Bitmap.h>
|
#include <LibGfx/Bitmap.h>
|
||||||
|
@ -21,6 +23,9 @@ public:
|
||||||
static constexpr int width = 80;
|
static constexpr int width = 80;
|
||||||
static constexpr int height = 100;
|
static constexpr int height = 100;
|
||||||
static constexpr int card_count = 13;
|
static constexpr int card_count = 13;
|
||||||
|
static constexpr Array<StringView, card_count> labels = {
|
||||||
|
"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"
|
||||||
|
};
|
||||||
|
|
||||||
enum Type {
|
enum Type {
|
||||||
Clubs,
|
Clubs,
|
||||||
|
@ -67,3 +72,30 @@ private:
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<>
|
||||||
|
struct AK::Formatter<Solitaire::Card> : Formatter<FormatString> {
|
||||||
|
void format(FormatBuilder& builder, const Solitaire::Card& card)
|
||||||
|
{
|
||||||
|
StringView type;
|
||||||
|
|
||||||
|
switch (card.type()) {
|
||||||
|
case Solitaire::Card::Type::Clubs:
|
||||||
|
type = "C"sv;
|
||||||
|
break;
|
||||||
|
case Solitaire::Card::Type::Diamonds:
|
||||||
|
type = "D"sv;
|
||||||
|
break;
|
||||||
|
case Solitaire::Card::Type::Hearts:
|
||||||
|
type = "H"sv;
|
||||||
|
break;
|
||||||
|
case Solitaire::Card::Type::Spades:
|
||||||
|
type = "S"sv;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
VERIFY_NOT_REACHED();
|
||||||
|
}
|
||||||
|
|
||||||
|
Formatter<FormatString>::format(builder, "{:>2}{}", Solitaire::Card::labels[card.value()], type);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "Card.h"
|
#include "Card.h"
|
||||||
|
#include <AK/Format.h>
|
||||||
#include <AK/Vector.h>
|
#include <AK/Vector.h>
|
||||||
|
|
||||||
namespace Solitaire {
|
namespace Solitaire {
|
||||||
|
@ -28,6 +29,7 @@ public:
|
||||||
bool is_empty() const { return m_stack.is_empty(); }
|
bool is_empty() const { return m_stack.is_empty(); }
|
||||||
bool is_focused() const { return m_focused; }
|
bool is_focused() const { return m_focused; }
|
||||||
Type type() const { return m_type; }
|
Type type() const { return m_type; }
|
||||||
|
const NonnullRefPtrVector<Card>& stack() const { return m_stack; }
|
||||||
size_t count() const { return m_stack.size(); }
|
size_t count() const { return m_stack.size(); }
|
||||||
const Card& peek() const { return m_stack.last(); }
|
const Card& peek() const { return m_stack.last(); }
|
||||||
Card& peek() { return m_stack.last(); }
|
Card& peek() { return m_stack.last(); }
|
||||||
|
@ -84,3 +86,41 @@ private:
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<>
|
||||||
|
struct AK::Formatter<Solitaire::CardStack> : Formatter<FormatString> {
|
||||||
|
void format(FormatBuilder& builder, const Solitaire::CardStack& stack)
|
||||||
|
{
|
||||||
|
StringView type;
|
||||||
|
|
||||||
|
switch (stack.type()) {
|
||||||
|
case Solitaire::CardStack::Type::Stock:
|
||||||
|
type = "Stock"sv;
|
||||||
|
break;
|
||||||
|
case Solitaire::CardStack::Type::Normal:
|
||||||
|
type = "Normal"sv;
|
||||||
|
break;
|
||||||
|
case Solitaire::CardStack::Type::Foundation:
|
||||||
|
type = "Foundation"sv;
|
||||||
|
break;
|
||||||
|
case Solitaire::CardStack::Type::Waste:
|
||||||
|
type = "Waste"sv;
|
||||||
|
break;
|
||||||
|
case Solitaire::CardStack::Type::Play:
|
||||||
|
type = "Play"sv;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
VERIFY_NOT_REACHED();
|
||||||
|
}
|
||||||
|
|
||||||
|
StringBuilder cards;
|
||||||
|
bool first_card = true;
|
||||||
|
|
||||||
|
for (const auto& card : stack.stack()) {
|
||||||
|
cards.appendff("{}{}", (first_card ? "" : " "), card);
|
||||||
|
first_card = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
Formatter<FormatString>::format(builder, "{:<10} {:>16}: {}", type, stack.bounding_box(), cards.build());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "Game.h"
|
#include "Game.h"
|
||||||
|
#include <AK/Debug.h>
|
||||||
#include <LibGUI/Painter.h>
|
#include <LibGUI/Painter.h>
|
||||||
#include <LibGfx/Palette.h>
|
#include <LibGfx/Palette.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
@ -139,6 +140,8 @@ void Game::keydown_event(GUI::KeyEvent& event)
|
||||||
|
|
||||||
if (event.shift() && (event.key() == KeyCode::Key_F12))
|
if (event.shift() && (event.key() == KeyCode::Key_F12))
|
||||||
start_game_over_animation();
|
start_game_over_animation();
|
||||||
|
else if (event.shift() && (event.key() == KeyCode::Key_F11))
|
||||||
|
dump_layout();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Game::mousedown_event(GUI::MouseEvent& event)
|
void Game::mousedown_event(GUI::MouseEvent& event)
|
||||||
|
@ -466,4 +469,13 @@ void Game::paint_event(GUI::PaintEvent& event)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Game::dump_layout() const
|
||||||
|
{
|
||||||
|
if constexpr (SOLITAIRE_DEBUG) {
|
||||||
|
dbgln("------------------------------");
|
||||||
|
for (const auto& stack : m_stacks)
|
||||||
|
dbgln("{}", stack);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -114,6 +114,7 @@ private:
|
||||||
void stop_game_over_animation();
|
void stop_game_over_animation();
|
||||||
void create_new_animation_card();
|
void create_new_animation_card();
|
||||||
void check_for_game_over();
|
void check_for_game_over();
|
||||||
|
void dump_layout() const;
|
||||||
|
|
||||||
ALWAYS_INLINE CardStack& stack(StackLocation location)
|
ALWAYS_INLINE CardStack& stack(StackLocation location)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue