From f4f7964f30601cf119697160408efe884c854d90 Mon Sep 17 00:00:00 2001 From: Silas Dohm <silas.dohm@stud.hs-bochum.de> Date: Fri, 2 Sep 2022 12:55:13 +0200 Subject: [PATCH] clean up --- window.cpp | 65 +++++++++++++++++++++++++++--------------------------- window.h | 4 ++-- 2 files changed, 34 insertions(+), 35 deletions(-) diff --git a/window.cpp b/window.cpp index 4969ccd..0f03bc3 100644 --- a/window.cpp +++ b/window.cpp @@ -85,53 +85,52 @@ 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; + int rndNr = rand() % validNrs.size(); // choose a random number; hist.back().cell->collapse(validNrs[rndNr]); // collapse cell with chosen number; hist.back().blocked[validNrs[rndNr]]++; } - 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) { - 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; } } void Window::clearButtonClicked() diff --git a/window.h b/window.h index b2186b1..f4e57ad 100644 --- a/window.h +++ b/window.h @@ -26,10 +26,10 @@ 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 = c->blocked; blocked[choice] ++; } }; -- GitLab