Skip to content
Snippets Groups Projects
Commit e9e29398 authored by Silas Dohm's avatar Silas Dohm
Browse files

moved work

parent 65eae54d
No related branches found
No related tags found
No related merge requests found
...@@ -57,7 +57,6 @@ void Cell::removeOption(int x) ...@@ -57,7 +57,6 @@ void Cell::removeOption(int x)
states[x]->setText(" "); states[x]->setText(" ");
states[x]->setDisabled(true); states[x]->setDisabled(true);
possibleStates--; possibleStates--;
options.erase(std::find(options.begin(), options.end(), x));
} }
blocked[x]++; blocked[x]++;
} }
...@@ -65,13 +64,11 @@ void Cell::removeOption(int x) ...@@ -65,13 +64,11 @@ void Cell::removeOption(int x)
void Cell::addOption(int x) void Cell::addOption(int x)
{ {
blocked[x]--; blocked[x]--;
// if (!states[x]->isEnabled())
if (blocked[x] < 1) if (blocked[x] < 1)
{ {
states[x]->setText(QString::number(x + 1)); states[x]->setText(QString::number(x + 1));
states[x]->setDisabled(false); states[x]->setDisabled(false);
possibleStates++; possibleStates++;
options.push_back(x);
} }
} }
void Cell::collapsedCellClicked(void) void Cell::collapsedCellClicked(void)
......
...@@ -15,7 +15,7 @@ public: ...@@ -15,7 +15,7 @@ public:
void resizeEvent(QResizeEvent *event); void resizeEvent(QResizeEvent *event);
int possibleStates = 9; int possibleStates = 9;
bool collapsed = false; bool collapsed = false;
std::vector<int> options = {0, 1, 2, 3, 4, 5, 6, 7, 8}; std::array<int, 9> blocked;
private: private:
QSignalMapper *mapper; QSignalMapper *mapper;
...@@ -23,7 +23,6 @@ private: ...@@ -23,7 +23,6 @@ private:
QGridLayout *gridLayout; QGridLayout *gridLayout;
QPushButton *states[9]; QPushButton *states[9];
QPushButton *number; QPushButton *number;
std::array<int, 9> blocked;
QStackedWidget *stackedWidget; QStackedWidget *stackedWidget;
signals: signals:
......
...@@ -9,7 +9,6 @@ ...@@ -9,7 +9,6 @@
#include <QSpacerItem> #include <QSpacerItem>
#include <QTimer> #include <QTimer>
Window::Window(QWidget *parent) Window::Window(QWidget *parent)
: QWidget{parent} : QWidget{parent}
{ {
...@@ -54,7 +53,8 @@ Window::Window(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(update(int)), &grid[x + d * 9], SLOT(removeOption(int)));
connect(&grid[x + y * 9], SIGNAL(undo(int)), &grid[x + d * 9], SLOT(addOption(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(update(int)), &grid[d + y * 9], SLOT(removeOption(int)));
connect(&grid[x + y * 9], SIGNAL(undo(int)), &grid[d + y * 9], SLOT(addOption(int))); connect(&grid[x + y * 9], SIGNAL(undo(int)), &grid[d + y * 9], SLOT(addOption(int)));
} }
...@@ -88,19 +88,21 @@ void Window::solveButtonClicked() ...@@ -88,19 +88,21 @@ void Window::solveButtonClicked()
if (backtracking) if (backtracking)
{ {
hist.back().cell->collapsedCellClicked(); 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(); hist.pop_back();
continue; continue;
} }
backtracking = false; backtracking = false;
int choiceNr = rand() % hist.back().options.size(); // choose a random number; int choiceNr = rand() % b.size(); // choose a random number;
repaint(); // draw cells again repaint(); // draw cells again
delay(delayTime); delay(delayTime);
hist.back().cell->collapse(hist.back().options[choiceNr]); // collapse cell with chosen number; hist.back().cell->collapse(b[choiceNr]); // collapse cell with chosen number;
std::vector<int>::iterator it; hist.back().blocked[b[choiceNr]]++;
it = hist.back().options.begin() + choiceNr;
hist.back().options.erase(it);
} }
else else
{ {
...@@ -137,9 +139,15 @@ void Window::solveButtonClicked() ...@@ -137,9 +139,15 @@ void Window::solveButtonClicked()
} }
int choiceCell = rand() % b.size(); // choose a random cell int choiceCell = rand() % b.size(); // choose a random cell
int choiceNr = rand() % b[choiceCell]->options.size(); // choose a random number; std::vector<int> c;
hist.push_back(History(b[choiceCell], b[choiceCell]->options, choiceNr)); for (int i = 0; i < 9; i++)
b[choiceCell]->collapse(b[choiceCell]->options[choiceNr]); // collapse that random cell with chosen number; {
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;
} }
} }
} }
......
...@@ -28,16 +28,12 @@ private: ...@@ -28,16 +28,12 @@ private:
struct History struct History
{ {
Cell *cell; Cell *cell;
std::vector<int> options; std::array<int, 9> blocked;
History(Cell *c, std::vector<int> in, int choice) History(Cell *c, std::array<int,9> in, int choice)
{ {
cell = c; cell = c;
for (int i = 0; i < in.size(); i++) blocked =in;
{ blocked[choice] ++;
if (i == choice)
continue;
options.push_back(in[i]);
}
} }
}; };
inline void delay(int millisecondsWait); inline void delay(int millisecondsWait);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment