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

renamed

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