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

moved work

parent 65eae54d
No related branches found
No related tags found
No related merge requests found
......@@ -57,7 +57,6 @@ void Cell::removeOption(int x)
states[x]->setText(" ");
states[x]->setDisabled(true);
possibleStates--;
options.erase(std::find(options.begin(), options.end(), x));
}
blocked[x]++;
}
......@@ -65,13 +64,11 @@ void Cell::removeOption(int x)
void Cell::addOption(int x)
{
blocked[x]--;
// if (!states[x]->isEnabled())
if (blocked[x] < 1)
{
states[x]->setText(QString::number(x + 1));
states[x]->setDisabled(false);
possibleStates++;
options.push_back(x);
}
}
void Cell::collapsedCellClicked(void)
......
......@@ -15,7 +15,7 @@ public:
void resizeEvent(QResizeEvent *event);
int possibleStates = 9;
bool collapsed = false;
std::vector<int> options = {0, 1, 2, 3, 4, 5, 6, 7, 8};
std::array<int, 9> blocked;
private:
QSignalMapper *mapper;
......@@ -23,7 +23,6 @@ private:
QGridLayout *gridLayout;
QPushButton *states[9];
QPushButton *number;
std::array<int, 9> blocked;
QStackedWidget *stackedWidget;
signals:
......
......@@ -9,7 +9,6 @@
#include <QSpacerItem>
#include <QTimer>
Window::Window(QWidget *parent)
: QWidget{parent}
{
......@@ -54,7 +53,8 @@ Window::Window(QWidget *parent)
{
connect(&grid[x + y * 9], SIGNAL(update(int)), &grid[x + d * 9], SLOT(removeOption(int)));
connect(&grid[x + y * 9], SIGNAL(undo(int)), &grid[x + d * 9], SLOT(addOption(int)));
if(&grid[x+y*9]!= &grid[d+y*9]){
if (&grid[x + y * 9] != &grid[d + y * 9])
{
connect(&grid[x + y * 9], SIGNAL(update(int)), &grid[d + y * 9], SLOT(removeOption(int)));
connect(&grid[x + y * 9], SIGNAL(undo(int)), &grid[d + y * 9], SLOT(addOption(int)));
}
......@@ -88,19 +88,21 @@ void Window::solveButtonClicked()
if (backtracking)
{
hist.back().cell->collapsedCellClicked();
if (hist.back().options.size() == 0)
std::vector<int> b;
for (int i = 0; i < 9; i++)
if (hist.back().blocked[i] == 0)
b.push_back(i);
if (b.size() == 0)
{
hist.pop_back();
continue;
}
backtracking = false;
int choiceNr = rand() % hist.back().options.size(); // choose a random number;
int choiceNr = rand() % b.size(); // choose a random number;
repaint(); // draw cells again
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;
hist.back().options.erase(it);
hist.back().cell->collapse(b[choiceNr]); // collapse cell with chosen number;
hist.back().blocked[b[choiceNr]]++;
}
else
{
......@@ -137,9 +139,15 @@ void Window::solveButtonClicked()
}
int choiceCell = rand() % b.size(); // choose a random cell
int choiceNr = rand() % b[choiceCell]->options.size(); // choose a random number;
hist.push_back(History(b[choiceCell], b[choiceCell]->options, choiceNr));
b[choiceCell]->collapse(b[choiceCell]->options[choiceNr]); // collapse that random cell with chosen number;
std::vector<int> c;
for (int i = 0; i < 9; i++)
{
if (b[choiceCell]->blocked[i] == 0)
c.push_back(i);
}
int choiceNr = rand() % c.size(); // choose a random number;
hist.push_back(History(b[choiceCell], b[choiceCell]->blocked, c[choiceNr])); // fix this!!!
b[choiceCell]->collapse(c[choiceNr]); // collapse that random cell with chosen number;
}
}
}
......
......@@ -28,16 +28,12 @@ private:
struct History
{
Cell *cell;
std::vector<int> options;
History(Cell *c, std::vector<int> in, int choice)
std::array<int, 9> blocked;
History(Cell *c, std::array<int,9> in, int choice)
{
cell = c;
for (int i = 0; i < in.size(); i++)
{
if (i == choice)
continue;
options.push_back(in[i]);
}
blocked =in;
blocked[choice] ++;
}
};
inline void delay(int millisecondsWait);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment