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

Minesweeper: Implement some feature requests.

Someone was playing this game and suggested a number of improvements so here
we go trying to address them:

- Add "chording" support, where you can click a numbered square using both
  mouse buttons simultaneously to sweep all non-flagged adjacent squares.

- Mis-flagged squares are now revealed as such on game over, with a special
  "bad flag" icon.

- The game timer now shows tenths of seconds. It also doesn't start until
  you click the first square.

- Add the three difficulty modes from the classic Windows version.
This commit is contained in:
Andreas Kling 2019-04-26 19:54:31 +02:00
parent e90b501b31
commit 146aedc32c
5 changed files with 213 additions and 40 deletions

View file

@ -2,12 +2,21 @@
#include <LibGUI/GFrame.h>
#include <LibCore/CTimer.h>
#include <AK/Noncopyable.h>
class SquareButton;
class Field;
class GButton;
class GLabel;
class SquareButton;
class SquareLabel;
struct Square {
class Square {
AK_MAKE_NONCOPYABLE(Square)
public:
Square() { }
~Square();
Field* field { nullptr };
bool is_swept { false };
bool has_mine { false };
bool has_flag { false };
@ -15,10 +24,14 @@ struct Square {
int column { 0 };
int number { 0 };
SquareButton* button { nullptr };
GLabel* label { nullptr };
SquareLabel* label { nullptr };
template<typename Callback> void for_each_neighbor(Callback);
};
class Field final : public GFrame {
friend class Square;
friend class SquareLabel;
public:
Field(GLabel& flag_label, GLabel& time_label, GButton& face_button, GWidget* parent);
virtual ~Field() override;
@ -28,19 +41,25 @@ public:
int mine_count() const { return m_mine_count; }
int square_size() const { return 15; }
void set_field_size(int rows, int columns, int mine_count);
void reset();
Function<void()> on_size_changed;
private:
virtual void paint_event(GPaintEvent&) override;
void on_square_clicked(Square&);
void on_square_right_clicked(Square&);
void on_square_chorded(Square&);
void game_over();
void win();
void reveal_mines();
void set_chord_preview(Square&, bool);
Square& square(int row, int column) { return m_squares[row * columns() + column]; }
const Square& square(int row, int column) const { return m_squares[row * columns() + column]; }
Square& square(int row, int column) { return *m_squares[row * columns() + column]; }
const Square& square(int row, int column) const { return *m_squares[row * columns() + column]; }
void flood_fill(Square&);
@ -53,15 +72,20 @@ private:
int m_columns { 9 };
int m_mine_count { 10 };
int m_unswept_empties { 0 };
Vector<Square> m_squares;
Vector<OwnPtr<Square>> m_squares;
RetainPtr<GraphicsBitmap> m_mine_bitmap;
RetainPtr<GraphicsBitmap> m_flag_bitmap;
RetainPtr<GraphicsBitmap> m_badflag_bitmap;
RetainPtr<GraphicsBitmap> m_default_face_bitmap;
RetainPtr<GraphicsBitmap> m_good_face_bitmap;
RetainPtr<GraphicsBitmap> m_bad_face_bitmap;
RetainPtr<GraphicsBitmap> m_number_bitmap[8];
GButton& m_face_button;
GLabel& m_flag_label;
GLabel& m_time_label;
CTimer m_timer;
int m_seconds_elapsed { 0 };
int m_time_elapsed { 0 };
int m_flags_left { 0 };
Face m_face { Face::Default };
bool m_chord_preview { false };
};