int solve_rec(struct shape *board, const struct puzzle *p, int current_tile) { int x, y; const struct shape *current_shape; if (current_tile == p->no_tiles) { /* * Found solution */ printf("Solution:\n"); dump_board(board, 0); dump_board_svg(board); printf("\n"); return (0); } current_shape = &p->tiles[current_tile]; for (y = 0; y < board->height - current_shape->height + 1; y++) { for (x = 0; x < board->width - current_shape->width + 1; x++) { if (can_insert_tile(board, &p->tiles[current_tile], x, y)) { insert_tile(board, current_tile + 1, &p->tiles[current_tile], x, y); solve_rec(board, p, current_tile + 1); remove_tile(board, current_tile + 1); } } } return (0); }
/** * Tries to solve the puzzle * * Returns true if the puzzle was fully solved */ int solve(void) { int nr = 1; /* loop as long as there is some progress */ while (nr) { nr = 0; debug("Scanning for singles..."); if ((nr = scan_singles())) { /* print_board(); */ continue; } debug("Scanning boxes for singles and pointing pairs/triples..."); if ((nr = scan_boxes())) { /* print_board(); */ continue; } debug("Scanning for singles on rows and cols..."); if ((nr = scan_singles_rowcol())) { /* print_board(); */ continue; } debug("Scanning for naked pairs..."); if ((nr = scan_all_groups(&scan_naked_pairs_group, "naked pairs"))) { /* print_board(); */ continue; } debug("Scanning for hidden pairs..."); if ((nr = scan_all_groups(&scan_hidden_pairs_group, "hidden pairs"))) { /*print_board(); */ continue; } debug("Doing box/line reduction..."); if ((nr = scan_rows_cols(&scan_box_line_group, "box/line"))) { /*print_board(); */ continue; } } print_board(); if ((nr = check_unsolved()) == 0) info("Sudoku solved!"); else info("Sudoku not solved, %d numbers left =(", nr); dump_board(stdout); return (nr == 0); }
/****************************************************************************** my_error_handler (my_display, my_error) Handle those pesky errors which used to completely destroy the game. Now they cause the game to dump the current board state, then exit gracefully. ******************************************************************************/ int my_error_handler (Display *my_display, XErrorEvent *my_error) { char message[MAX_LINE]; /** Grab the system error message and print it **/ XGetErrorText (my_display, my_error->error_code, message, MAX_LINE); throw_message ("ERROR: ", "Unrecoverable, code %s", message); /** Dump the board and exit **/ fprintf (stderr, " dumping board to %s\n", Config->file_store_map); dump_board (Config->file_store_map, FALSE); #if !USE_FATAL_RECOVER exit (0); #else return 0; #endif }
/****************************************************************************** my_io_error_handler (my_display) Attempt to recover from an error caused by someone remotely executing one of the xwindows. Tries to find the window that has been eliminated, and removes that player from the game. Failing that, dumps the board. ******************************************************************************/ int my_io_error_handler (Display *my_display) { int player, bad_player; static int iteration=0; /** If this routine gets called many times, something is seriously wrong **/ if (iteration < 20) iteration++; else { /** fprintf (stderr, "EXTRA FATAL ERROR: display %s\n", XDisplayName (my_display)); **/ throw_error ("Lost X-Window connection", NULL); } /** Match the error window with a particular player **/ bad_player = -1; for (player=0; player<Config->player_count; player++) if (XWindow[player]->display == my_display) bad_player = player; /** Print error message and dump board **/ fprintf (stderr, "FATAL ERROR: player %d\n", bad_player); fprintf (stderr, " dumping board to %s\n", Config->file_store_map); dump_board (Config->file_store_map, FALSE); /** Continue only if error window found, disabling that player **/ if (bad_player < 0) throw_error ("Cannot find resolve display death", NULL); else XWindow[bad_player]->open = FALSE; /** Return, in whatever manner is selected, to the main program **/ #if USE_LONGJMP longjmp (saved_environment, 1); return 0; #else # if USE_PROCEDURE run_unix_loop (); # endif # if !USE_FATAL_RECOVER exit (1); # else return 0; # endif #endif }