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

backtracking

parent b893c7ad
Branches
No related tags found
1 merge request!2backtracking
......@@ -71,10 +71,27 @@ Window::Window(QWidget *parent)
void Window::solveButtonClicked()
{
// std::array<Cell,81> start = m_cell;
std::list<Cell*> hist;
std::list<History *> hist;
bool end = false;
srand(time(NULL));
while (1)
{
if (end)
{
hist.back()->cell->un();
if (hist.back()->index.size() == 0)
{
hist.pop_back();
continue;
}
end = false;
int choiceNr = rand() % hist.back()->index.size(); // choose a random number;
hist.back()->cell->collapse(hist.back()->index[choiceNr]); // collapse cell with chosen number;
std::vector<int>::iterator it;
it = hist.back()->index.begin() + choiceNr;
hist.back()->index.erase(it);
}
else
{
std::vector<Cell *> b; // vector of cells with least entropy
int minEtropy = 10;
......@@ -101,26 +118,26 @@ void Window::solveButtonClicked()
qInfo("Solved!!");
return;
}
else if(minEtropy ==0){
while (hist.size()>0)
else if (minEtropy == 0)
{
hist.back()->un();
hist.pop_back();
}
return;
//solveButtonClicked();
qInfo("backtracking");
end = true;
continue;
}
int choiceCell = rand() % b.size(); // choose a random cell
int choiceNr = rand() % b[choiceCell]->index.size(); // choose a random number;
hist.push_back(new History(b[choiceCell], b[choiceCell]->index, choiceNr));
b[choiceCell]->collapse(b[choiceCell]->index[choiceNr]); // collapse that random cell with chosen number;
hist.push_back(b[choiceCell]);
}
repaint(); // draw cells again
QThread::msleep(m_delay);
}
// qInfo("%d---:)");
}
void Window::setValue(int s){
void Window::setValue(int s)
{
m_delay = s;
return;
}
\ No newline at end of file
......@@ -11,6 +11,7 @@ class Window : public QWidget
Q_OBJECT
public:
explicit Window(QWidget *parent = nullptr);
private:
QPushButton *m_buttonQuit;
QPushButton *m_button;
......@@ -19,6 +20,21 @@ private:
QSlider *m_slider;
std::array<Cell, 81> m_cell;
int m_delay = 300;
struct History
{
Cell *cell;
std::vector<int> index;
History(Cell *c, std::vector<int> in,int choice)
{
cell = c;
for(int i=0;i<in.size();i++){
if(i == choice)
continue;
index.push_back(in[i]);
}
}
};
signals:
void counterReached();
private slots:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment