From 47c14c73b3d5901345256178989c48f44a53d9da Mon Sep 17 00:00:00 2001 From: Miguel Fernandez Date: Thu, 19 May 2016 14:07:35 +0000 Subject: [PATCH] Add cell options, generate seed for random --- build/configure.ac | 2 +- build/src_neaten.sh | 2 +- src/cpp/main.cpp | 31 ++++++++++++++++++++++++++++++- src/cpp/qqwing.cpp | 12 ++++++++++-- src/cpp/qqwing.hpp | 7 +++++++ test/app/help.sh | 1 + 6 files changed, 50 insertions(+), 5 deletions(-) diff --git a/build/configure.ac b/build/configure.ac index 41aba80..0494e42 100755 --- a/build/configure.ac +++ b/build/configure.ac @@ -1,5 +1,5 @@ AC_PREREQ(2.59) -AC_INIT(qqwing, 1.3.4, http://qqwing.com/) +AC_INIT(qqwing, 1.3.6, http://qqwing.com/) AC_CONFIG_MACRO_DIR([m4]) AC_CONFIG_SRCDIR([config.h.in]) AC_CONFIG_HEADER([config.h]) diff --git a/build/src_neaten.sh b/build/src_neaten.sh index 805189e..1bbf318 100755 --- a/build/src_neaten.sh +++ b/build/src_neaten.sh @@ -26,6 +26,6 @@ then touch --date 1980-01-01 $neaten_time_file fi -find build/ doc/ src/ test/ Makefile -type f -newer $neaten_time_file | grep -vEi 'changelog|copyright|COPYING|README|\.gif|\.png|\.jpg$' | xargs ./build/src_neaten.pl +find build/ doc/ src/ test/ Makefile -type f -newer $neaten_time_file | grep -vEi 'changelog|copyright|COPYING|README|\.gif|\.png|\.jpg|\.gch$' | xargs ./build/src_neaten.pl touch $neaten_time_file diff --git a/src/cpp/main.cpp b/src/cpp/main.cpp index 13a4355..a146c5a 100644 --- a/src/cpp/main.cpp +++ b/src/cpp/main.cpp @@ -42,6 +42,7 @@ bool readPuzzleFromStdIn(int* puzzle); void printHelp(); void printVersion(); void printAbout(); +unsigned long generateSeed(unsigned long a, unsigned long b, unsigned long c); /** * Main method -- the entry point into the program. @@ -69,6 +70,7 @@ int main(int argc, char *argv[]){ SudokuBoard::PrintStyle printStyle = SudokuBoard::READABLE; int numberToGenerate = 1; bool printStats = false; + int givenCells = 0; SudokuBoard::Difficulty difficulty = SudokuBoard::UNKNOWN; SudokuBoard::Symmetry symmetry = SudokuBoard::NONE; @@ -155,6 +157,17 @@ int main(int argc, char *argv[]){ return 1; } i++; + } else if (!strcmp(argv[i],"--given-cells")){ + if (argc <= i+1){ + cout << "Please specify a number of given cells between 0 and " << BOARD_SIZE << "." << endl; + return 1; + } + givenCells = atoi(argv[i+1]); + if (givenCells < 0 || givenCells > BOARD_SIZE){ + cout << "number of given cells expected to be in range [0-" << BOARD_SIZE << "]. receive: " << givenCells << endl; + return 1; + } + i++; } else if (!strcmp(argv[i],"--solve")){ action = SOLVE; printSolution = true; @@ -201,7 +214,7 @@ int main(int argc, char *argv[]){ } // Initialize the random number generator - srand ( unsigned ( time(0) ) ); + srand (generateSeed(clock(), time(NULL), getpid())); // If printing out CSV, print a header if (printStyle == SudokuBoard::CSV){ @@ -221,6 +234,7 @@ int main(int argc, char *argv[]){ ss->setRecordHistory(printHistory || printInstructions || printStats || difficulty!=SudokuBoard::UNKNOWN); ss->setLogHistory(logHistory); ss->setPrintStyle(printStyle); + ss->setNbGivenCells(givenCells); // Solve puzzle or generate puzzles // until end of input for solving, or @@ -451,6 +465,7 @@ void printHelp(){ cout << " --solve Solve all the puzzles from standard input" << endl; cout << " --difficulty Generate only simple, easy, intermediate, expert, or any" << endl; cout << " --symmetry Symmetry: none, rotate90, rotate180, mirror, flip, or random" << endl; + cout << " --given-cells Number of given cells according to sudoku games." << endl; cout << " --puzzle Print the puzzle (default when generating)" << endl; cout << " --nopuzzle Do not print the puzzle (default when solving)" << endl; cout << " --solution Print the solution (default when solving)" << endl; @@ -514,3 +529,17 @@ long getMicroseconds(){ return 0; #endif } + +// http://burtleburtle.net/bob/hash/doobs.html +unsigned long generateSeed(unsigned long a, unsigned long b, unsigned long c){ + a=a-b; a=a-c; a=a^(c >> 13); + b=b-c; b=b-a; b=b^(a << 8); + c=c-a; c=c-b; c=c^(b >> 13); + a=a-b; a=a-c; a=a^(c >> 12); + b=b-c; b=b-a; b=b^(a << 16); + c=c-a; c=c-b; c=c^(b >> 5); + a=a-b; a=a-c; a=a^(c >> 3); + b=b-c; b=b-a; b=b^(a << 10); + c=c-a; c=c-b; c=c^(b >> 15); + return c; +} diff --git a/src/cpp/qqwing.cpp b/src/cpp/qqwing.cpp index ed18ff0..d41cf86 100644 --- a/src/cpp/qqwing.cpp +++ b/src/cpp/qqwing.cpp @@ -120,7 +120,8 @@ namespace qqwing { solveHistory ( new vector() ), solveInstructions ( new vector() ), printStyle ( READABLE ), - lastSolveRound (0) + lastSolveRound (0), + nbGivenCells (0) { {for (int i=0; i 0){ diff --git a/src/cpp/qqwing.hpp b/src/cpp/qqwing.hpp index 41062a4..75ca5e6 100644 --- a/src/cpp/qqwing.hpp +++ b/src/cpp/qqwing.hpp @@ -103,6 +103,7 @@ void setRecordHistory(bool recHistory); void setLogHistory(bool logHist); void setPrintStyle(PrintStyle ps); + void setNbGivenCells(int nbGivenCells); bool generatePuzzle(); bool generatePuzzleSymmetry(SudokuBoard::Symmetry symmetry); int getGivenCount(); @@ -204,6 +205,12 @@ * The last round of solving */ int lastSolveRound; + + /** + * expected number of given cells + */ + int nbGivenCells; + bool reset(); bool singleSolveMove(int round); bool onlyPossibilityForCell(int round); diff --git a/test/app/help.sh b/test/app/help.sh index e36f28b..0417da6 100755 --- a/test/app/help.sh +++ b/test/app/help.sh @@ -26,6 +26,7 @@ Sudoku solver and generator. --solve Solve all the puzzles from standard input --difficulty Generate only simple, easy, intermediate, expert, or any --symmetry Symmetry: none, rotate90, rotate180, mirror, flip, or random + --given-cells Number of given cells according to sudoku games. --puzzle Print the puzzle (default when generating) --nopuzzle Do not print the puzzle (default when solving) --solution Print the solution (default when solving)