diff --git a/src/apps/sudoku/SudokuView.cpp b/src/apps/sudoku/SudokuView.cpp index d79eafed7a..e46adcad89 100644 --- a/src/apps/sudoku/SudokuView.cpp +++ b/src/apps/sudoku/SudokuView.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2007-2012, Axel Dörfler, axeld@pinc-software.de. + * Copyright 2007-2014, Axel Dörfler, axeld@pinc-software.de. * Distributed under the terms of the MIT License. */ @@ -1101,53 +1101,17 @@ SudokuView::MessageReceived(BMessage* message) Redo(); break; - case kMsgSolveSudoku: - { - SudokuSolver solver; - solver.SetTo(fField); - bigtime_t start = system_time(); - solver.ComputeSolutions(); - printf("found %" B_PRIu32 " solutions in %g msecs\n", - solver.CountSolutions(), (system_time() - start) / 1000.0); - if (solver.CountSolutions() > 0) { - _PushUndo(); - fField->SetTo(solver.SolutionAt(0)); - Invalidate(); - } else - beep(); + case kMsgSetAllHints: + _SetAllHints(); + break; + + case kMsgSolveSudoku: + _Solve(); break; - } case kMsgSolveSingle: - { - if (fField->IsSolved()) { - beep(); - break; - } - - SudokuSolver solver; - solver.SetTo(fField); - bigtime_t start = system_time(); - solver.ComputeSolutions(); - printf("found %" B_PRIu32 " solutions in %g msecs\n", - solver.CountSolutions(), (system_time() - start) / 1000.0); - if (solver.CountSolutions() > 0) { - _PushUndo(); - - // find free spot - uint32 x, y; - do { - x = rand() % fField->Size(); - y = rand() % fField->Size(); - } while (fField->ValueAt(x, y)); - - fField->SetValueAt(x, y, - solver.SolutionAt(0)->ValueAt(x, y)); - _InvalidateField(x, y); - } else - beep(); + _SolveSingle(); break; - } default: BView::MessageReceived(message); @@ -1320,3 +1284,70 @@ SudokuView::Draw(BRect /*updateRect*/) } +void +SudokuView::_SetAllHints() +{ + uint32 size = fField->Size(); + + for (uint32 y = 0; y < size; y++) { + for (uint32 x = 0; x < size; x++) { + uint32 validMask = fField->ValidMaskAt(x, y); + fField->SetHintMaskAt(x, y, validMask); + } + } + Invalidate(); +} + + +void +SudokuView::_Solve() +{ + SudokuSolver solver; + if (_GetSolutions(solver)) { + _PushUndo(); + fField->SetTo(solver.SolutionAt(0)); + Invalidate(); + } else + beep(); +} + + +void +SudokuView::_SolveSingle() +{ + if (fField->IsSolved()) { + beep(); + return; + } + + SudokuSolver solver; + if (_GetSolutions(solver)) { + _PushUndo(); + + // find free spot + uint32 x, y; + do { + x = rand() % fField->Size(); + y = rand() % fField->Size(); + } while (fField->ValueAt(x, y)); + + fField->SetValueAt(x, y, + solver.SolutionAt(0)->ValueAt(x, y)); + _InvalidateField(x, y); + } else + beep(); +} + + +bool +SudokuView::_GetSolutions(SudokuSolver& solver) +{ + solver.SetTo(fField); + bigtime_t start = system_time(); + solver.ComputeSolutions(); + + printf("found %" B_PRIu32 " solutions in %g msecs\n", + solver.CountSolutions(), (system_time() - start) / 1000.0); + + return solver.CountSolutions() > 0; +} diff --git a/src/apps/sudoku/SudokuView.h b/src/apps/sudoku/SudokuView.h index b146c0f885..beb6de94de 100644 --- a/src/apps/sudoku/SudokuView.h +++ b/src/apps/sudoku/SudokuView.h @@ -1,5 +1,5 @@ /* - * Copyright 2007-2012, Axel Dörfler, axeld@pinc-software.de. + * Copyright 2007-2014, Axel Dörfler, axeld@pinc-software.de. * Distributed under the terms of the MIT License. */ #ifndef SUDOKU_VIEW_H @@ -12,6 +12,7 @@ class BDataIO; class SudokuField; +class SudokuSolver; struct entry_ref; @@ -113,6 +114,10 @@ private: void _UndoRedo(BObjectList& undos, BObjectList& redos); void _PushUndo(); + void _SetAllHints(); + void _Solve(); + void _SolveSingle(); + bool _GetSolutions(SudokuSolver& solver); private: rgb_color fBackgroundColor; @@ -146,6 +151,7 @@ private: static const uint32 kMsgSudokuSolved = 'susl'; static const uint32 kMsgSolveSudoku = 'slvs'; static const uint32 kMsgSolveSingle = 'slsg'; +static const uint32 kMsgSetAllHints = 'salh'; // you can observe these: static const int32 kUndoRedoChanged = 'unre'; diff --git a/src/apps/sudoku/SudokuWindow.cpp b/src/apps/sudoku/SudokuWindow.cpp index 135568b12a..71d6b514a7 100644 --- a/src/apps/sudoku/SudokuWindow.cpp +++ b/src/apps/sudoku/SudokuWindow.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2007-2011, Axel Dörfler, axeld@pinc-software.de. + * Copyright 2007-2014, Axel Dörfler, axeld@pinc-software.de. * Distributed under the terms of the MIT License. */ @@ -276,6 +276,10 @@ SudokuWindow::SudokuWindow() fRestoreStateItem->SetEnabled(fStoredState != NULL); menu->AddSeparatorItem(); + menu->AddItem(new BMenuItem(B_TRANSLATE("Set all hints"), + new BMessage(kMsgSetAllHints))); + menu->AddSeparatorItem(); + menu->AddItem(new BMenuItem(B_TRANSLATE("Solve"), new BMessage(kMsgSolveSudoku))); menu->AddItem(new BMenuItem(B_TRANSLATE("Solve single field"),