diff --git a/window.cpp b/window.cpp index 323de8740e41df9b99dc77a058b7162806be8a08..37fe239a88016d62c20e4d0c717732f9a096da5c 100644 --- a/window.cpp +++ b/window.cpp @@ -83,8 +83,7 @@ void Window::solveButtonClicked() srand(time(NULL)); // random seed while (1) { - repaint(); // draw cells again - delay(delayTime); + delay(); if (backtracking) { hist.back().cell->collapsedCellClicked(); @@ -94,64 +93,65 @@ void Window::solveButtonClicked() validNrs.push_back(i); if (validNrs.size() == 0) { + if (hist.size() <= 0) + return; hist.pop_back(); continue; } backtracking = false; - int rndNr = rand() % validNrs.size(); // choose a random number; - repaint(); // draw cells again - delay(delayTime); + int rndNr = rand() % validNrs.size(); // choose a random number; + delay(); hist.back().cell->collapse(validNrs[rndNr]); // collapse cell with chosen number; hist.back().blocked[validNrs[rndNr]]++; + delay(); } - else + std::vector<Cell *> b; // vector of cells with least entropy + int minEtropy = 10; + for (auto &cell : grid) { - std::vector<Cell *> b; // vector of cells with least entropy - int minEtropy = 10; - for (auto &cell : grid) + if (cell.collapsed) + continue; + else if (cell.possibleStates < minEtropy) { - if (cell.collapsed) - continue; - else if (cell.possibleStates < minEtropy) - { - minEtropy = cell.possibleStates; - b.clear(); - b.push_back(&cell); - } - else if (cell.possibleStates == minEtropy) - { - b.push_back(&cell); - } + minEtropy = cell.possibleStates; + b.clear(); + b.push_back(&cell); } - if (b.size() == 0) // if b is empty -> solved - return; - - else if (minEtropy == 0) + else if (cell.possibleStates == minEtropy) { - // qInfo("backtracking"); - backtracking = true; - continue; + b.push_back(&cell); } + } + if (b.size() == 0) // if b is empty -> solved + return; - int rndCell = rand() % b.size(); // choose a random cell - std::vector<int> validNrs; - for (int i = 0; i < 9; i++) - { - if (!b[rndCell]->blocked[i]) - validNrs.push_back(i); - } - int rndNr = rand() % validNrs.size(); // choose a random number; - hist.push_back(History(b[rndCell], b[rndCell]->blocked, validNrs[rndNr])); - b[rndCell]->collapse(validNrs[rndNr]); // collapse that random cell with chosen number; + else if (minEtropy == 0) + { + backtracking = true; + continue; + } + + int rndCell = rand() % b.size(); // choose a random cell + std::vector<int> validNrs; + for (int i = 0; i < 9; i++) + { + if (!b[rndCell]->blocked[i]) + validNrs.push_back(i); } + int rndNr = rand() % validNrs.size(); // choose a random number; + hist.push_back(History(b[rndCell], validNrs[rndNr])); + b[rndCell]->collapse(validNrs[rndNr]); // collapse that random cell with chosen number; } } -inline void Window::delay(int millisecondsWait) +inline void Window::delay() { + if(!delayTime) + return; + repaint(); QEventLoop loop; QTimer t; t.connect(&t, &QTimer::timeout, &loop, &QEventLoop::quit); - t.start(millisecondsWait); + t.start(delayTime); loop.exec(); } void Window::clearButtonClicked() diff --git a/window.h b/window.h index b3c878deff875ee6d63bada77cb670b15f560823..266c141c3ebcd5a396a0af311aefe170afe0d998 100644 --- a/window.h +++ b/window.h @@ -29,14 +29,14 @@ private: { Cell *cell; std::array<int, 9> blocked; - History(Cell *c, std::array<int,9> in, int choice) + History(Cell *c, int choice) { cell = c; - blocked =in; - blocked[choice] ++; + blocked = c->blocked; + blocked[choice]++; } }; - inline void delay(int millisecondsWait); + inline void delay(); private slots: void setValue(int s);