Skip to content

Commit

Permalink
enable remaining buttons in game view
Browse files Browse the repository at this point in the history
  • Loading branch information
valpo committed Aug 10, 2015
1 parent 2537941 commit d519927
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 9 deletions.
6 changes: 6 additions & 0 deletions qt/buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,9 @@ bool Buffer::empty() const
return buffer.isEmpty();
}

void Buffer::clear()
{
QMutexLocker locker(&lock);
buffer.clear();
}

1 change: 1 addition & 0 deletions qt/buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Buffer : public QObject
void put(const Sudoku& s);
Sudoku get();
bool empty() const;
void clear();

signals:
void added(); // emitted if item added
Expand Down
68 changes: 62 additions & 6 deletions qt/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ static unsigned SudokuIn[9][9] = {
};

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent), sudoku(9)
QMainWindow(parent), sudoku(9), startGame(9)
{
setupUi(this);
initSudoku();
Expand Down Expand Up @@ -105,16 +105,72 @@ void MainWindow::refreshView()
void MainWindow::on_generateButton_clicked()
{
qDebug("generate sudoku");
viewRefresh->stop();
while (solver) {
bool res = solver->wait();
if (res) {
solver->deleteLater();
solver = nullptr;
}
}
buffer.clear();
state = State::IDLE;
sudoku.generate();
startGame = sudoku;
showSudoku(sudoku);
qDebug("generate sudoku finished");
}

void MainWindow::on_playButton_clicked()
{
Solver *solv = new Solver(&buffer, sudoku, this);
connect(solv, SIGNAL(finished()), this, SLOT(solverFinished()));
solv->start();
solverRunning = true;
viewRefresh->start(0);
if (state == State::SOLVER_PAUSING or state == State::IDLE) {
if (!solver) {
startGame = sudoku;
solver = new Solver(&buffer, sudoku, this);
connect(solver, SIGNAL(finished()), this, SLOT(solverFinished()));
solver->start();
solverRunning = true;
}
viewRefresh->start(0);
playButton->setText("Stop");
state = State::SOLVER_PLAYING;
}
else if (state == State::SOLVER_PLAYING) {
viewRefresh->stop();
playButton->setText("Play");
state = State::SOLVER_PAUSING;
}
}

void MainWindow::on_resetButton_clicked()
{
viewRefresh->stop();
while (solver) {
bool res = solver->wait();
if (res) {
solver->deleteLater();
solver = nullptr;
}
}
buffer.clear();
sudoku = startGame;
showSudoku(sudoku);
state = State::IDLE;
playButton->setText("Play");
}

void MainWindow::on_nextButton_clicked()
{

viewRefresh->stop();
if (!solver) {
startGame = sudoku;
solver = new Solver(&buffer, sudoku, this);
connect(solver, SIGNAL(finished()), this, SLOT(solverFinished()));
solver->start();
solverRunning = true;
}
state = State::SOLVER_PAUSING;
playButton->setText("Play");
refreshView();
}
8 changes: 7 additions & 1 deletion qt/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,20 @@
#include <vector>

class SudokuLabel;
class Solver;

class MainWindow : public QMainWindow, private Ui::MainWindow
{
Q_OBJECT
Sudoku sudoku;
enum class State { IDLE, SOLVER_PLAYING, SOLVER_PAUSING };
State state = State::IDLE;
Sudoku sudoku, startGame;
Buffer buffer;
QTimer *viewRefresh;
bool solverRunning;
std::vector<SudokuLabel*> labelVec; // speed up access to labels
QElapsedTimer *bencher = 0;
Solver *solver = 0;
public:
explicit MainWindow(QWidget *parent = 0);
void showSudoku(const Sudoku& s);
Expand All @@ -30,6 +34,8 @@ private slots:
void refreshView(); // gets item from buffer and displays it
void on_generateButton_clicked();
void on_playButton_clicked();
void on_resetButton_clicked();
void on_nextButton_clicked();
};

#endif // MAINWINDOW_H
4 changes: 2 additions & 2 deletions qt/mainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ p, li { white-space: pre-wrap; }
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton_2">
<widget class="QPushButton" name="nextButton">
<property name="text">
<string>Next</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton">
<widget class="QPushButton" name="resetButton">
<property name="text">
<string>Reset</string>
</property>
Expand Down

0 comments on commit d519927

Please sign in to comment.