From 451cbc4a94f85e8eb1d8923358a409f738d01c17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=B6rfler?= Date: Wed, 29 Aug 2007 13:24:31 +0000 Subject: [PATCH] * SudokuSolver::ComputeSolutions() can now be called more than once without doubling the solution list. * ComputeSolutions() will now check if solving the Sudoku is affordable for this algorithm (at least 1/6th of the fields must be known). This fixes one part of bug #1435. * SudokuView now checks if the Sudoku is already solved before trying to fill in a value from the solution (and then it did not find a free spot, surprisingly). This fixes the other part of bug #1435. * SudokuView now beeps if there was no solution. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@22110 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/apps/sudoku/SudokuSolver.cpp | 22 +++++++++++++++++++++- src/apps/sudoku/SudokuSolver.h | 2 ++ src/apps/sudoku/SudokuView.cpp | 12 ++++++++++-- 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/apps/sudoku/SudokuSolver.cpp b/src/apps/sudoku/SudokuSolver.cpp index f39c201fba..1f53bcc3a2 100644 --- a/src/apps/sudoku/SudokuSolver.cpp +++ b/src/apps/sudoku/SudokuSolver.cpp @@ -144,7 +144,13 @@ SudokuSolver::SudokuSolver() SudokuSolver::~SudokuSolver() { // we don't own the field but the solutions - + _MakeEmpty(); +} + + +void +SudokuSolver::_MakeEmpty() +{ for (uint32 i = 0; i < fSolutions.size(); i++) { delete fSolutions[i]; } @@ -161,6 +167,20 @@ SudokuSolver::SetTo(SudokuField* field) void SudokuSolver::ComputeSolutions() { + _MakeEmpty(); + + // We need to check if generating a solution is affordable with a + // brute force algorithm like this one + uint32 set = 0; + for (uint32 y = 0; y < fField->Size(); y++) { + for (uint32 x = 0; x < fField->Size(); x++) { + if (fField->ValueAt(x, y)) + set++; + } + } + if (set < fField->Size() * fField->Size() / 6) + return; + Stack stack; SolutionStep* step = new SolutionStep(fField); step->ToFirstUnset(); diff --git a/src/apps/sudoku/SudokuSolver.h b/src/apps/sudoku/SudokuSolver.h index 9c1fd59b94..7bfd4f53b0 100644 --- a/src/apps/sudoku/SudokuSolver.h +++ b/src/apps/sudoku/SudokuSolver.h @@ -26,6 +26,8 @@ public: SudokuField* SolutionAt(uint32 index); private: + void _MakeEmpty(); + typedef std::vector SudokuList; SudokuField* fField; diff --git a/src/apps/sudoku/SudokuView.cpp b/src/apps/sudoku/SudokuView.cpp index 53438ea970..61cf1fdd47 100644 --- a/src/apps/sudoku/SudokuView.cpp +++ b/src/apps/sudoku/SudokuView.cpp @@ -15,6 +15,7 @@ #include #include +#include #include #include @@ -617,12 +618,18 @@ SudokuView::MessageReceived(BMessage* message) if (solver.CountSolutions() > 0) { fField->SetTo(solver.SolutionAt(0)); Invalidate(); - } + } else + beep(); break; } case kMsgSolveSingle: { + if (fField->IsSolved()) { + beep(); + break; + } + SudokuSolver solver; solver.SetTo(fField); bigtime_t start = system_time(); @@ -640,7 +647,8 @@ SudokuView::MessageReceived(BMessage* message) fField->SetValueAt(x, y, solver.SolutionAt(0)->ValueAt(x, y)); _InvalidateField(x, y); - } + } else + beep(); break; }