refactoring

This commit is contained in:
Bernardo Magri
2025-03-07 07:23:59 +00:00
parent 2c13567747
commit 052690fede
6 changed files with 454 additions and 92 deletions

View File

@@ -12,31 +12,30 @@
void MainWindow::OnCellRightClick(int n_press, double n_x, double n_y, int index) {
(void)n_press, (void)n_x, (void)n_y;
int x = index % field.getCols();
int y = index / field.getRows();
int y = index / field.getCols();
int pos = x + y * field.getRows();
if(field.isCleared(x, y) == false) {
if(field.isOpened(x, y) == false) {
field.toggleFlag(x, y);
if(field.isFlagged(x, y)) {
auto imgflag = Gtk::make_managed<Gtk::Image>();
imgflag->set(m_pixbufFlag);
buttons.at(x + y * field.getRows())->set_child(*imgflag);
buttons.at(x + y * field.getRows())->set_active(true);
buttons.at(pos)->set_child(*imgflag);
buttons.at(pos)->set_active(true);
}
else {
buttons.at(x + y * field.getRows())->unset_child();
buttons.at(x+ y * field.getRows())->queue_draw();
buttons.at(x + y * field.getRows())->set_active(false);
buttons.at(pos)->unset_child();
buttons.at(pos)->queue_draw();
buttons.at(pos)->set_active(false);
}
}
// Glib::ustring msg = Glib::ustring::compose("Remaining flags: %1", field.getRemainingFlags());
// flagLabel.set_label(msg);
}
void MainWindow::updateFlagsLabel(int flags) {
Glib::ustring msg = Glib::ustring::compose("Remaining flags: %1", flags);
flagLabel.set_label(msg);
}
// void MainWindow::OnNewButtonClick() {
// newGame = true;
// gameOver = false;
@@ -47,7 +46,7 @@ void MainWindow::updateFlagsLabel(int flags) {
// button->set_label("");
// }
// field->remainingFlags = MINES;
// //field->remainingFlags = MINES;
// Glib::ustring msg = Glib::ustring::compose("Remaining flags: %1", field->remainingFlags);
// flagLabel.set_label(msg);
@@ -57,41 +56,6 @@ void MainWindow::updateFlagsLabel(int flags) {
// }
// void MainWindow::OpenNearCells(int index, std::set<int> &visited) {
// int cols = field->Cols();
// int x = index % cols;
// int y = index / cols;
// if (visited.count(index)) return;
// Cell* cell = field->GetCell(x, y);
// if (!cell || cell->bombsNearby > 0 || cell->type == CellType::Bomb) return;
// visited.insert(index);
// buttons[index]->set_active(true);
// for (int i = -1; i <= 1; i++) {
// for (int j = -1; j <= 1; j++) {
// if (i == 0 && j == 0) continue; // Skip the current cell
// int nx = x + i;
// int ny = y + j;
// int newIndex = ny * cols + nx;
// Cell* neighborCell = field->GetCell(nx, ny);
// // Bounds check before recursive call
// if (nx >= 0 && nx < cols && ny >= 0 && ny < cols) {
// if (visited.count(newIndex) == 0) {
// OpenNearCells(newIndex, visited);
// }
// if (neighborCell && !buttons[newIndex]->get_active() && !neighborCell->isFlag) {
// OpenNearCells(newIndex, visited);
// }
// }
// }
// }
// }
void MainWindow::OnCellClick(int x, int y) {
@@ -107,7 +71,7 @@ void MainWindow::OnCellClick(int x, int y) {
openBombs();
}
else {
field.clearCell(x, y);
field.openCell(x, y);
}
}
@@ -115,7 +79,7 @@ void MainWindow::OnCellClick(int x, int y) {
void MainWindow::openBombs() {
for(int i=0; i < field.getCols() * field.getRows(); i++) {
int x = i % field.getCols();
int y = i / field.getRows();
int y = i / field.getCols();
buttons.at(i)->set_sensitive(false);
@@ -136,38 +100,39 @@ void MainWindow::openBombs() {
}
void MainWindow::updateCell(int x, int y) {
if(field.isCleared(x, y)) {
int pos = x + y * field.getRows();
if(field.isOpened(x, y)) {
if (field.bombsNearby(x, y) > 0) {
switch(field.bombsNearby(x, y)) {
case 1:
buttons.at(x + y * field.getRows())->get_style_context()->add_class("label-1");
buttons.at(pos)->get_style_context()->add_class("label-1");
break;
case 2:
buttons.at(x + y * field.getRows())->get_style_context()->add_class("label-2");
buttons.at(pos)->get_style_context()->add_class("label-2");
break;
case 3:
buttons.at(x + y * field.getRows())->get_style_context()->add_class("label-3");
buttons.at(pos)->get_style_context()->add_class("label-3");
break;
case 4:
buttons.at(x + y * field.getRows())->get_style_context()->add_class("label-4");
buttons.at(pos)->get_style_context()->add_class("label-4");
break;
case 5:
buttons.at(x + y * field.getRows())->get_style_context()->add_class("label-5");
buttons.at(pos)->get_style_context()->add_class("label-5");
break;
case 6:
buttons.at(x + y * field.getRows())->get_style_context()->add_class("label-6");
buttons.at(pos)->get_style_context()->add_class("label-6");
break;
case 7:
buttons.at(x + y * field.getRows())->get_style_context()->add_class("label-7");
buttons.at(pos)->get_style_context()->add_class("label-7");
break;
case 8:
buttons.at(x + y * field.getRows())->get_style_context()->add_class("label-8");
buttons.at(pos)->get_style_context()->add_class("label-8");
break;
}
buttons.at(x + y * field.getRows())->set_label(Glib::ustring::format(field.bombsNearby(x, y)));
buttons.at(pos)->set_label(Glib::ustring::format(field.bombsNearby(x, y)));
}
buttons.at(x + y * field.getRows())->set_active(true);
buttons.at(x + y * field.getRows())->set_sensitive(false);
buttons.at(pos)->set_active(true);
buttons.at(pos)->set_sensitive(false);
}
}
// void MainWindow::ShowGameWonAnimation() {
@@ -299,9 +264,8 @@ MainWindow::MainWindow()
grid.attach(*button, x, y);
}
field.clearCellSignal.connect(sigc::bind(sigc::mem_fun(*this, &MainWindow::updateCell)));
field.remainingFlagsChangedSignal.connect(sigc::bind(sigc::mem_fun(*this, \
&MainWindow::updateFlagsLabel)));
field.openCellSignal.connect(sigc::bind(sigc::mem_fun(*this, &MainWindow::updateCell)));
field.remainingFlagsSignal.connect(sigc::bind(sigc::mem_fun(*this, &MainWindow::updateFlagsLabel)));
//newGameButton.set_label("New");
//newGameButton.add_css_class("suggested-action");
//newGameButton.signal_clicked().connect(sigc::mem_fun(*this, &MainWindow::OnNewButtonClick));