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

Merge branch 'feature' into 'main'

backtracking

See merge request sdohm/sodoku-solver!2
parents b893c7ad e42ed312
No related branches found
No related tags found
1 merge request!2backtracking
......@@ -31,15 +31,6 @@ Cell::Cell(QWidget *parent) : QWidget(parent)
connect(m_number,SIGNAL(clicked()),this,SLOT(un()));
}
Cell::Cell(const Cell &old_obj){
possibleStates = old_obj.possibleStates;
collapsed = old_obj.collapsed;
index = old_obj.index;
m_number = new QPushButton(old_obj.m_number);
//m_states = old_obj.m_states;
qInfo("aaa");
}
void Cell::collapse(int x)
{
collapsed = true;
......
......@@ -9,7 +9,6 @@ class Cell : public QWidget
Q_OBJECT
public:
explicit Cell(QWidget *parent = 0);
Cell (const Cell &old_obj);
int possibleStates=9;
bool collapsed = false;
std::vector<int> index={0,1,2,3,4,5,6,7,8};
......
......@@ -10,14 +10,18 @@ Window::Window(QWidget *parent)
: QWidget{parent}
{
setFixedWidth(1600);
m_button = new QPushButton("solve", this);
connect(m_button, SIGNAL(clicked()), this, SLOT(solveButtonClicked()));
m_button->setGeometry(1200, height() - 30, width() - 1200, 30);
m_solveButton = new QPushButton("solve", this);
m_solveButton->setGeometry(1100, height() - 100, width() - 1200, 30);
connect(m_solveButton, SIGNAL(clicked()), this, SLOT(solveButtonClicked()));
m_clearButton = new QPushButton("Clear", this);
m_clearButton->setGeometry(1100, height() - 30, width() - 1200, 30);
connect(m_clearButton, SIGNAL(clicked()), this, SLOT(clearButtonClicked()));
m_slider = new QSlider(this);
m_slider->setRange(0, 1000);
m_slider->setOrientation(Qt::Horizontal);
m_slider->setValue(m_delay);
m_slider->setGeometry(1200, height() - 60, width() - 1200, 30);
m_slider->setGeometry(1100, height() - 60, width() - 1200, 30);
connect(m_slider, SIGNAL(valueChanged(int)), this, SLOT(setValue(int)));
int dx = 0;
......@@ -38,14 +42,8 @@ Window::Window(QWidget *parent)
dy = 8;
m_cell[x + y * 9].setParent(this);
m_cell[x + y * 9].setGeometry(x * 100 + dx, y * 100 + dy, 100, 100);
/* code */
}
}
// init signals
for (int x = 0; x < 9; x++)
{
for (int y = 0; y < 9; y++)
{
//connecting signals & slots
for (int d = 0; d < 9; d++) // rows and columns
{
connect(&m_cell[x + y * 9], SIGNAL(update(int)), &m_cell[x + d * 9], SLOT(removeOption(int)));
......@@ -60,6 +58,8 @@ Window::Window(QWidget *parent)
{
for (int y1 = b; y1 < b + 3; y1++)
{
if(x1 == x || y1 == y)
continue;
connect(&m_cell[x + y * 9], SIGNAL(update(int)), &m_cell[x1 + y1 * 9], SLOT(removeOption(int)));
connect(&m_cell[x + y * 9], SIGNAL(undo(int)), &m_cell[x1 + y1 * 9], SLOT(addOption(int)));
......@@ -71,10 +71,31 @@ Window::Window(QWidget *parent)
void Window::solveButtonClicked()
{
// std::array<Cell,81> start = m_cell;
std::list<Cell*> hist;
srand(time(NULL));
std::list<History *> hist;
bool backtracking = false;
srand(time(NULL)); //random seed
while (1)
{
repaint(); // draw cells again
QThread::msleep(m_delay);
if (backtracking)
{
hist.back()->cell->un();
if (hist.back()->index.size() == 0)
{
hist.pop_back();
continue;
}
backtracking = false;
int choiceNr = rand() % hist.back()->index.size(); // choose a random number;
repaint(); // draw cells again
QThread::msleep(m_delay);
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 +122,33 @@ 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");
backtracking = 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::clearButtonClicked()
{
for (auto &x : m_cell)
{
if (x.collapsed)
x.un();
}
return;
}
void Window::setValue(int s)
{
m_delay = s;
return;
}
\ No newline at end of file
......@@ -11,19 +11,36 @@ class Window : public QWidget
Q_OBJECT
public:
explicit Window(QWidget *parent = nullptr);
private:
QPushButton *m_buttonQuit;
QPushButton *m_button;
QPushButton *m_clearButton;
QPushButton *m_solveButton;
QPushButton *grid[9][9];
QProgressBar *m_progressBar;
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:
void setValue(int s);
void solveButtonClicked();
void clearButtonClicked();
};
#endif // WINDOW_H
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment