int main(int argc, char **argv) { extern char *optarg; extern int optind, optopt; char *fname = NULL; char step = 0; int c; init(); /* check args * -f: read sudokus from file (- for stdin) */ while ((c = getopt(argc, argv, "f:vsh")) != EOF) { switch (c) { case 'f': fname = (char *) malloc(strlen(optarg)+1); if (fname == NULL) eprintf("malloc() failed:"); strncpy(fname, optarg, strlen(optarg)); fname[strlen(optarg)] = '\0'; break; case 'v': set_loglevel(get_loglevel() + 1); break; case 'h': usage(); exit(EXIT_SUCCESS); case 's': step = 1; break; default: break; } } /* if (step && verbose == 0) ++verbose; */ if (fname != NULL) { FILE *file; char *line; int bufsize = X*Y + 2; if (strcmp(fname, "-") == 0) file = stdin; else { file = fopen(fname, "r"); if (file == NULL) eprintf("Couldn't open file '%s':", fname); } line = (char *) malloc(bufsize); if (NULL == line) eprintf("malloc() failed:"); while (fgets(line, bufsize, file) != NULL) { if (line[0] == '#') continue; init(); parse_board(line); mode = MODE_NORMAL; if (step) mode = MODE_STEP; printf(line); solve(); } if (fileno(file) != STDIN_FILENO) fclose(file); free(line); free(fname); } else { /* try if there was a puzzle as argument */ if (argc > optind) { printf("Initializing the board ...\n"); if (parse_board(argv[optind]) != X*Y) { eprintf("Error parsing the puzzle"); } print_board(); mode = MODE_NORMAL; if (step) mode = MODE_STEP; solve(); } else { wprintf("Error: no puzzle(s) given"); usage(); exit(EXIT_FAILURE); } } exit(EXIT_SUCCESS); }
int main() { state_t *s = parse_board( #define BIG 0 #if BIG == 0 8, 7, "#######" "# #" "# #" "#. # #" "#. $$ #" "#.$$ #" "#.# @#" "#######" #elif BIG == 1 5, 13, "#############" "# # #" "# $$$$$$$ @#" "#....... #" "#############" #elif BIG == 2 5, 13, "#############" "#... # #" "#.$$$$$$$ @#" "#... #" "#############" #else 11, 19, " ##### " " # # " " # # " " ### #$## " " # # " "### #$## # ######" "# # ## ##### .#" "# $ $ ..#" "##### ### #@## .#" " # #########" " ####### " #endif ); show_board(s); extend_table(); queue_move(s); for (int i = 0; !done; i++) { printf("depth %d\r", i); fflush(stdout); state_t *head = next_level; for (next_level = NULL; head && !done; head = head->qnext) do_move(head); if (!next_level) { puts("no solution?"); return 1; } } printf("press any key to see moves\n"); getchar(), puts("\033[H\033[J"); show_moves(done); #if 0 free(buckets); free(board); free(goals); free(live); while (block_root) { void *tmp = block_root->next; free(block_root); block_root = tmp; } #endif return 0; }