Advertisement
Guest User

mysudokugenerator.exe

a guest
Jul 31st, 2016
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.52 KB | None | 0 0
  1.  #include <iostream>
  2.  #include <array>
  3.  #include <algorithm>
  4.  #include <random>
  5.  #include <bitset>
  6.  
  7.  typedef unsigned int uint;
  8.  
  9.  class Sudoku
  10.  {
  11.  public:
  12.      Sudoku() noexcept
  13.      :   gen(rd())
  14.      {  
  15.          square.fill(0);
  16.          std::generate(  std::begin(square),
  17.                          std::end(square),
  18.                          [](){ static uint i = 0; return ++i; });
  19.          generate();
  20.      }
  21.      void print() noexcept
  22.      {  
  23.          for (int i = 0; i < 81; ++i)
  24.          {  
  25.              if (!(i%9) && i)
  26.                  std::cout << "|\n";
  27.              if (!(i%27))
  28.                  std::cout << "+---+---+---+\n";
  29.              if (!(i%3))
  30.                  std::cout << "|";
  31.              std::cout << board[i];
  32.          }
  33.          std::cout << "|\n+---+---+---+\n";
  34.          std::cout << std::flush;
  35.      }
  36.      void generate() noexcept
  37.      {
  38.          // Use rejection sampling
  39.          do
  40.          {
  41.              fillBoardSquare(0);     fillBoardSquare(3);     fillBoardSquare(6);
  42.              fillBoardSquare(27);    fillBoardSquare(30);    fillBoardSquare(33);
  43.              fillBoardSquare(54);    fillBoardSquare(57);    fillBoardSquare(60);
  44.          } while (!validate());
  45.      }
  46.  
  47.  private:
  48.      std::array<uint,81> board;
  49.      std::array<uint,9> square;
  50.      std::random_device rd;
  51.      std::mt19937 gen;
  52.  
  53.      void fillBoardSquare(size_t anchor) noexcept
  54.      {
  55.          std::shuffle(std::begin(square),std::end(square),gen);
  56.          for (size_t i = 0; i < 9; ++i)
  57.          {
  58.              board[anchor+(i%3)] = square[i];
  59.              if (i%3 == 2)
  60.                  anchor += 9;
  61.          }
  62.      }
  63.      bool validate() noexcept
  64.      {
  65.          bool valid = true;
  66.          std::bitset<9> length;
  67.          uint counter = 0;
  68.          for (size_t i = 0; i < 9 && valid; ++i)
  69.          {
  70.              for (size_t j = 0; j < 9 && valid; ++j)
  71.                  length.set(board[i + j*9]-1);
  72.              if (valid = length.all())
  73.                  length.reset();
  74.              ++counter;
  75.          }
  76.          for (size_t j = 0; j < 9 && valid; ++j)
  77.          {
  78.              for (size_t i = 0; i < 9 && valid; ++i)
  79.                  length.set(board[i + j*9]-1);
  80.              if (valid = length.all())
  81.                  length.reset();
  82.              ++counter;
  83.          }
  84.          return valid;
  85.      }
  86.  };
  87.  
  88.  int main()
  89.  {
  90.      std::cout << "Here is your sudoku:\n";
  91.      // You may be waiting a while
  92.      Sudoku sudoku;
  93.      sudoku.print();
  94.      return 0;
  95.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement