diff --git a/cell.cpp b/cell.cpp index 9fa90822afc9e36b5b536efbfc0e7955c05fa487..9e7a3e468f97e8821d4740b144718c86e5d39fac 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 1d1c53a51d313038cf338ea78018105ce2290e4f..a90e7b6086c0b74a63412055bf70c33b57d7a99f 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 2b7236eca5a1d2e2bd682a2316600e2d78929b94..44f3bec512fb44c675f2a6d0894048ac03455e30 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 08c704847bfb7f8fbbafaee21ddded365438a925..2a6b5db0f64cd306726a433667c35abe816929b2 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);