From 88efb393c2739a14432391de113f180d00b702df Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Sat, 13 Jul 2013 23:04:45 +0000 Subject: Cleaned up message boxes, unified "Save" and "Save as" overwrite warnings. --- TODO | 5 ++- src/main.cc | 6 --- src/solverwindow.cc | 113 ++++++++++++++++++++++++++++++++++++---------------- src/solverwindow.h | 2 + src/sudoku.h | 11 ++++- 5 files changed, 93 insertions(+), 44 deletions(-) diff --git a/TODO b/TODO index 3ea2985..b0e56bf 100644 --- a/TODO +++ b/TODO @@ -1,9 +1,10 @@ SUDOKU SOLVER - TODO - - Add Game->Settings (colors, homepath) - - Disable the built-in 'overwrite' warning in the Save As dialog, use the same warning for SAve and Save As. + - Add Game->Settings (colors, homepath) + - [SOLVED] Disable the built-in 'overwrite' warning in the Save As dialog, use the same warning for Save and Save As. + - [SOLVED] Have the Move->Validate option tell if the sudoku can be solved through constrains, full search, or not at all - [SOLVED] Fix homedir business on WIN32 (uses Application data) - [SOLVED] Add windows exe icon - [SOLVED] Add status bar diff --git a/src/main.cc b/src/main.cc index c7b1fff..d8fb3c5 100644 --- a/src/main.cc +++ b/src/main.cc @@ -7,12 +7,6 @@ int main(int argc, char **argv) { - // TODO initialize random seed - /* - unsigned int seed = THE_SEED; - srandom(seed); - */ - QApplication application(argc, argv); MainWindow mainwindow; diff --git a/src/solverwindow.cc b/src/solverwindow.cc index 0c2b082..410868c 100644 --- a/src/solverwindow.cc +++ b/src/solverwindow.cc @@ -125,39 +125,44 @@ void SolverWindow::doRevert() { if (!solverwindow_filename.isEmpty()) { QFileInfo fileinfo(solverwindow_filename); - if (QMessageBox::warning(this, tr("Revert?"), tr("Revert the state of the game to the previously saved file \"%1\"?").arg(fileinfo.fileName()), QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) { + if (QMessageBox::warning(this, tr("Revert?"), tr("Revert the game to the previously saved file \"%1\"?").arg(fileinfo.fileName()), QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) { openFromFile(solverwindow_filename); } } } +bool SolverWindow::confirmOverwrite(const QString & filename) +{ + QFile file (solverwindow_filename); + if (file.exists()) { + QFileInfo fileinfo(file); + return (QMessageBox::warning(this, tr("Overwrite file?"), tr("The file \"%1\" already exists.\nDo you wish to overwrite it?").arg(fileinfo.fileName()), QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes); + } else { + return true; + } +} + void SolverWindow::doSave() { if (solverwindow_filename.isEmpty()) { // no file name is set, use the Save As dialog doSaveAs(); - } else { - // confirm overwriting an existing file - QFile file (solverwindow_filename); - if (file.exists()) { - QFileInfo fileinfo(file); - if (QMessageBox::warning(this, tr("Overwrite file?"), tr("The file \"%1\" already exists. Do you wish to overwrite it?").arg(fileinfo.fileName()), QMessageBox::Yes | QMessageBox::No) != QMessageBox::Yes) { - return; - } - } + } else if (confirmOverwrite(solverwindow_filename)) { saveToFile(solverwindow_filename); - } } void SolverWindow::doSaveAs() { - // QFileDialog::getSaveFileName() warns about existing files - QString filename = QFileDialog::getSaveFileName(this, tr("Save as..."), globalSettings().homePath(), "Sudoku (*.sudoku)"); - - if (!filename.isEmpty()) { - saveToFile(filename); - } + QString filename; + do { + filename = QFileDialog::getSaveFileName(this, tr("Save as..."), globalSettings().homePath(), "Sudoku (*.sudoku)", 0, QFileDialog::DontConfirmOverwrite); + if (filename.isEmpty()) { + return; + } + } while (!confirmOverwrite(filename)); + + saveToFile(filename); } void SolverWindow::doNew() @@ -176,6 +181,11 @@ void SolverWindow::doStep() Sudoku sudoku; solverwindow_sudokuwidget->get_values(sudoku); + if (!sudoku.validate()) { + QMessageBox::warning(this, tr("Step"), tr("This sudoku is not valid!")); + return; + } + Sudoku solution(sudoku); int solved = solution.solve_rules(); if (solved == 0) { @@ -205,6 +215,11 @@ void SolverWindow::doGuess() Sudoku sudoku; solverwindow_sudokuwidget->get_values(sudoku); + if (!sudoku.validate()) { + QMessageBox::warning(this, tr("Guess"), tr("This sudoku is not valid!")); + return; + } + Sudoku solution(sudoku); int solved = solution.solve_search(); if (solved == 0) { @@ -231,31 +246,37 @@ void SolverWindow::doGuess() void SolverWindow::doSolve() { - // TODO detect invalid and solved states Sudoku sudoku; solverwindow_sudokuwidget->get_values(sudoku); - int solved = sudoku.solve_rules(); - sudoku.validate(); + bool is_valid = sudoku.validate(); + sudoku.solve_rules(); + bool is_solved = sudoku.solved(); solverwindow_sudokuwidget->set_values(sudoku); - if (solved == 0) { - QMessageBox::warning(this, tr("Solve rules"), tr("No more cells to solve!")); - return; + if (!is_valid) { + QMessageBox::warning(this, tr("Find solution"), tr("This sudoku is not valid!")); + } else if (is_solved) { + QMessageBox::information(this, tr("Find solution"), tr("This sudoku has been solved.")); + } else { + QMessageBox::warning(this, tr("Find solution"), tr("This sudoku can not be solved by applying the rules!")); } } void SolverWindow::doSearch() { - // TODO detect invalid and solved states Sudoku sudoku; solverwindow_sudokuwidget->get_values(sudoku); - int iterations = sudoku.solve_search(); - sudoku.validate(); - solverwindow_sudokuwidget->set_values(sudoku); + bool is_valid = sudoku.validate(); + sudoku.solve_search(); + bool is_solved = sudoku.solved(); + solverwindow_sudokuwidget->set_values(sudoku); - if (iterations == 0) { - QMessageBox::warning(this, tr("Find solution"), tr("No more cells to solve!")); - return; + if (!is_valid) { + QMessageBox::warning(this, tr("Find solution"), tr("This sudoku is not valid!")); + } else if (is_solved) { + QMessageBox::information(this, tr("Find solution"), tr("This sudoku has been solved.")); + } else { + QMessageBox::warning(this, tr("Find solution"), tr("This sudoku can not be solved!")); } } @@ -265,13 +286,35 @@ void SolverWindow::doValidate() solverwindow_sudokuwidget->get_values(sudoku); bool is_valid = sudoku.validate(); + solverwindow_sudokuwidget->set_values(sudoku); - if (is_valid) { - QMessageBox::information(this, tr("Validate"), tr("Sudoku is valid.")); - } else { - QMessageBox::warning(this, tr("Validate"), tr("Sudoku is not valid!")); - } + if (!is_valid) { + QMessageBox::warning(this, tr("Validate"), tr("This sudoku is not valid!")); + return; + } + + bool is_solved = sudoku.solved(); + if (is_solved) { + QMessageBox::information(this, tr("Validate"), tr("This sudoku has been solved.")); + return; + } + + sudoku.solve_rules(); + is_solved = sudoku.solved(); + if (is_solved) { + QMessageBox::information(this, tr("Validate"), tr("This sudoku can be solved by applying the rules.")); + return; + } + + sudoku.solve_search(); + is_solved = sudoku.solved(); + if (is_solved) { + QMessageBox::information(this, tr("Validate"), tr("This sudoku can be solved but needs guessing.")); + return; + } + + QMessageBox::warning(this, tr("Validate"), tr("This sudoku is valid but can not be solved!")); } void SolverWindow::step_constraints() diff --git a/src/solverwindow.h b/src/solverwindow.h index d279a1a..3a7daf1 100644 --- a/src/solverwindow.h +++ b/src/solverwindow.h @@ -57,6 +57,8 @@ private: void saveToFile(const QString & filename); void openFromFile(const QString & filename); + bool confirmOverwrite(const QString & filename); + SudokuWidget *solverwindow_sudokuwidget; QString solverwindow_filename; }; diff --git a/src/sudoku.h b/src/sudoku.h index 394f165..965de09 100644 --- a/src/sudoku.h +++ b/src/sudoku.h @@ -26,10 +26,19 @@ public: * */ void reset_cell(int pos_row, int pos_column); + /** + * @brief resolve sudoku constraint rules for a single cell + * */ int solve_constraints(int pos_row, int pos_column); + /** + * @brief resolve sudoku constraint rules + * */ int solve_constraints(); + /** + * @brief resolve sudoku coverage rules + * */ int solve_coverage(); /** @@ -38,7 +47,7 @@ public: int solve_rules(); /** - * @brief solve the sudoku, using exhaustive search + * @brief solve the sudoku, using full search * */ int solve_search(); -- cgit v1.2.3