Ejemplo n.º 1
0
void process_deriviate_solutions(
	struct Board *board,
	struct Num_Coordinates *solution,
	int *points,
	int *iterations)
{
	int i, j, best_i, best_j;
	int best_change, current_change;

	do
	{
		(*iterations)++;

		best_i = NONE;
		best_j = NONE;

		i = best_change = 0;

		while (i < solution->count)
		{
			i = find_number_to_swap(board, solution, i);

			j = i + 1;

			while (j < solution->count)
			{
				j = find_number_to_swap(board, solution, j);

				if (j < solution->count)
				{
					current_change = assess_deriviate(solution, points, i, j);

					if (current_change > best_change)
					{
						best_i = i;
						best_j = j;
						best_change = current_change;
					}
				}

				j++;
			}

			i++;
		}

		if (best_change > 0)
		{
			swap_numbers(solution, best_i, best_j);
			update_points(solution, points, best_i - 1, best_i + 1);
			update_points(solution, points, best_j - 1, best_j + 1);
		}

	} while (best_change > 0);
} 
Ejemplo n.º 2
0
/* randomly swap out values, by num_itr,
 * number of iterations, of known solved puzzle;
 * should result in a new (completed) random puzzle
 */
static int build_puzzle(int* puzzle, int* matrix)
{
    int num_itr = 0, x = 0, y = 0;
    puts("generating sudoku puzzle ...");

	/* There is a stdlib call "rand"; this function is tuned primarily for speed
	 * and distribution, not for unpredictability. Almost all built-in random 
	 * functions for various languages and frameworks use this function by 
	 * default. There are also "cryptographic" random number generators that are
	 * much less predictable, but run much slower. These should be used in any sort
	 * of security-related application. - tylerl
	 * http://stackoverflow.com/questions/822323/how-to-generate-a-random-number-in-c
	 */

#ifdef JSW_RANDOM
    puts("using jsw_srand()");
    jsw_seed(time(NULL));
    num_itr = (jsw_rand() % 1000000);
#else
    puts("using srand()");
    srand(time(NULL));
    num_itr = (rand() % 1000000);
#endif
    
    printf("number iterations: %d\n", num_itr);

    for (int i = 0; i<num_itr; i++) 
    {
#ifdef JSW_RANDOM
        x = (jsw_rand() % 9);
        y = (jsw_rand() % 9);
#else
        x = (rand() % 9);
        y = (rand() % 9);
#endif
        x++;
        y++;
        /* printf("x = %d, y = %d\n", x, y); */
        swap_numbers(puzzle, x, y);
    }

    print_grid(puzzle_solution);
    remove_values(matrix);
    return 0;
}