Guest User

Untitled

a guest
Apr 28th, 2020
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.76 KB | None | 0 0
  1. /*
  2. randomly generated SUDOKU (unique numbers in 3x3 squares, in every row, and every column)
  3. in C++, created by redve as test of his skills, BD
  4. */
  5.  
  6. // includes
  7. #include <iostream> // standard library, here it's just to print values
  8. #include <cstdlib> // randomize numbers
  9. #include <time.h> // RNG source
  10.  
  11. // set namespace
  12. using namespace std; // namespace std
  13.  
  14. bool CheckEverything(int** table, int random, int column, int row); // Check is everything OK
  15.  
  16. bool CheckRows(int** table, int random, int column, int row); // Check unique values in a row
  17.  
  18. bool CheckColumn(int** table, int random, int column, int row); // Check unique values in a column
  19.  
  20. bool CheckSquares(int** table, int random, int column, int row); // check unique values in 3x3 square
  21.  
  22. void ReturnValues(int** table); // print values
  23.  
  24. void SetTo0(int** table); // set table to 0
  25.  
  26. int SquareDetection(int x, int y); // detect square
  27.  
  28. // main function
  29. int main()
  30. {
  31. int random = 0; // bufor do losowania liczb przed wstawieniem ich do tabeli
  32. bool stop; // variable to check
  33. int Counter = 0; // Counter of critical errors
  34.  
  35. // declare 2D table
  36. int** table = new int* [9]; // create pointer to pointers
  37.  
  38. // create table
  39. for (int i = 0; i < 9; i++) // create pointers
  40. table[i] = new int[9]; // make them pointers to ints
  41.  
  42. // initiate RNG
  43. srand(time(NULL)); // start RNG
  44.  
  45. // set table to 0
  46. SetTo0(table);
  47.  
  48. // get a random numbers
  49. for (int row = 0; row < 9; row++) { // go through rows
  50. for (int column = 0; column < 9; column++) { // go through colums
  51. do {
  52. random = rand() % 9 + 1; // random number to next table slot
  53. stop = true;
  54. stop = CheckEverything(table, random, column, row); // Check if number repeats in current row, column, and square 3x3
  55. Counter++; // increment every loop cycle
  56. if (Counter >= 1000) { // if there were 1000 cycles without correct number
  57. Counter = 0; // try fill new, clear table
  58. column = 0; // set column to 0
  59. row = 0; // set row to 0
  60. break; // and break current cycle
  61. }
  62. } while (!stop); // if CheckEverything is correct it returns TRUE, so try opposite argument
  63. table[row][column] = random; // if everything is correct, fill these table slot
  64. }
  65. }
  66.  
  67. // return values
  68. ReturnValues(table); // print values of these table
  69.  
  70. // exit program
  71. return 0; // return integer
  72. }
  73.  
  74. /*////////////////////////////////////////////////////////////////
  75. FUNCTIONS
  76. ////////////////////////////////////////////////////////////////*/
  77.  
  78. bool CheckRows(int** table, int random, int column, int row) { // Check unique values in a row
  79. //cout << "================" << endl << "wartosc tabeli komorki: " << table[row][column] << endl << "aktualnie rozpatrywana liczba: " << random << endl << "aktualna kolumna: " << column << endl << "aktualny rzad: "<< row << endl << "==============" << endl;
  80. for (int i = 0; i < row; i++)
  81. if (random == table[i][column]) // if there were value like actually random number
  82. return false; // find new value
  83. return true; // default its true
  84. }
  85.  
  86. bool CheckColumn(int** table, int random, int column, int row) { // Check unique values in a column
  87. for (int i = 0; i < column; i++)
  88. if (random == table[row][i]) // if there were value like actually random number
  89. return false; // find new value
  90. return true; // default it's true
  91. }
  92.  
  93. bool CheckSquares(int** table, int random, int column, int row) { // check unique values in 3x3 square
  94. int square = SquareDetection(row, column); // which field 3x3 it is
  95. int** square_values = new int* [9]; // create new 9 pointers table
  96. for (int i = 0; i < 9; i++) // create 9x9 table
  97. square_values[i] = new int[9];
  98. square_values[square][(column % 3 + row % 3) % 9] = random; // fill them with current random number
  99. for (int i = 0; i < (column % 3 + row % 3) % 9; i++)
  100. if (random = square_values[square][i]) // if there is same number in square 3x3
  101. return false; // it's false
  102. return true; // default it's true
  103. }
  104.  
  105. void SetTo0(int** table) { // set default values to 0
  106.  
  107. for (int i = 0; i < 9; i++) // rows
  108. for (int j = 0; j < 9; j++) // columns
  109. table[i][j] = 0; // set values to 0
  110. }
  111.  
  112. void ReturnValues(int** table) { // print values
  113. for (int row = 0; row < 9; row++) { // rows
  114. for (int column = 0; column < 9; column++) // column
  115. cout << table[row][column] << " "; // print values
  116. cout << endl; // go to the next row
  117. }
  118. }
  119.  
  120. bool CheckEverything(int** table, int random, int column, int row) { // check all statements
  121. if (CheckRows(table, random, column, row) && CheckColumn(table, random, column, row) && CheckSquares(table, random, column, row))
  122. return true; // everything is true
  123. else // if not
  124. return false; // it's not
  125. }
  126.  
  127. int SquareDetection(int x, int y) {
  128. int square = 0;
  129. if ((x >= 0 && x <= 2) && (y >= 0 && y <= 2)) {
  130. square = 0;
  131. }
  132. if ((x >= 3 && x <= 5) && (y >= 0 && y <= 2)) {
  133. square = 3;
  134. }
  135. if ((x >= 6 && x <= 8) && (y >= 0 && y <= 2)) {
  136. square = 6;
  137. }
  138. if ((x >= 0 && x <= 2) && (y >= 3 && y <= 5)) {
  139. square = 1;
  140. }
  141. if ((x >= 3 && x <= 5) && (y >= 3 && y <= 5)) {
  142. square = 4;
  143. }
  144. if ((x >= 6 && x <= 8) && (y >= 3 && y <= 5)) {
  145. square = 7;
  146. }
  147. if ((x >= 0 && x <= 2) && (y >= 6 && y <= 8)) {
  148. square = 2;
  149. }
  150. if ((x >= 3 && x <= 5) && (y >= 6 && y <= 8)) {
  151. square = 5;
  152. }
  153. if ((x >= 6 && x <= 8) && (y >= 6 && y <= 8)) {
  154. square = 8;
  155. }
  156. return square;
  157. }
Advertisement
Add Comment
Please, Sign In to add comment