Logo Search packages:      
Sourcecode: vdr-plugin-sudoku version File versions  Download package

Generator::Generator ( Puzzle puzzle,
unsigned int  givens_count,
bool  symmetric = true,
unsigned int  max_iter = 0 
)

Constructor

Definition at line 23 of file generator.cpp.

References BackTrack::Algorithm::find_next_solution(), Sudoku::Puzzle::reset(), Sudoku::SDIM, and BackTrack::Algorithm::solution_is_valid().

                                                            :
  Algorithm(*this, max_iter), puzzle(puzzle), symmetric(symmetric)
{
  assert(givens_count <= SDIM);

  // Search a random Sudoku solution.
  for (bool found = false; !found;)
  {
    sudoku.reset();
    Solver solver(sudoku, true);
    solver.find_next_solution();
    found = solver.solution_is_valid();
  }

  // If symmetric pos_list contains only the first halve of all positions.
  pos_count = SDIM;
  free_count = SDIM - givens_count;
  free_center = symmetric && pos_count % 2 != 0 && free_count % 2 != 0;
  if (symmetric)
    pos_count /= 2, free_count /= 2;

  // Fill pos_list with positions in random order.
  bool list[pos_count];
  unsigned int p, i, c;
  for (p = 0; p < pos_count; ++p)
    list[p] = true;
  for (i = 0; i < pos_count; ++i)
  {
    c = rand(pos_count - i) + 1;
    for (p = 0; p < pos_count; ++p)
      if (list[p])
        if (--c == 0)
          break;
    assert(p < pos_count);
    list[p] = false;
    pos_list[i] = p;
  }
}


Generated by  Doxygen 1.6.0   Back to index