File indexing completed on 2024-04-28 04:04:44

0001 /****************************************************************************
0002  *    Copyright 2015  Ian Wadham <iandw.au@gmail.com>                       *
0003  *                                                                          *
0004  *    This program is free software; you can redistribute it and/or         *
0005  *    modify it under the terms of the GNU General Public License as        *
0006  *    published by the Free Software Foundation; either version 2 of        *
0007  *    the License, or (at your option) any later version.                   *
0008  *                                                                          *
0009  *    This program is distributed in the hope that it will be useful,       *
0010  *    but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0011  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0012  *    GNU General Public License for more details.                          *
0013  *                                                                          *
0014  *    You should have received a copy of the GNU General Public License     *
0015  *    along with this program.  If not, see <http://www.gnu.org/licenses/>. *
0016  ****************************************************************************/
0017 
0018 #include "mathdokugenerator.h"
0019 #include "ksudoku_logging.h"
0020 #include "skgraph.h"
0021 #include "cagegenerator.h"
0022 
0023 
0024 MathdokuGenerator::MathdokuGenerator (SKGraph * graph)
0025     :
0026     mGraph       (graph)
0027 {
0028 }
0029 
0030 bool MathdokuGenerator::generateMathdokuTypes (BoardContents & puzzle,
0031                                                BoardContents & solution,
0032                                                QList<int> * solutionMoves,
0033                                                Difficulty difficultyRequired)
0034 {
0035     // Cage sizes must be no more than the number of cells in a column or row.
0036     int  maxSize   = qMin ((2 + difficultyRequired), mGraph->order());
0037     int  maxVal    = 1000;
0038     bool hideOps   = false;
0039     // int  maxCombos = 120;
0040     int  maxCombos = 2000;
0041 
0042     int  maxTries  = 20;
0043 
0044     CageGenerator cageGen (solution);
0045 
0046     int  numTries = 0;
0047     int  numMultis = 0;
0048     int  n = 0;
0049     while ((n <= 0) && (numTries < maxTries)) {
0050     numTries++;
0051     n = cageGen.makeCages (mGraph, solutionMoves,
0052                                maxSize, maxVal, hideOps, maxCombos);
0053     if (n < 0) {
0054         numMultis++;
0055     }
0056     }
0057     if (numTries >= maxTries) {
0058     qCDebug(KSudokuLog) << "makeCages() FAILED after" << numTries << "tries"
0059              << numMultis << "multi-solutions";
0060         return false;       // Try another set of Sudoku cell-values.
0061     }
0062 
0063     qCDebug(KSudokuLog) << "makeCages() required" << numTries << "tries"
0064              << numMultis << "multi-solutions";;
0065     puzzle = mGraph->emptyBoard();
0066     for (int n = 0; n < mGraph->cageCount(); n++) {
0067          if (mGraph->cage(n).count() == 1) {
0068              int index = mGraph->cage(n).at(0);
0069              puzzle[index] = solution.at(index);
0070          }
0071     }
0072     return true;
0073 }
0074 
0075 int MathdokuGenerator::solveMathdokuTypes (BoardContents & solution,
0076                                            QList<int> * solutionMoves)
0077 {
0078     bool hideOps = false;
0079     int result   = 0;
0080     CageGenerator cageGen (solution);
0081     result = cageGen.checkPuzzle (mGraph, solution, solutionMoves, hideOps);
0082     return result;
0083 }
0084