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

collapse reverse functionality added

parent dee1cc35
No related branches found
No related tags found
1 merge request!1Master
...@@ -14,28 +14,52 @@ Cell::Cell(QWidget *parent) : QWidget(parent) ...@@ -14,28 +14,52 @@ Cell::Cell(QWidget *parent) : QWidget(parent)
float w = width() / 3.0; float w = width() / 3.0;
float h = height() / 3.0; float h = height() / 3.0;
QSignalMapper *mapper = new QSignalMapper(this); QSignalMapper *mapper = new QSignalMapper(this);
connect(mapper, SIGNAL(mapped(int)), this, SLOT(slotButtonClicked(int))); connect(mapper, SIGNAL(mapped(int)), this, SLOT(collapse(int)));
for (char i = 0; i < 9; i++) for (char i = 0; i < 9; i++)
{ {
states[i] = new QPushButton(QString::number(i), this); m_states[i] = new QPushButton(QString::number(i + 1), this);
states[i]->setGeometry(i%3 * w, i/3 * h, w, h); m_states[i]->setGeometry(i % 3 * w, i / 3 * h, w, h);
states[i]->setStyleSheet("border:none"); m_states[i]->setStyleSheet("border:none");
connect(states[i], SIGNAL(clicked()), mapper, SLOT(map())); connect(m_states[i], SIGNAL(clicked()), mapper, SLOT(map()));
mapper->setMapping(states[i], i); mapper->setMapping(m_states[i], i);
} }
collapsed = new QPushButton(this); m_number = new QPushButton(this);
collapsed->setFixedSize(100, 100); m_number->setFixedSize(100, 100);
collapsed->setStyleSheet("font-size:44px"); m_number->setStyleSheet("font-size:44px");
collapsed->hide(); m_number->hide();
connect(m_number,SIGNAL(clicked()),this,SLOT(un()));
} }
void Cell::slotButtonClicked(int x) void Cell::collapse(int x)
{ {
collapsed->show(); collapsed = true;
collapsed->setText(QString::number(x)); m_number->show();
emit(collapse(x)); m_number->setText(QString::number(x + 1));
emit(update(x));
} }
void Cell::foo(int x) void Cell::removeOption(int x)
{
// states[x]->hide();
if (m_states[x]->isEnabled())
{ {
states[x]->hide(); m_states[x]->setDisabled(true);
possibleStates--;
index.erase(std::find(index.begin(), index.end(), x));
}
}
void Cell::addOption(int x)
{
if (!m_states[x]->isEnabled())
{
m_states[x]->setDisabled(false);
possibleStates++;
index.push_back(x);
}
}
void Cell::un(void){
collapsed = false;
m_number->hide();
emit(undo(m_number->text().toInt()-1));
} }
\ No newline at end of file
...@@ -9,16 +9,23 @@ class Cell : public QWidget ...@@ -9,16 +9,23 @@ class Cell : public QWidget
Q_OBJECT Q_OBJECT
public: public:
explicit Cell(QWidget *parent = 0); explicit Cell(QWidget *parent = 0);
int possibleStates=9;
bool collapsed = false;
std::vector<int> index={0,1,2,3,4,5,6,7,8};
private: private:
QPushButton *states[9]; QPushButton *m_states[9];
QPushButton *collapsed; QPushButton *m_number;
signals: signals:
void collapse(int x); void update(int x);
void undo(int x);
public slots: public slots:
void foo(int x); void addOption(int x);
void removeOption(int x);
void collapse(int x);
private slots: private slots:
void slotButtonClicked(int x); void un(void);
}; };
#endif // CELL_H #endif // CELL_H
\ No newline at end of file
...@@ -2,14 +2,24 @@ ...@@ -2,14 +2,24 @@
#include <QApplication> #include <QApplication>
#include <QStandardPaths> #include <QStandardPaths>
#include <QDebug> #include <QDebug>
#include <QThread>
#include <QProgressBar>
#include <QSlider>
Window::Window(QWidget *parent) Window::Window(QWidget *parent)
: QWidget{parent} : QWidget{parent}
{ {
m_counter = 0; setFixedWidth(1600);
// quitbutton m_button = new QPushButton("solve", this);
// m_buttonQuit = new QPushButton("quit", this); connect(m_button, SIGNAL(clicked()), this, SLOT(solveButtonClicked()));
// m_buttonQuit->setGeometry(0, height() - 30, width(), 30); m_button->setGeometry(1200, height() - 30, width() - 1200, 30);
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);
connect(m_slider,SIGNAL(valueChanged(int)),this,SLOT(setValue(int)));
int dx = 0; int dx = 0;
int dy = 0; int dy = 0;
for (int x = 0; x < 9; x++) for (int x = 0; x < 9; x++)
...@@ -36,20 +46,73 @@ Window::Window(QWidget *parent) ...@@ -36,20 +46,73 @@ Window::Window(QWidget *parent)
{ {
for (int y = 0; y < 9; y++) for (int y = 0; y < 9; y++)
{ {
for (int d = 0; d < 9; d++) for (int d = 0; d < 9; d++) //rows and columns
{ {
connect(m_cell[x][y], SIGNAL(collapse(int)), m_cell[x][d], SLOT(foo(int))); connect(m_cell[x][y], SIGNAL(update(int)), m_cell[x][d], SLOT(removeOption(int)));
connect(m_cell[x][y], SIGNAL(collapse(int)), m_cell[d][y], SLOT(foo(int))); connect(m_cell[x][y], SIGNAL(update(int)), m_cell[d][y], SLOT(removeOption(int)));
connect(m_cell[x][y], SIGNAL(undo(int)), m_cell[x][d], SLOT(addOption(int)));
connect(m_cell[x][y], SIGNAL(undo(int)), m_cell[d][y], SLOT(addOption(int)));
} }
int a = x / 3 * 3; int a = x / 3 * 3;
int b = y / 3 * 3; int b = y / 3 * 3;
for (int x1=a; x1 < a+3; x1++) for (int x1 = a; x1 < a + 3; x1++) //square
{ {
for (int y1 = b; y1 < b + 3; y1++) for (int y1 = b; y1 < b + 3; y1++)
{ {
connect(m_cell[x][y], SIGNAL(collapse(int)), m_cell[x1][y1], SLOT(foo(int))); connect(m_cell[x][y], SIGNAL(update(int)), m_cell[x1][y1], SLOT(removeOption(int)));
connect(m_cell[x][y], SIGNAL(undo(int)), m_cell[x1][y1], SLOT(addOption(int)));
}
}
}
}
}
void Window::solveButtonClicked()
{
srand(time(NULL));
while (1)
{
std::vector<Cell *> b; //vector of cells with least entropy
int minEtropy = 10;
for (int x = 0; x < 9; x++) //loop through all cells and fill list b --> this could be optimzed
{
for (int y = 0; y < 9; y++)
{
if (m_cell[x][y]->collapsed)
continue;
else if (m_cell[x][y]->possibleStates < minEtropy)
{
minEtropy = m_cell[x][y]->possibleStates;
b.clear();
b.push_back(m_cell[x][y]);
} }
else if (m_cell[x][y]->possibleStates == minEtropy)
{
b.push_back(m_cell[x][y]);
} }
} }
} }
if (b.size() == 0) //if b is empty -> solved
{
qInfo("Solved!!");
return;
}
else if(minEtropy ==0){
qInfo("dead End reached");
return;
}
int choiceCell = rand() % b.size(); //choose a random cell
int choiceNr = rand() % b[choiceCell]->index.size(); //choose a random number;
b[choiceCell]->collapse(b[choiceCell]->index[choiceNr]); //collapse that random cell with chosen number;
repaint(); //draw cells again
QThread::msleep(m_delay);
}
// qInfo("%d---:)");
}
void Window::setValue(int s){
m_delay = s;
return;
} }
\ No newline at end of file
...@@ -18,11 +18,12 @@ private: ...@@ -18,11 +18,12 @@ private:
QProgressBar *m_progressBar; QProgressBar *m_progressBar;
QSlider *m_slider; QSlider *m_slider;
Cell *m_cell[9][9]; Cell *m_cell[9][9];
int m_counter; int m_delay =300;
signals: signals:
void counterReached(); void counterReached();
private slots: private slots:
void setValue(int s);
void solveButtonClicked();
}; };
#endif // WINDOW_H #endif // WINDOW_H
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment