generator/sudokuGen.cpp
// START: Custom Initialising with grid passed as argument
Custom Initialising with grid passed as argument
Custom Initialising with grid passed as argument
Sudoku::Sudoku(string grid_str, bool row_major)
{
  if(grid_str.length() != 81)
  {
    grid_status=false;
    return;
  }
  for(int i=0; i<81; ++i)
  {
    int curr_num = grid_str[i]-'0';
    if(!((curr_num == UNASSIGNED) || (curr_num > 0 && curr_num < 10)))
    {
      grid_status=false;
      return;
    }
    if(row_major) grid[i/9][i%9] = curr_num;
    else          grid[i%9][i/9] = curr_num;
  }
  for (int col_num=0; col_num<9; ++col_num)
  {
    bool nums[10]={false};
    for (int row_num=0; row_num<9; ++row_num)
    {
      int curr_num = grid[row_num][col_num];
      if(curr_num!=UNASSIGNED && nums[curr_num]==true)
      {
        grid_status=false;
        return;
      }
      nums[curr_num] = true;
    }
  }
  for (int row_num=0; row_num<9; ++row_num)
  {
    bool nums[10]={false};
    for (int col_num=0; col_num<9; ++col_num)
    {
      int curr_num = grid[row_num][col_num];
      if(curr_num!=UNASSIGNED && nums[curr_num]==true)
      {
        grid_status=false;
        return;
      }
      nums[curr_num] = true;
    }
  }
  for (int block_num=0; block_num<9; ++block_num)
  {
    bool nums[10]={false};
    for (int cell_num=0; cell_num<9; ++cell_num)
    {
      int curr_num = grid[((int)(block_num/3))*3 + (cell_num/3)][((int)(block_num%3))*3 + (cell_num%3)];
      if(curr_num!=UNASSIGNED && nums[curr_num]==true)
      {
        grid_status=false;
        return;
      }
      nums[curr_num] = true;
    }
  }
  for(int i=0;i<9;i++)
  {
    this->guessNum[i]=i+1;
  }
  random_shuffle(this->guessNum, (this->guessNum) + 9, genRandNum);
  grid_status = true;
}
Sudoku :: Sudoku ( string grid_str , bool row_major ) { if ( grid_str . length ( ) != 81 ) { grid_status = false ; return ; } for ( int i = 0 ; i < 81 ; ++ i ) { int curr_num = grid_str [ i ] - ' ' ; if ( ! ( ( curr_num == UNASSIGNED ) || ( curr_num > 0 && curr_num < 10 ) ) ) { grid_status = false ; return ; } if ( row_major ) grid [ i / 9 ] [ i % 9 ] = curr_num ; else grid [ i % 9 ] [ i / 9 ] = curr_num ; } for ( int col_num = 0 ; col_num < 9 ; ++ col_num ) { bool nums [ 10 ] = { false } ; for ( int row_num = 0 ; row_num < 9 ; ++ row_num ) { int curr_num = grid [ row_num ] [ col_num ] ; if ( curr_num != UNASSIGNED && nums [ curr_num ] == true ) { grid_status = false ; return ; } nums [ curr_num ] = true ; } } for ( int row_num = 0 ; row_num < 9 ; ++ row_num ) { bool nums [ 10 ] = { false } ; for ( int col_num = 0 ; col_num < 9 ; ++ col_num ) { int curr_num = grid [ row_num ] [ col_num ] ; if ( curr_num != UNASSIGNED && nums [ curr_num ] == true ) { grid_status = false ; return ; } nums [ curr_num ] = true ; } } for ( int block_num = 0 ; block_num < 9 ; ++ block_num ) { bool nums [ 10 ] = { false } ; for ( int cell_num = 0 ; cell_num < 9 ; ++ cell_num ) { int curr_num = grid [ ( ( int ) ( block_num / 3 ) ) * 3 + ( cell_num / 3 ) ] [ ( ( int ) ( block_num % 3 ) ) * 3 + ( cell_num % 3 ) ] ; if ( curr_num != UNASSIGNED && nums [ curr_num ] == true ) { grid_status = false ; return ; } nums [ curr_num ] = true ; } } for ( int i = 0 ; i < 9 ; i ++ ) { this -> guessNum [ i ] = i + 1 ; } random_shuffle ( this -> guessNum , ( this -> guessNum ) + 9 , genRandNum ) ; grid_status = true ; }
START: Custom Initialising with grid passed as argument
START : Custom Initialising with grid passed as argument
// First pass: Check if all cells are valid // Second pass: Check if all columns are valid // Third pass: Check if all rows are valid // Fourth pass: Check if all blocks are valid // Randomly shuffling the guessing number array