From aeaa8a71d8184d2603d6041a8e3fe018bfbe15e8 Mon Sep 17 00:00:00 2001 From: daleclack Date: Mon, 24 Oct 2022 22:09:17 +0800 Subject: [PATCH] Update gtk141 --- .../res/cambalache/.gitignore | 1 + Gtkmm3/gtk141_minesweeper2/src/MineSweeper.cc | 22 ++++++++++----- Gtkmm3/gtk141_minesweeper2/src/MineSweeper.hh | 28 ++++++++++--------- 3 files changed, 31 insertions(+), 20 deletions(-) create mode 100644 Gtkmm3/gtk141_minesweeper2/res/cambalache/.gitignore diff --git a/Gtkmm3/gtk141_minesweeper2/res/cambalache/.gitignore b/Gtkmm3/gtk141_minesweeper2/res/cambalache/.gitignore new file mode 100644 index 0000000..25284c2 --- /dev/null +++ b/Gtkmm3/gtk141_minesweeper2/res/cambalache/.gitignore @@ -0,0 +1 @@ +*.ui \ No newline at end of file diff --git a/Gtkmm3/gtk141_minesweeper2/src/MineSweeper.cc b/Gtkmm3/gtk141_minesweeper2/src/MineSweeper.cc index c8bb587..71de642 100644 --- a/Gtkmm3/gtk141_minesweeper2/src/MineSweeper.cc +++ b/Gtkmm3/gtk141_minesweeper2/src/MineSweeper.cc @@ -8,7 +8,7 @@ MineSweeper::MineSweeper() cell(nullptr) { // Initalize Window - header.set_title("Gtkmm MineSweeper"); + header.set_title("MineSweeper"); set_titlebar(header); header.set_show_close_button(); header.set_decoration_layout("close,minimize,maximize:menu"); @@ -23,7 +23,7 @@ MineSweeper::MineSweeper() menu_btn.set_popover(popover1); // Add Actions - add_action("new_game", sigc::mem_fun(*this, &MineSweeper::reset_game)); + add_action("new_game", sigc::mem_fun(*this, &MineSweeper::new_game)); add_action("scores", sigc::mem_fun(*this, &MineSweeper::show_scores)); add_action("show_mines", sigc::mem_fun(*this, &MineSweeper::show_mines)); add_action("quit", sigc::mem_fun(*this, &MineSweeper::hide)); @@ -38,7 +38,7 @@ MineSweeper::MineSweeper() btn_box.pack_start(btnstart, Gtk::PACK_SHRINK); btn_box.pack_start(btnshow, Gtk::PACK_SHRINK); btn_box.pack_start(btnexit, Gtk::PACK_SHRINK); - btnstart.signal_clicked().connect(sigc::mem_fun(*this, &MineSweeper::reset_game)); + btnstart.signal_clicked().connect(sigc::mem_fun(*this, &MineSweeper::new_game)); btnshow.signal_clicked().connect(sigc::mem_fun(*this, &MineSweeper::show_mines)); btnexit.signal_clicked().connect(sigc::mem_fun(*this, &MineSweeper::hide)); @@ -67,17 +67,25 @@ MineSweeper::MineSweeper() } MineSweeper::~MineSweeper(){ + // Delete all resources delete input_dialog; + delete scores_win; if(cell != nullptr){ delete[] cell; } } -void MineSweeper::reset_game() +void MineSweeper::new_game(){ + reset_game(); +} + +void MineSweeper::reset_game(int width, int height, int mines) { + // Clear the cells if(cell != nullptr){ delete[] cell; } + cell = new MineCell[49]; // Reset timer mytimer.disconnect(); @@ -88,9 +96,9 @@ void MineSweeper::reset_game() mines_clear = 0; mine_grid.set_sensitive(); // Reset all data - for (int i = 0; i < 7; i++) + for (int i = 0; i < width; i++) { - for (int j = 0; j < 7; j++) + for (int j = 0; j < height; j++) { // Initalize cell cell[i * 7 + j].set_relief(Gtk::RELIEF_HALF); @@ -104,7 +112,7 @@ void MineSweeper::reset_game() } // Reset mines - while (mine_count < 9) + while (mine_count < mines) { int index = g_random_int_range(0, 49); if (!(cell[index].has_mine)) diff --git a/Gtkmm3/gtk141_minesweeper2/src/MineSweeper.hh b/Gtkmm3/gtk141_minesweeper2/src/MineSweeper.hh index b67c9e7..577ddd0 100644 --- a/Gtkmm3/gtk141_minesweeper2/src/MineSweeper.hh +++ b/Gtkmm3/gtk141_minesweeper2/src/MineSweeper.hh @@ -7,11 +7,12 @@ class MineCell : public Gtk::Button { public: - bool has_mine = false; // Whether the grid has mine - bool cleared = false; // Whether the mine is cleared - int mines_around; // The number near the grid - int x, y; // The Position of the grid - MineCell(){ + bool has_mine = false; // Whether the grid has mine + bool cleared = false; // Whether the mine is cleared + int mines_around; // The number near the grid + int x, y; // The Position of the grid + MineCell() + { // Set button style set_relief(Gtk::RELIEF_NONE); mines_around = 0; @@ -53,12 +54,13 @@ private: ScoresWin *scores_win; // Signal Handlers - void reset_game(); - void calc_mines(); - void show_mines(); - void show_scores(); - void game_lost(int explode_index); - void cell_clicked(MineCell *cell1); - bool timer_func(); - void check_mines(int pos_x, int pos_y); + void new_game(); // "New Game" handler + void reset_game(int width = 7, int height = 7, int mines = 9); // Reset all mines + void calc_mines(); // Get the mines around + void show_mines(); // Show all mines + void show_scores(); // Show all scores + void game_lost(int explode_index); // You lost the game + void cell_clicked(MineCell *cell1); // Open a cell + bool timer_func(); // Timer + void check_mines(int pos_x, int pos_y); // Check if there is a mine };