Update gtk141

This commit is contained in:
daleclack 2022-10-15 10:34:12 +08:00
parent bd8eb3d8b5
commit 67fd417be5
4 changed files with 194 additions and 50 deletions

View File

@ -0,0 +1,88 @@
{
"files.associations": {
"cctype": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"array": "cpp",
"atomic": "cpp",
"bit": "cpp",
"*.tcc": "cpp",
"bitset": "cpp",
"chrono": "cpp",
"codecvt": "cpp",
"compare": "cpp",
"concepts": "cpp",
"condition_variable": "cpp",
"cstdint": "cpp",
"deque": "cpp",
"list": "cpp",
"map": "cpp",
"set": "cpp",
"string": "cpp",
"unordered_map": "cpp",
"unordered_set": "cpp",
"vector": "cpp",
"exception": "cpp",
"algorithm": "cpp",
"functional": "cpp",
"iterator": "cpp",
"memory": "cpp",
"memory_resource": "cpp",
"numeric": "cpp",
"optional": "cpp",
"random": "cpp",
"ratio": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"utility": "cpp",
"fstream": "cpp",
"initializer_list": "cpp",
"iomanip": "cpp",
"iosfwd": "cpp",
"iostream": "cpp",
"istream": "cpp",
"limits": "cpp",
"mutex": "cpp",
"new": "cpp",
"numbers": "cpp",
"ostream": "cpp",
"semaphore": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"stop_token": "cpp",
"streambuf": "cpp",
"thread": "cpp",
"cinttypes": "cpp",
"typeinfo": "cpp",
"valarray": "cpp",
"variant": "cpp",
"__bit_reference": "cpp",
"__bits": "cpp",
"__config": "cpp",
"__debug": "cpp",
"__errc": "cpp",
"__hash_table": "cpp",
"__locale": "cpp",
"__mutex_base": "cpp",
"__node_handle": "cpp",
"__split_buffer": "cpp",
"__threading_support": "cpp",
"__tree": "cpp",
"__tuple": "cpp",
"__verbose_abort": "cpp",
"ios": "cpp",
"locale": "cpp",
"queue": "cpp",
"stack": "cpp"
}
}

View File

@ -65,7 +65,7 @@ if(WIN32)
set_property(SOURCE ../icon.rc APPEND PROPERTY
OBJECT_DEPENDS ${PROJECT_SOURCE_DIR}/../icon.ico
)
add_executable(${PROJECT_NAME} WIN32 ${app_WINRC} ${SOURCE_FILE} ${RESOURCE_FILE})
add_executable(${PROJECT_NAME} ${app_WINRC} ${SOURCE_FILE} ${RESOURCE_FILE})
add_custom_command( TARGET ${PROJECT_NAME}
COMMAND echo * > ${CMAKE_BINARY_DIR}/.gitignore
COMMAND echo **/* > ${CMAKE_BINARY_DIR}/.hgignore)

View File

@ -1,5 +1,6 @@
#include "MineSweeper.hh"
#include <string>
#include <iostream>
MineSweeper::MineSweeper()
: main_box(Gtk::ORIENTATION_VERTICAL, 5),
@ -18,6 +19,10 @@ MineSweeper::MineSweeper()
cell[i * 7 + j].signal_clicked().connect(sigc::bind(
sigc::mem_fun(*this, &MineSweeper::cell_clicked), &cell[i * 7 + j]));
mine_grid.attach(cell[i * 7 + j], j, i);
cell[i * 7 + j].set_relief(Gtk::RELIEF_HALF);
cell[i * 7 + j].x = j;
cell[i * 7 + j].y = i;
cell[i * 7 + j].cleared = false;
}
}
@ -38,6 +43,7 @@ MineSweeper::MineSweeper()
// Pack widgets
status_label.set_halign(Gtk::ALIGN_CENTER);
btn_box.set_halign(Gtk::ALIGN_CENTER);
mine_grid.set_halign(Gtk::ALIGN_CENTER);
main_box.pack_start(status_label, Gtk::PACK_SHRINK);
main_box.pack_start(mine_grid);
main_box.pack_start(btn_box, Gtk::PACK_SHRINK);
@ -55,12 +61,14 @@ void MineSweeper::reset_game()
mytimer = Glib::signal_timeout().connect(sigc::mem_fun(*this, &MineSweeper::timer_func), 1000);
mine_count = 0;
mines_clear = 0;
// Reset all data
for (int i = 0; i < 7; i++)
{
for (int j = 0; j < 7; j++)
{
//cell[i * 7 + j].set_label("?");
// cell[i * 7 + j].set_label("?");
cell[i * 7 + j].set_relief(Gtk::RELIEF_HALF);
cell[i * 7 + j].set_image_from_icon_name("");
cell[i * 7 + j].set_always_show_image();
cell[i * 7 + j].mines_around = 0;
@ -93,15 +101,6 @@ void MineSweeper::calc_mines()
{
for (int j = 0; j < 7; j++)
{
// std::fstream outfile;
// outfile.open("test.txt", std::ios_base::app);
// if (outfile.is_open())
// {
// outfile << "Start:" << i << " " << j << std::endl;
// outfile << "index1:" << MAX(0, i - 1) << " " << MIN(i + 1, 6) << std::endl;
// outfile << "index2:" << MAX(0, j - 1) << " " << MIN(j + 1, 6) << std::endl;
// }
// outfile.close();
int index1, index2;
for (index1 = MAX(0, i - 1); index1 < MIN(i + 1, 6) + 1; index1++)
{
@ -120,6 +119,16 @@ void MineSweeper::calc_mines()
}
}
}
// std::fstream outfile;
// outfile.open("test.txt", std::ios_base::app);
// if (outfile.is_open())
// {
// outfile << "Start:" << j << " " << i << std::endl;
// outfile << "index1:" << MAX(0, i - 1) << " " << MIN(i + 1, 6) << std::endl;
// outfile << "index2:" << MAX(0, j - 1) << " " << MIN(j + 1, 6) << std::endl;
// outfile << "Mines" << cell[i * 7 + j].mines_around << std::endl;
// }
// outfile.close();
// if (!cell[i * 7 + j].has_mine)
// {
//
@ -128,15 +137,29 @@ void MineSweeper::calc_mines()
}
}
void MineSweeper::show_mines(){
for(int i = 0; i < 49; i++){
if(cell[i].has_mine){
void MineSweeper::show_mines()
{
for (int i = 0; i < 49; i++)
{
if (cell[i].has_mine)
{
cell[i].set_image_from_icon_name("mine");
}
}
}
bool MineSweeper::timer_func(){
void MineSweeper::game_lost(int explode_index){
for (int i = 0; i < 49; i++)
{
if (cell[i].has_mine && i != explode_index)
{
cell[i].set_image_from_icon_name("mine");
}
}
}
bool MineSweeper::timer_func()
{
// Set timer
char tmp[50];
timer_count++;
@ -145,49 +168,79 @@ bool MineSweeper::timer_func(){
return true;
}
void MineSweeper::cell_clicked(MineCell *cell)
void MineSweeper::cell_clicked(MineCell *cell1)
{
if (!game_ended && !cell->cleared)
cell1->set_relief(Gtk::RELIEF_NONE);
if (!game_ended && !cell1->cleared)
{
cell->cleared = true;
//
// If get mine, the game will end now
if (cell->has_mine)
if (cell1->has_mine)
{
// Set game to stop
winned = false;
show_mines();
cell1->cleared = true;
cell1->set_image_from_icon_name("exploded");
game_lost(cell1->y * 7 + cell1->x);
status_label.set_label("You lost!");
game_ended = true;
mytimer.disconnect();
}
else
{
mines_clear++;
// std::cout << mines_clear << std::endl;
if (cell->mines_around == 0)
{
// cell->set_label(" ");
cell->set_image_from_icon_name("");
}
else
{
char *label = g_strdup_printf("%dmines", cell->mines_around);
cell->set_image_from_icon_name(label);
g_free(label);
}
}
// If all the mines has cleared, you has winned
if (mines_clear == 40)
{
// Stop the game
status_label.set_label("You winned!");
winned = true;
game_ended = true;
mytimer.disconnect();
// Save the time of game
check_mines(cell1->x, cell1->y);
}
}
}
void MineSweeper::check_mines(int pos_x, int pos_y)
{
if (pos_x >= 0 && pos_x <= 6 &&
pos_y >= 0 && pos_y <= 6)
{
if (!cell[pos_y * 7 + pos_x].has_mine &&
!cell[pos_y * 7 + pos_x].cleared)
{
mines_clear++;
// std::cout << mines_clear << std::endl;
// std::cout << pos_y << " " << pos_x << std::endl;
if (cell[pos_y * 7 + pos_x].mines_around == 0)
{
// cell->set_label(" ");
cell[pos_y * 7 + pos_x].set_image_from_icon_name("");
}
else
{
char *label = g_strdup_printf("%dmines", cell[pos_y * 7 + pos_x].mines_around);
// status_label.set_label(label);
cell[pos_y * 7 + pos_x].set_image_from_icon_name(label);
g_free(label);
}
cell[pos_y * 7 + pos_x].set_relief(Gtk::RELIEF_NONE);
cell[pos_y * 7 + pos_x].cleared = true;
if (cell[pos_y * 7 + pos_x].mines_around == 0)
{
check_mines(pos_y - 1, pos_x - 1);
check_mines(pos_y + 1, pos_x + 1);
check_mines(pos_y - 1, pos_x + 1);
check_mines(pos_y + 1, pos_x - 1);
check_mines(pos_y, pos_x - 1);
check_mines(pos_y, pos_x + 1);
check_mines(pos_y + 1, pos_x);
check_mines(pos_y - 1, pos_x);
}
}
}
// If all the mines has cleared, you has winned
if (mines_clear == 40)
{
// Stop the game
status_label.set_label("You winned!");
winned = true;
game_ended = true;
mytimer.disconnect();
// Save the time of game
}
}

View File

@ -5,9 +5,10 @@
class MineCell : public Gtk::Button
{
public:
bool has_mine = false;
bool cleared = false;
int mines_around;
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);
@ -38,6 +39,8 @@ private:
void reset_game();
void calc_mines();
void show_mines();
void cell_clicked(MineCell *cell);
void game_lost(int explode_index);
void cell_clicked(MineCell *cell1);
bool timer_func();
void check_mines(int pos_x, int pos_y);
};