Guest User

Untitled

a guest
Apr 28th, 2020
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.36 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. // main function
  27. int main()
  28. {
  29. int random = 0; // bufor do losowania liczb przed wstawieniem ich do tabeli
  30. bool stop; // variable to check
  31. int Counter = 0; // Counter of critical errors
  32.  
  33. // declare 2D table
  34. int** table = new int* [9]; // create pointer to pointers
  35.  
  36. // create table
  37. for (int i = 0; i < 9; i++) // create pointers
  38. table[i] = new int[9]; // make them pointers to ints
  39.  
  40. // initiate RNG
  41. srand(time(NULL)); // start RNG
  42.  
  43. // set table to 0
  44. SetTo0(&table[0][0]);
  45.  
  46. // get a random numbers
  47. for (int row = 0; row < 9; row++) { // go through rows
  48. for (int column = 0; column < 9; column++) { // go through colums
  49. do {
  50. random = rand() % 9 + 1; // random number to next table slot
  51. stop = true;
  52. stop=CheckEverything(*table, random,column,row); // Check if number repeats in current row, column, and square 3x3
  53. Counter++; // increment every loop cycle
  54. if (Counter >= 1000) { // if there were 1000 cycles without correct number
  55. Counter = 0; // try fill new, clear table
  56. column = 0; // set column to 0
  57. row = 0; // set row to 0
  58. break; // and break current cycle
  59. }
  60. } while (!stop); // if CheckEverything is correct it returns TRUE, so try opposite argument
  61. table[row][column] = random; // if everything is correct, fill these table slot
  62. }
  63. }
  64.  
  65. // return values
  66. ReturnValues(&table[0][0]); // print values of these table
  67.  
  68. // end program
  69. return 0; // return integer
  70. }
  71.  
  72. /*////////////////////////////////////////////////////////////////
  73. FUNCTIONS
  74. ////////////////////////////////////////////////////////////////*/
  75.  
  76. // check all statements
  77. bool CheckEverything(int *table,int random,int column,int row){
  78. if (CheckRows(*table, random,column,row) && CheckColumn(*table, random,column,row) && CheckSquares(*table, random,column,row)) // if everything is true
  79. return true; // everything is true
  80. else // if not
  81. return false; // it's not
  82. }
  83.  
  84. bool CheckRows(int *table, int random,int column, int row){ // Check unique values in a row
  85. for (int counter = 0; counter < row; counter++) {
  86. if (random == table[counter][row]) // if there were value like actually random number
  87. return false; // find new value
  88. else // if there weren't
  89. return true; // else it's correct
  90. }
  91. }
  92.  
  93. bool CheckColumn(int *table, int random,int column, int row){ // Check unique values in a column
  94. for (int counter = 0; counter < column; counter++) { // if there were value like actually random number
  95. if (random == *table[row][counter]) // find new value
  96. return false; // find new value
  97. else // if there weren't
  98. return true; // else it's correct
  99. }
  100. }
  101.  
  102. bool CheckSquares(int *table, int random, int column, int row) { // check unique values in 3x3 square
  103. return true;
  104. }
  105.  
  106. void SetTo0(int *table)
  107. {
  108. for (int i = 0; i < 9; i++) { // rows
  109. for (int j = 0; j < 9; j++) { // columns
  110. table[i][j] = 0; // set values to 0
  111. }
  112. }
  113. }
  114.  
  115. void ReturnValues(int* table)
  116. {
  117. for (int row = 0; row < 9; row++) { // rows
  118. for (int column = 0; column < 9; column++) { // column
  119. cout << table[row][column] << " "; // print values
  120. }
  121. cout << endl; // go to the next row
  122. }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment