diff options
Diffstat (limited to 'tools/sudoku_generator.cpp')
-rw-r--r-- | tools/sudoku_generator.cpp | 63 |
1 files changed, 42 insertions, 21 deletions
diff --git a/tools/sudoku_generator.cpp b/tools/sudoku_generator.cpp index fdd3e30..8b563f5 100644 --- a/tools/sudoku_generator.cpp +++ b/tools/sudoku_generator.cpp @@ -17,7 +17,7 @@ * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * - * $Id: sudoku_generator.cpp 117 2008-03-21 17:57:50Z tom $ + * $Id: sudoku_generator.cpp 161 2008-11-23 00:18:02Z tom $ */ #include "../puzzle.h" @@ -30,15 +30,24 @@ using namespace Sudoku; -void print_copyleft(unsigned int givens_count) +int print_version() +{ + printf("sudoku_generator %s\n" + "Copyright (C) 2005-2008, Thomas Günther <tom@toms-cafe.de>\n" + "This GPL program comes with ABSOLUTELY NO WARRANTY;\n" + "this is free software, and you are welcome to redistribute it\n" + "under certain conditions; see the source for details.\n", VERSION); + return 0; +} + +void print_description(unsigned int givens_count) { printf("Sudoku with %d givens generated by sudoku_generator %s\n" - " Copyright (C) 2005, Thomas Günther <tom@toms-cafe.de>\n" - " This puzzle can be used without any limitations.\n" + " This puzzle can be used without any limitations.\n" "\n", givens_count, VERSION); } -void print_usage() +int print_usage() { printf("Usage: sudoku_generator [-n|--non-sym] [-d|--dump] [<givens_count>]\n" " Generate a Sudoku puzzle.\n" @@ -60,9 +69,13 @@ void print_usage() " Perform some test procedures.\n" "\n" #endif + " sudoku_generator -v|--version\n" + " Print version information and exit.\n" + "\n" " sudoku_generator -h|--help\n" - " Print this help.\n" + " Print this help message and exit.\n" "\n"); + return 2; } void print_sudoku(const Numbers* sudoku_list[], unsigned int count, @@ -108,7 +121,7 @@ void print_sudoku(const Numbers* sudoku_list[], unsigned int count, } printf("\n"); if (givens_count != 0) - print_copyleft(givens_count); + print_description(givens_count); } void print_sudoku(const Numbers& sudoku, unsigned int givens_count = 0) @@ -305,19 +318,21 @@ int main(int argc, char* argv[]) #ifdef WITH_TEST { "test", no_argument, NULL, 't' }, #endif + { "version", no_argument, NULL, 'v' }, { "help", no_argument, NULL, 'h' }, { NULL } }; #ifdef WITH_TEST - static const char* options = "ndspth"; + static const char* options = "ndsptvh"; #else - static const char* options = "ndsph"; + static const char* options = "ndspvh"; #endif bool non_sym = false; bool dump = false; bool solve = false; bool print = false; bool test = false; + bool version = false; bool help = false; bool error = false; int c; @@ -332,33 +347,39 @@ int main(int argc, char* argv[]) #ifdef WITH_TEST case 't': test = true; break; #endif + case 'v': version = true; break; case 'h': help = true; break; default: error = true; } } int arg_count = argc - optind; + bool generate = non_sym || dump || + (arg_count == 0 && !test && !version && !help); unsigned int givens_count = 36; - if ((arg_count == 0 || - (arg_count == 1 && sscanf(argv[optind], "%u", &givens_count) == 1)) && - givens_count > 0 && givens_count <= SDIM && - !solve && !print && !test && !help && !error) + if (arg_count == 1 && sscanf(argv[optind], "%u", &givens_count) == 1) + generate = true; + + if ((generate ? 1 : 0) + (solve ? 1 : 0) + (print ? 1 : 0) + (test ? 1 : 0) + + (version ? 1 : 0) + (help ? 1 : 0) > 1 || error) + return print_usage(); + + if (generate && 0 < givens_count && givens_count <= SDIM) return generate_puzzle(givens_count, non_sym, dump); - if (solve && arg_count == 1 && strlen(argv[optind]) >= SDIM && - !non_sym && !dump && !test && !help && !error) + if (solve && arg_count == 1 && strlen(argv[optind]) >= SDIM) return solve_puzzle(argv[optind]); - if (print && arg_count == 1 && strlen(argv[optind]) >= SDIM && - !non_sym && !dump && !test && !help && !error) + if (print && arg_count == 1 && strlen(argv[optind]) >= SDIM) return print_puzzle(argv[optind]); #ifdef WITH_TEST - if (test && arg_count == 0 && - !non_sym && !dump && !print && !help && !error) + if (test) return test_sudoku(); #endif - print_usage(); - return 2; + if (version) + return print_version(); + + return print_usage(); } |