浏览代码

Minesweeper: Start working on a simple minesweeper game. :^)

Andreas Kling 6 年之前
父节点
当前提交
a90e218c71

二进制
Base/res/icons/minesweeper/1.png


二进制
Base/res/icons/minesweeper/2.png


二进制
Base/res/icons/minesweeper/3.png


二进制
Base/res/icons/minesweeper/4.png


二进制
Base/res/icons/minesweeper/face-bad.png


二进制
Base/res/icons/minesweeper/face-default.png


二进制
Base/res/icons/minesweeper/face-good.png


二进制
Base/res/icons/minesweeper/flag.png


二进制
Base/res/icons/minesweeper/mine.png


+ 3 - 0
Games/Minesweeper/.gitignore

@@ -0,0 +1,3 @@
+*.o
+*.d
+Minesweeper

+ 176 - 0
Games/Minesweeper/Field.cpp

@@ -0,0 +1,176 @@
+#include "Field.h"
+#include <LibGUI/GButton.h>
+#include <LibGUI/GLabel.h>
+#include <AK/HashTable.h>
+#include <unistd.h>
+#include <time.h>
+
+class SquareButton final : public GButton {
+public:
+    SquareButton(GWidget* parent)
+        : GButton(parent)
+    {
+    }
+
+    Function<void()> on_right_click;
+
+    virtual void mousedown_event(GMouseEvent& event) override
+    {
+        if (event.button() == GMouseButton::Right) {
+            if (on_right_click)
+                on_right_click();
+        }
+        GButton::mousedown_event(event);
+    }
+};
+
+Field::Field(GWidget* parent)
+    : GWidget(parent)
+{
+    m_mine_bitmap = GraphicsBitmap::load_from_file("/res/icons/minesweeper/mine.png");
+    m_flag_bitmap = GraphicsBitmap::load_from_file("/res/icons/minesweeper/flag.png");
+    m_one_bitmap = GraphicsBitmap::load_from_file("/res/icons/minesweeper/1.png");
+    m_two_bitmap = GraphicsBitmap::load_from_file("/res/icons/minesweeper/2.png");
+    m_three_bitmap = GraphicsBitmap::load_from_file("/res/icons/minesweeper/3.png");
+    m_four_bitmap = GraphicsBitmap::load_from_file("/res/icons/minesweeper/4.png");
+
+    set_fill_with_background_color(true);
+    set_background_color(Color::LightGray);
+    reset();
+}
+
+Field::~Field()
+{
+}
+
+template<typename Callback>
+void Field::for_each_neighbor_of(const Square& square, Callback callback)
+{
+    int r = square.row;
+    int c = square.column;
+    if (r > 0) // Up
+        callback(this->square(r - 1, c));
+    if (c > 0) // Left
+        callback(this->square(r, c - 1));
+    if (r < (m_rows - 2)) // Down
+        callback(this->square(r + 1, c));
+    if (c < (m_columns - 2)) // Right
+        callback(this->square(r, c + 1));
+}
+
+void Field::reset()
+{
+    set_greedy_for_hits(false);
+    srand(time(nullptr));
+    m_squares.resize(rows() * columns());
+
+    HashTable<int> mines;
+    for (int i = 0; i < m_mine_count; ++i)
+        mines.set(rand() % (rows() * columns()));
+
+    int i = 0;
+    for (int r = 0; r < rows(); ++r) {
+        for (int c = 0; c < columns(); ++c) {
+            Rect rect = { c * square_size(), r * square_size(), square_size(), square_size() };
+            auto& square = this->square(r, c);
+            square.row = r;
+            square.column = c;
+            square.has_mine = mines.contains(i);
+            square.has_flag = false;
+            square.is_swept = false;
+            if (!square.label)
+                square.label = new GLabel(this);
+            square.label->set_relative_rect(rect);
+            square.label->set_visible(false);
+            square.label->set_icon(square.has_mine ? m_mine_bitmap : nullptr);
+            square.label->set_background_color(Color::from_rgb(0xff4040));
+            if (!square.button)
+                square.button = new SquareButton(this);
+            square.button->set_relative_rect(rect);
+            square.button->set_visible(true);
+            square.button->on_click = [this, &square] (GButton&) {
+                on_square_clicked(square);
+            };
+            square.button->on_right_click = [this, &square] {
+                on_square_right_clicked(square);
+            };
+            ++i;
+        }
+    }
+    for (int r = 0; r < rows(); ++r) {
+        for (int c = 0; c < columns(); ++c) {
+            auto& square = this->square(r, c);
+            int number = 0;
+            for_each_neighbor_of(square, [&number] (auto& neighbor) {
+                number += neighbor.has_mine;
+            });
+            square.number = number;
+            if (square.has_mine)
+                continue;
+            switch (number) {
+            case 1:
+                square.label->set_icon(m_one_bitmap.copy_ref());
+                break;
+            case 2:
+                square.label->set_icon(m_two_bitmap.copy_ref());
+                break;
+            case 3:
+                square.label->set_icon(m_three_bitmap.copy_ref());
+                break;
+            case 4:
+                square.label->set_icon(m_four_bitmap.copy_ref());
+                break;
+            }
+        }
+    }
+}
+
+void Field::flood_fill(Square& square)
+{
+    on_square_clicked(square);
+    for_each_neighbor_of(square, [this] (auto& neighbor) {
+        if (!neighbor.is_swept && !neighbor.has_mine && neighbor.number == 0)
+            flood_fill(neighbor);
+    });
+}
+
+void Field::on_square_clicked(Square& square)
+{
+    if (square.is_swept)
+        return;
+    if (square.has_flag)
+        return;
+    square.is_swept = true;
+    square.button->set_visible(false);
+    square.label->set_visible(true);
+    if (square.has_mine) {
+        square.label->set_fill_with_background_color(true);
+        game_over();
+    } else if (square.number == 0) {
+        flood_fill(square);
+    }
+}
+
+void Field::on_square_right_clicked(Square& square)
+{
+    if (square.is_swept)
+        return;
+    square.has_flag = !square.has_flag;
+    square.button->set_icon(square.has_flag ? m_flag_bitmap : nullptr);
+    square.button->update();
+}
+
+void Field::game_over()
+{
+    set_greedy_for_hits(true);
+    for (int r = 0; r < rows(); ++r) {
+        for (int c = 0; c < columns(); ++c) {
+            auto& square = this->square(r, c);
+            if (square.has_mine) {
+                square.button->set_visible(false);
+                square.label->set_visible(true);
+            }
+        }
+    }
+    update();
+}

+ 53 - 0
Games/Minesweeper/Field.h

@@ -0,0 +1,53 @@
+#pragma once
+
+#include <LibGUI/GWidget.h>
+
+class SquareButton;
+class GLabel;
+
+struct Square {
+    bool is_swept { false };
+    bool has_mine { false };
+    bool has_flag { false };
+    int row { 0 };
+    int column { 0 };
+    int number { 0 };
+    SquareButton* button { nullptr };
+    GLabel* label { nullptr };
+};
+
+class Field final : public GWidget {
+public:
+    explicit Field(GWidget* parent);
+    virtual ~Field() override;
+
+    int rows() const { return m_rows; }
+    int columns() const { return m_columns; }
+    int mine_count() const { return m_mine_count; }
+    int square_size() const { return 15; }
+
+    void reset();
+
+private:
+    void on_square_clicked(Square&);
+    void on_square_right_clicked(Square&);
+    void game_over();
+
+    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&);
+
+    template<typename Callback> void for_each_neighbor_of(const Square&, Callback);
+
+    int m_rows { 10 };
+    int m_columns { 10 };
+    int m_mine_count { 10 };
+    Vector<Square> m_squares;
+    RetainPtr<GraphicsBitmap> m_mine_bitmap;
+    RetainPtr<GraphicsBitmap> m_flag_bitmap;
+    RetainPtr<GraphicsBitmap> m_one_bitmap;
+    RetainPtr<GraphicsBitmap> m_two_bitmap;
+    RetainPtr<GraphicsBitmap> m_three_bitmap;
+    RetainPtr<GraphicsBitmap> m_four_bitmap;
+};

+ 32 - 0
Games/Minesweeper/Makefile

@@ -0,0 +1,32 @@
+OBJS = \
+    Field.o \
+    main.o
+
+APP = Minesweeper
+
+STANDARD_FLAGS = -std=c++17 -Wno-sized-deallocation
+WARNING_FLAGS = -Wextra -Wall -Wundef -Wcast-qual -Wwrite-strings -Wimplicit-fallthrough
+FLAVOR_FLAGS = -fno-exceptions -fno-rtti
+OPTIMIZATION_FLAGS = -Os
+INCLUDE_FLAGS = -I../.. -I. -I../../LibC
+
+DEFINES = -DSERENITY -DSANITIZE_PTRS -DUSERLAND
+
+CXXFLAGS = -MMD -MP $(WARNING_FLAGS) $(OPTIMIZATION_FLAGS) $(FLAVOR_FLAGS) $(STANDARD_FLAGS) $(INCLUDE_FLAGS) $(DEFINES)
+CXX = i686-pc-serenity-g++
+LD = i686-pc-serenity-g++
+LDFLAGS = -L../../LibC -L../../LibCore -L../../LibGUI
+
+all: $(APP)
+
+$(APP): $(OBJS)
+	$(LD) -o $(APP) $(LDFLAGS) $(OBJS) -lgui -lcore -lc
+
+.cpp.o:
+	@echo "CXX $<"; $(CXX) $(CXXFLAGS) -o $@ -c $<
+
+-include $(OBJS:%.o=%.d)
+
+clean:
+	@echo "CLEAN"; rm -f $(APPS) $(OBJS) *.d
+

+ 18 - 0
Games/Minesweeper/main.cpp

@@ -0,0 +1,18 @@
+#include "Field.h"
+#include <LibGUI/GApplication.h>
+#include <LibGUI/GWindow.h>
+
+int main(int argc, char** argv)
+{
+    GApplication app(argc, argv);
+
+    auto* window = new GWindow;
+    window->set_title("Minesweeper");
+    window->set_rect(100, 100, 200, 300);
+    auto* field = new Field(nullptr);
+    window->set_main_widget(field);
+
+    window->show();
+
+    return app.exec();
+}

+ 2 - 0
Kernel/makeall.sh

@@ -42,6 +42,8 @@ $make_cmd -C ../Applications/Downloader clean && \
 $make_cmd -C ../Applications/Downloader && \
 $make_cmd -C ../Applications/Downloader && \
 $make_cmd -C ../Applications/VisualBuilder clean && \
 $make_cmd -C ../Applications/VisualBuilder clean && \
 $make_cmd -C ../Applications/VisualBuilder && \
 $make_cmd -C ../Applications/VisualBuilder && \
+$make_cmd -C ../Applications/Games/Minesweeper clean && \
+$make_cmd -C ../Applications/Games/Minesweeper && \
 $make_cmd clean &&\
 $make_cmd clean &&\
 $make_cmd && \
 $make_cmd && \
 sudo ./sync.sh
 sudo ./sync.sh

+ 2 - 0
Kernel/sync.sh

@@ -99,6 +99,8 @@ cp -v ../Applications/Downloader/Downloader mnt/bin/Downloader
 ln -s Downloader mnt/bin/dl
 ln -s Downloader mnt/bin/dl
 cp -v ../Applications/VisualBuilder/VisualBuilder mnt/bin/VisualBuilder
 cp -v ../Applications/VisualBuilder/VisualBuilder mnt/bin/VisualBuilder
 ln -s VisualBuilder mnt/bin/vb
 ln -s VisualBuilder mnt/bin/vb
+cp -v ../Games/Minesweeper/Minesweeper mnt/bin/Minesweeper
+ln -s Minesweeper mnt/bin/ms
 cp -v kernel.map mnt/
 cp -v kernel.map mnt/
 sh sync-local.sh
 sh sync-local.sh
 umount mnt || ( sleep 0.5 && sync && umount mnt )
 umount mnt || ( sleep 0.5 && sync && umount mnt )

+ 2 - 1
LibGUI/GButton.h

@@ -41,7 +41,7 @@ public:
 
 
     virtual const char* class_name() const override { return "GButton"; }
     virtual const char* class_name() const override { return "GButton"; }
 
 
-private:
+protected:
     virtual void paint_event(GPaintEvent&) override;
     virtual void paint_event(GPaintEvent&) override;
     virtual void mousedown_event(GMouseEvent&) override;
     virtual void mousedown_event(GMouseEvent&) override;
     virtual void mouseup_event(GMouseEvent&) override;
     virtual void mouseup_event(GMouseEvent&) override;
@@ -49,6 +49,7 @@ private:
     virtual void enter_event(CEvent&) override;
     virtual void enter_event(CEvent&) override;
     virtual void leave_event(CEvent&) override;
     virtual void leave_event(CEvent&) override;
 
 
+private:
     String m_caption;
     String m_caption;
     RetainPtr<GraphicsBitmap> m_icon;
     RetainPtr<GraphicsBitmap> m_icon;
     ButtonStyle m_button_style { ButtonStyle::Normal };
     ButtonStyle m_button_style { ButtonStyle::Normal };