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

renamed

parent 8e5577bf
Branches
No related tags found
No related merge requests found
...@@ -9,67 +9,67 @@ ...@@ -9,67 +9,67 @@
Cell::Cell(QWidget *parent) : QWidget(parent) Cell::Cell(QWidget *parent) : QWidget(parent)
{ {
m_mapper = new QSignalMapper(this); mapper = new QSignalMapper(this);
connect(m_mapper, SIGNAL(mapped(int)), this, SLOT(collapse(int))); connect(mapper, SIGNAL(mapped(int)), this, SLOT(collapse(int)));
m_groupBox = new QGroupBox(); groupBox = new QGroupBox();
m_gridLayout = new QGridLayout(); gridLayout = new QGridLayout();
setStyleSheet("border: 1px solid #1e282c; border-radius: 0px;background-color:#52585d;"); setStyleSheet("border: 1px solid #1e282c; border-radius: 0px;background-color:#52585d;");
for (char i = 0; i < 9; i++) for (char i = 0; i < 9; i++)
{ {
m_states[i] = new QPushButton(QString::number(i + 1), this); states[i] = new QPushButton(QString::number(i + 1), this);
m_gridLayout->addWidget(m_states[i], i / 3, i % 3); gridLayout->addWidget(states[i], i / 3, i % 3);
m_states[i]->setStyleSheet("border:none"); states[i]->setStyleSheet("border:none");
connect(m_states[i], SIGNAL(clicked()), m_mapper, SLOT(map())); connect(states[i], SIGNAL(clicked()), mapper, SLOT(map()));
m_mapper->setMapping(m_states[i], i); mapper->setMapping(states[i], i);
m_blocked[i] = 0; blocked[i] = 0;
} }
m_groupBox->setLayout(m_gridLayout); groupBox->setLayout(gridLayout);
m_number = new QPushButton(this); number = new QPushButton(this);
connect(m_number, SIGNAL(clicked()), this, SLOT(collapsedCellClicked())); connect(number, SIGNAL(clicked()), this, SLOT(collapsedCellClicked()));
m_stackedWidget = new QStackedWidget(this); stackedWidget = new QStackedWidget(this);
m_stackedWidget->addWidget(m_groupBox); stackedWidget->addWidget(groupBox);
m_stackedWidget->addWidget(m_number); stackedWidget->addWidget(number);
setMinimumSize(70, 70); setMinimumSize(70, 70);
} }
void Cell::resizeEvent(QResizeEvent *event) void Cell::resizeEvent(QResizeEvent *event)
{ {
QWidget::resizeEvent(event); QWidget::resizeEvent(event);
m_stackedWidget->setFixedSize(this->size()); stackedWidget->setFixedSize(this->size());
int smallest = this->width() < this->height() ? this->width() : this->height(); int smallest = this->width() < this->height() ? this->width() : this->height();
m_number->setStyleSheet("font-size:" + QString::number(smallest / 2) + "px;"); number->setStyleSheet("font-size:" + QString::number(smallest / 2) + "px;");
return; return;
} }
void Cell::collapse(int x) void Cell::collapse(int x)
{ {
collapsed = true; collapsed = true;
m_stackedWidget->setCurrentIndex(1); stackedWidget->setCurrentIndex(1);
m_number->setText(QString::number(x + 1)); number->setText(QString::number(x + 1));
emit update(x); emit update(x);
} }
void Cell::removeOption(int x) void Cell::removeOption(int x)
{ {
if (m_blocked[x] == 0) if (blocked[x] == 0)
{ {
m_states[x]->setText(" "); states[x]->setText(" ");
m_states[x]->setDisabled(true); states[x]->setDisabled(true);
possibleStates--; possibleStates--;
options.erase(std::find(options.begin(), options.end(), x)); options.erase(std::find(options.begin(), options.end(), x));
} }
m_blocked[x]++; blocked[x]++;
} }
void Cell::addOption(int x) void Cell::addOption(int x)
{ {
m_blocked[x]--; blocked[x]--;
// if (!m_states[x]->isEnabled()) // if (!states[x]->isEnabled())
if (m_blocked[x] < 1) if (blocked[x] < 1)
{ {
m_states[x]->setText(QString::number(x + 1)); states[x]->setText(QString::number(x + 1));
m_states[x]->setDisabled(false); states[x]->setDisabled(false);
possibleStates++; possibleStates++;
options.push_back(x); options.push_back(x);
} }
...@@ -77,6 +77,6 @@ void Cell::addOption(int x) ...@@ -77,6 +77,6 @@ void Cell::addOption(int x)
void Cell::collapsedCellClicked(void) void Cell::collapsedCellClicked(void)
{ {
collapsed = false; collapsed = false;
m_stackedWidget->setCurrentIndex(0); stackedWidget->setCurrentIndex(0);
emit undo(m_number->text().toInt() - 1); emit undo(number->text().toInt() - 1);
} }
\ No newline at end of file
...@@ -18,13 +18,13 @@ public: ...@@ -18,13 +18,13 @@ public:
std::vector<int> options = {0, 1, 2, 3, 4, 5, 6, 7, 8}; std::vector<int> options = {0, 1, 2, 3, 4, 5, 6, 7, 8};
private: private:
QSignalMapper *m_mapper; QSignalMapper *mapper;
QGroupBox *m_groupBox; QGroupBox *groupBox;
QGridLayout *m_gridLayout; QGridLayout *gridLayout;
QPushButton *m_states[9]; QPushButton *states[9];
QPushButton *m_number; QPushButton *number;
std::array<int, 9> m_blocked; std::array<int, 9> blocked;
QStackedWidget *m_stackedWidget; QStackedWidget *stackedWidget;
signals: signals:
void update(int x); void update(int x);
......
...@@ -13,35 +13,35 @@ ...@@ -13,35 +13,35 @@
Window::Window(QWidget *parent) Window::Window(QWidget *parent)
: QWidget{parent} : QWidget{parent}
{ {
m_solveButton = new QPushButton("solve", this); solveButton = new QPushButton("solve", this);
connect(m_solveButton, SIGNAL(clicked()), this, SLOT(solveButtonClicked())); connect(solveButton, SIGNAL(clicked()), this, SLOT(solveButtonClicked()));
m_clearButton = new QPushButton("Clear", this); clearButton = new QPushButton("Clear", this);
connect(m_clearButton, SIGNAL(clicked()), this, SLOT(clearButtonClicked())); connect(clearButton, SIGNAL(clicked()), this, SLOT(clearButtonClicked()));
m_slider = new QSlider(this); slider = new QSlider(this);
m_slider->setRange(0, 1000); slider->setRange(0, 1000);
m_slider->setOrientation(Qt::Horizontal); slider->setOrientation(Qt::Horizontal);
m_slider->setValue(m_delay); slider->setValue(delayTime);
connect(m_slider, SIGNAL(valueChanged(int)), this, SLOT(setValue(int))); connect(slider, SIGNAL(valueChanged(int)), this, SLOT(setValue(int)));
m_horizontalLayout = new QHBoxLayout(this); horizontalLayout = new QHBoxLayout(this);
m_verticalLayout = new QVBoxLayout(); verticalLayout = new QVBoxLayout();
m_layout = new QGridLayout(); layout = new QGridLayout();
m_spacer[0] = new QSpacerItem(4, 4, QSizePolicy::Minimum, QSizePolicy::Fixed); spacer[0] = new QSpacerItem(4, 4, QSizePolicy::Minimum, QSizePolicy::Fixed);
m_spacer[1] = new QSpacerItem(4, 4, QSizePolicy::Minimum, QSizePolicy::Fixed); spacer[1] = new QSpacerItem(4, 4, QSizePolicy::Minimum, QSizePolicy::Fixed);
m_spacer[2] = new QSpacerItem(8, 8, QSizePolicy::Minimum, QSizePolicy::Expanding); spacer[2] = new QSpacerItem(8, 8, QSizePolicy::Minimum, QSizePolicy::Expanding);
m_verticalLayout->addItem(m_spacer[2]); verticalLayout->addItem(spacer[2]);
m_verticalLayout->addWidget(m_solveButton); verticalLayout->addWidget(solveButton);
m_verticalLayout->addWidget(m_slider); verticalLayout->addWidget(slider);
m_verticalLayout->addWidget(m_clearButton); verticalLayout->addWidget(clearButton);
m_horizontalLayout->addLayout(m_layout, 5); horizontalLayout->addLayout(layout, 5);
m_horizontalLayout->addLayout(m_verticalLayout, 1); horizontalLayout->addLayout(verticalLayout, 1);
m_layout->addItem(m_spacer[0], 3, 3); layout->addItem(spacer[0], 3, 3);
m_layout->addItem(m_spacer[1], 7, 7); layout->addItem(spacer[1], 7, 7);
m_layout->setSpacing(0); layout->setSpacing(0);
int dx = 0; int dx = 0;
int dy = 0; int dy = 0;
...@@ -49,16 +49,16 @@ Window::Window(QWidget *parent) ...@@ -49,16 +49,16 @@ Window::Window(QWidget *parent)
{ {
for (int y = 0; y < 9; y++) for (int y = 0; y < 9; y++)
{ {
m_cell[x + y * 9].setParent(this); cell[x + y * 9].setParent(this);
m_layout->addWidget(&m_cell[x + y * 9], y + y / 3, x + x / 3); layout->addWidget(&cell[x + y * 9], y + y / 3, x + x / 3);
// connecting signals & slots // connecting signals & slots
for (int d = 0; d < 9; d++) // rows and columns 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))); connect(&cell[x + y * 9], SIGNAL(update(int)), &cell[x + d * 9], SLOT(removeOption(int)));
connect(&m_cell[x + y * 9], SIGNAL(update(int)), &m_cell[d + y * 9], SLOT(removeOption(int))); connect(&cell[x + y * 9], SIGNAL(update(int)), &cell[d + y * 9], SLOT(removeOption(int)));
connect(&m_cell[x + y * 9], SIGNAL(undo(int)), &m_cell[x + d * 9], SLOT(addOption(int))); connect(&cell[x + y * 9], SIGNAL(undo(int)), &cell[x + d * 9], SLOT(addOption(int)));
connect(&m_cell[x + y * 9], SIGNAL(undo(int)), &m_cell[d + y * 9], SLOT(addOption(int))); connect(&cell[x + y * 9], SIGNAL(undo(int)), &cell[d + y * 9], SLOT(addOption(int)));
} }
int a = x / 3 * 3; int a = x / 3 * 3;
int b = y / 3 * 3; int b = y / 3 * 3;
...@@ -68,9 +68,9 @@ Window::Window(QWidget *parent) ...@@ -68,9 +68,9 @@ Window::Window(QWidget *parent)
{ {
if (x1 == x || y1 == y) if (x1 == x || y1 == y)
continue; continue;
connect(&m_cell[x + y * 9], SIGNAL(update(int)), &m_cell[x1 + y1 * 9], SLOT(removeOption(int))); connect(&cell[x + y * 9], SIGNAL(update(int)), &cell[x1 + y1 * 9], SLOT(removeOption(int)));
connect(&m_cell[x + y * 9], SIGNAL(undo(int)), &m_cell[x1 + y1 * 9], SLOT(addOption(int))); connect(&cell[x + y * 9], SIGNAL(undo(int)), &cell[x1 + y1 * 9], SLOT(addOption(int)));
} }
} }
} }
...@@ -85,7 +85,7 @@ void Window::solveButtonClicked() ...@@ -85,7 +85,7 @@ void Window::solveButtonClicked()
while (1) while (1)
{ {
repaint(); // draw cells again repaint(); // draw cells again
delay(m_delay); delay(delayTime);
if (backtracking) if (backtracking)
{ {
hist.back().cell->collapsedCellClicked(); hist.back().cell->collapsedCellClicked();
...@@ -97,7 +97,7 @@ void Window::solveButtonClicked() ...@@ -97,7 +97,7 @@ void Window::solveButtonClicked()
backtracking = false; backtracking = false;
int choiceNr = rand() % hist.back().options.size(); // choose a random number; int choiceNr = rand() % hist.back().options.size(); // choose a random number;
repaint(); // draw cells again repaint(); // draw cells again
delay(m_delay); delay(delayTime);
hist.back().cell->collapse(hist.back().options[choiceNr]); // collapse cell with chosen number; hist.back().cell->collapse(hist.back().options[choiceNr]); // collapse cell with chosen number;
std::vector<int>::iterator it; std::vector<int>::iterator it;
it = hist.back().options.begin() + choiceNr; it = hist.back().options.begin() + choiceNr;
...@@ -111,17 +111,17 @@ void Window::solveButtonClicked() ...@@ -111,17 +111,17 @@ void Window::solveButtonClicked()
{ {
for (int y = 0; y < 9; y++) for (int y = 0; y < 9; y++)
{ {
if (m_cell[x + y * 9].collapsed) if (cell[x + y * 9].collapsed)
continue; continue;
else if (m_cell[x + y * 9].possibleStates < minEtropy) else if (cell[x + y * 9].possibleStates < minEtropy)
{ {
minEtropy = m_cell[x + y * 9].possibleStates; minEtropy = cell[x + y * 9].possibleStates;
b.clear(); b.clear();
b.push_back(&m_cell[x + y * 9]); b.push_back(&cell[x + y * 9]);
} }
else if (m_cell[x + y * 9].possibleStates == minEtropy) else if (cell[x + y * 9].possibleStates == minEtropy)
{ {
b.push_back(&m_cell[x + y * 9]); b.push_back(&cell[x + y * 9]);
} }
} }
} }
...@@ -154,7 +154,7 @@ inline void Window::delay(int millisecondsWait) ...@@ -154,7 +154,7 @@ inline void Window::delay(int millisecondsWait)
} }
void Window::clearButtonClicked() void Window::clearButtonClicked()
{ {
for (auto &x : m_cell) for (auto &x : cell)
{ {
if (x.collapsed) if (x.collapsed)
x.collapsedCellClicked(); x.collapsedCellClicked();
...@@ -163,6 +163,6 @@ void Window::clearButtonClicked() ...@@ -163,6 +163,6 @@ void Window::clearButtonClicked()
} }
void Window::setValue(int s) void Window::setValue(int s)
{ {
m_delay = s; delayTime = s;
return; return;
} }
\ No newline at end of file
...@@ -16,15 +16,15 @@ public: ...@@ -16,15 +16,15 @@ public:
explicit Window(QWidget *parent = nullptr); explicit Window(QWidget *parent = nullptr);
private: private:
QPushButton *m_clearButton; QPushButton *clearButton;
QPushButton *m_solveButton; QPushButton *solveButton;
QSlider *m_slider; QSlider *slider;
std::array<Cell, 81> m_cell; std::array<Cell, 81> cell;
int m_delay = 300; int delayTime = 300;
QSpacerItem *m_spacer[3]; QSpacerItem *spacer[3];
QHBoxLayout *m_horizontalLayout; QHBoxLayout *horizontalLayout;
QVBoxLayout *m_verticalLayout; QVBoxLayout *verticalLayout;
QGridLayout *m_layout; QGridLayout *layout;
struct History struct History
{ {
Cell *cell; Cell *cell;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment