Update gtk140
This commit is contained in:
parent
c8f944a4e9
commit
9ec57a7497
|
@ -1,6 +1,7 @@
|
||||||
{
|
{
|
||||||
"C_Cpp.default.configurationProvider": "ms-vscode.cmake-tools",
|
"C_Cpp.default.configurationProvider": "ms-vscode.cmake-tools",
|
||||||
"files.associations": {
|
"files.associations": {
|
||||||
"iostream": "cpp"
|
"iostream": "cpp",
|
||||||
|
"ostream": "cpp"
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,9 +1,10 @@
|
||||||
#include "MineSweeper.hh"
|
#include "MineSweeper.hh"
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <fstream>
|
||||||
|
|
||||||
MineSweeper::MineSweeper()
|
MineSweeper::MineSweeper()
|
||||||
:main_box(Gtk::ORIENTATION_VERTICAL, 5),
|
: main_box(Gtk::ORIENTATION_VERTICAL, 5),
|
||||||
btn_box(Gtk::ORIENTATION_HORIZONTAL, 5)
|
btn_box(Gtk::ORIENTATION_HORIZONTAL, 5)
|
||||||
{
|
{
|
||||||
// Initalize Window
|
// Initalize Window
|
||||||
set_title("Gtkmm MineSweeper");
|
set_title("Gtkmm MineSweeper");
|
||||||
|
@ -24,6 +25,17 @@ btn_box(Gtk::ORIENTATION_HORIZONTAL, 5)
|
||||||
// Default setting
|
// Default setting
|
||||||
reset_game();
|
reset_game();
|
||||||
|
|
||||||
|
// Buttons
|
||||||
|
btnstart.set_label("Start/Reset");
|
||||||
|
btnshow.set_label("Show All");
|
||||||
|
btnexit.set_label("Exit");
|
||||||
|
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));
|
||||||
|
btnshow.signal_clicked().connect(sigc::mem_fun(*this, &MineSweeper::show_mines));
|
||||||
|
btnexit.signal_clicked().connect(sigc::mem_fun(*this, &MineSweeper::hide));
|
||||||
|
|
||||||
// Pack widgets
|
// Pack widgets
|
||||||
status_label.set_halign(Gtk::ALIGN_CENTER);
|
status_label.set_halign(Gtk::ALIGN_CENTER);
|
||||||
btn_box.set_halign(Gtk::ALIGN_CENTER);
|
btn_box.set_halign(Gtk::ALIGN_CENTER);
|
||||||
|
@ -45,21 +57,73 @@ void MineSweeper::reset_game()
|
||||||
for (int j = 0; j < 7; j++)
|
for (int j = 0; j < 7; j++)
|
||||||
{
|
{
|
||||||
cell[i * 7 + j].set_label("?");
|
cell[i * 7 + j].set_label("?");
|
||||||
|
cell[i * 7 + j].mines_around = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reset mines
|
// Reset mines
|
||||||
while(mine_count < 9)
|
while (mine_count < 9)
|
||||||
{
|
{
|
||||||
int index = g_random_int_range(0, 49);
|
int index = g_random_int_range(0, 49);
|
||||||
if(!(cell[index].has_mine)){
|
if (!(cell[index].has_mine))
|
||||||
|
{
|
||||||
cell[index].has_mine = true;
|
cell[index].has_mine = true;
|
||||||
cell[index].set_label("x");
|
// cell[index].set_label("x");
|
||||||
mine_count++;
|
mine_count++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
game_ended = false;
|
game_ended = false;
|
||||||
winned = true;
|
winned = true;
|
||||||
|
calc_mines();
|
||||||
|
}
|
||||||
|
|
||||||
|
void MineSweeper::calc_mines()
|
||||||
|
{
|
||||||
|
for (int i = 0; i < 7; i++)
|
||||||
|
{
|
||||||
|
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++)
|
||||||
|
{
|
||||||
|
for (index2 = MAX(0, j - 1); index2 < MIN(j + 1, 6) + 1; index2++)
|
||||||
|
{
|
||||||
|
if ((cell[index1 * 7 + index2].has_mine))
|
||||||
|
{
|
||||||
|
// std::fstream outfile;
|
||||||
|
// outfile.open("test.txt", std::ios_base::app);
|
||||||
|
// if (outfile.is_open())
|
||||||
|
// {
|
||||||
|
// outfile << index1 << " " << index2 << "Has mine!" << std::endl;
|
||||||
|
// }
|
||||||
|
// outfile.close();
|
||||||
|
cell[i * 7 + j].mines_around++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// if (!cell[i * 7 + j].has_mine)
|
||||||
|
// {
|
||||||
|
//
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void MineSweeper::show_mines(){
|
||||||
|
for(int i = 0; i < 49; i++){
|
||||||
|
if(cell[i].has_mine){
|
||||||
|
cell[i].set_label("x");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void MineSweeper::cell_clicked(MineCell *cell)
|
void MineSweeper::cell_clicked(MineCell *cell)
|
||||||
|
@ -70,17 +134,29 @@ void MineSweeper::cell_clicked(MineCell *cell)
|
||||||
if (cell->has_mine)
|
if (cell->has_mine)
|
||||||
{
|
{
|
||||||
winned = false;
|
winned = false;
|
||||||
|
show_mines();
|
||||||
|
status_label.set_label("You lost!");
|
||||||
game_ended = true;
|
game_ended = true;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
mines_clear++;
|
mines_clear++;
|
||||||
cell->set_label(" ");
|
if (cell->mines_around == 0)
|
||||||
|
{
|
||||||
|
cell->set_label(" ");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
char *label = g_strdup_printf("%d", cell->mines_around);
|
||||||
|
cell->set_label(label);
|
||||||
|
g_free(label);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// If all the mines has cleared, you has winned
|
// If all the mines has cleared, you has winned
|
||||||
if (mines_clear == 40)
|
if (mines_clear == 40)
|
||||||
{
|
{
|
||||||
|
status_label.set_label("You winned!");
|
||||||
winned = true;
|
winned = true;
|
||||||
game_ended = true;
|
game_ended = true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,6 +10,7 @@ public:
|
||||||
MineCell(){
|
MineCell(){
|
||||||
// Set button style
|
// Set button style
|
||||||
set_relief(Gtk::RELIEF_NONE);
|
set_relief(Gtk::RELIEF_NONE);
|
||||||
|
mines_around = 0;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -23,6 +24,7 @@ private:
|
||||||
Gtk::Grid mine_grid;
|
Gtk::Grid mine_grid;
|
||||||
Gtk::Label status_label;
|
Gtk::Label status_label;
|
||||||
Gtk::Box main_box, btn_box;
|
Gtk::Box main_box, btn_box;
|
||||||
|
Gtk::Button btnstart, btnshow, btnexit;
|
||||||
MineCell cell[49];
|
MineCell cell[49];
|
||||||
bool winned, game_ended;
|
bool winned, game_ended;
|
||||||
int mines_clear, mine_count;
|
int mines_clear, mine_count;
|
||||||
|
@ -32,5 +34,7 @@ private:
|
||||||
|
|
||||||
// Signal Handlers
|
// Signal Handlers
|
||||||
void reset_game();
|
void reset_game();
|
||||||
|
void calc_mines();
|
||||||
|
void show_mines();
|
||||||
void cell_clicked(MineCell *cell);
|
void cell_clicked(MineCell *cell);
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue