From e9e293981ee203b7e7254bd32496abeabe77a165 Mon Sep 17 00:00:00 2001 From: Silas Dohm <silas.dohm@stud.hs-bochum.de> Date: Tue, 23 Aug 2022 23:27:50 +0200 Subject: [PATCH] moved work --- cell.cpp | 3 --- cell.h | 3 +-- window.cpp | 34 +++++++++++++++++++++------------- window.h | 12 ++++-------- 4 files changed, 26 insertions(+), 26 deletions(-) diff --git a/cell.cpp b/cell.cpp index 9fa9082..9e7a3e4 100644 --- a/cell.cpp +++ b/cell.cpp @@ -57,7 +57,6 @@ void Cell::removeOption(int x) states[x]->setText(" "); states[x]->setDisabled(true); possibleStates--; - options.erase(std::find(options.begin(), options.end(), x)); } blocked[x]++; } @@ -65,13 +64,11 @@ void Cell::removeOption(int x) void Cell::addOption(int x) { blocked[x]--; - // if (!states[x]->isEnabled()) if (blocked[x] < 1) { states[x]->setText(QString::number(x + 1)); states[x]->setDisabled(false); possibleStates++; - options.push_back(x); } } void Cell::collapsedCellClicked(void) diff --git a/cell.h b/cell.h index 1d1c53a..a90e7b6 100644 --- a/cell.h +++ b/cell.h @@ -15,7 +15,7 @@ public: void resizeEvent(QResizeEvent *event); int possibleStates = 9; bool collapsed = false; - std::vector<int> options = {0, 1, 2, 3, 4, 5, 6, 7, 8}; + std::array<int, 9> blocked; private: QSignalMapper *mapper; @@ -23,7 +23,6 @@ private: QGridLayout *gridLayout; QPushButton *states[9]; QPushButton *number; - std::array<int, 9> blocked; QStackedWidget *stackedWidget; signals: diff --git a/window.cpp b/window.cpp index 2b7236e..44f3bec 100644 --- a/window.cpp +++ b/window.cpp @@ -9,7 +9,6 @@ #include <QSpacerItem> #include <QTimer> - Window::Window(QWidget *parent) : QWidget{parent} { @@ -54,7 +53,8 @@ Window::Window(QWidget *parent) { connect(&grid[x + y * 9], SIGNAL(update(int)), &grid[x + d * 9], SLOT(removeOption(int))); connect(&grid[x + y * 9], SIGNAL(undo(int)), &grid[x + d * 9], SLOT(addOption(int))); - if(&grid[x+y*9]!= &grid[d+y*9]){ + if (&grid[x + y * 9] != &grid[d + y * 9]) + { connect(&grid[x + y * 9], SIGNAL(update(int)), &grid[d + y * 9], SLOT(removeOption(int))); connect(&grid[x + y * 9], SIGNAL(undo(int)), &grid[d + y * 9], SLOT(addOption(int))); } @@ -88,19 +88,21 @@ void Window::solveButtonClicked() if (backtracking) { hist.back().cell->collapsedCellClicked(); - if (hist.back().options.size() == 0) + std::vector<int> b; + for (int i = 0; i < 9; i++) + if (hist.back().blocked[i] == 0) + b.push_back(i); + if (b.size() == 0) { hist.pop_back(); continue; } backtracking = false; - int choiceNr = rand() % hist.back().options.size(); // choose a random number; - repaint(); // draw cells again + int choiceNr = rand() % b.size(); // choose a random number; + repaint(); // draw cells again delay(delayTime); - hist.back().cell->collapse(hist.back().options[choiceNr]); // collapse cell with chosen number; - std::vector<int>::iterator it; - it = hist.back().options.begin() + choiceNr; - hist.back().options.erase(it); + hist.back().cell->collapse(b[choiceNr]); // collapse cell with chosen number; + hist.back().blocked[b[choiceNr]]++; } else { @@ -136,10 +138,16 @@ void Window::solveButtonClicked() continue; } - int choiceCell = rand() % b.size(); // choose a random cell - int choiceNr = rand() % b[choiceCell]->options.size(); // choose a random number; - hist.push_back(History(b[choiceCell], b[choiceCell]->options, choiceNr)); - b[choiceCell]->collapse(b[choiceCell]->options[choiceNr]); // collapse that random cell with chosen number; + int choiceCell = rand() % b.size(); // choose a random cell + std::vector<int> c; + for (int i = 0; i < 9; i++) + { + if (b[choiceCell]->blocked[i] == 0) + c.push_back(i); + } + int choiceNr = rand() % c.size(); // choose a random number; + hist.push_back(History(b[choiceCell], b[choiceCell]->blocked, c[choiceNr])); // fix this!!! + b[choiceCell]->collapse(c[choiceNr]); // collapse that random cell with chosen number; } } } diff --git a/window.h b/window.h index 08c7048..2a6b5db 100644 --- a/window.h +++ b/window.h @@ -28,16 +28,12 @@ private: struct History { Cell *cell; - std::vector<int> options; - History(Cell *c, std::vector<int> in, int choice) + std::array<int, 9> blocked; + History(Cell *c, std::array<int,9> in, int choice) { cell = c; - for (int i = 0; i < in.size(); i++) - { - if (i == choice) - continue; - options.push_back(in[i]); - } + blocked =in; + blocked[choice] ++; } }; inline void delay(int millisecondsWait); -- GitLab