Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.35 KB | None | 0 0
  1. // This is an algorithm for solving a sudoku puzzle. The algorithm used is called "Backtracking", which can be easily
  2. // looked up on wikipedia
  3.  
  4. #include <iostream>
  5. #include <random> // For random generator engine
  6. #include <chrono>
  7. #include <thread>
  8.  
  9. using namespace std;
  10.  
  11. bool isValidInput(int number);
  12. void moveForward();
  13. void moveBackward();
  14. int returnIncreasedNum();
  15.  
  16.  
  17. int gameBoard[9][9] = {
  18. { 0,0,0,0,0,0,0,0,0 },
  19. { 0,0,0,0,0,0,0,0,0 },
  20. { 0,0,0,0,0,0,0,0,0 },
  21. { 0,0,0,0,0,0,0,0,0 },
  22. { 0,0,0,0,0,0,0,0,0 },
  23. { 0,0,0,0,0,0,0,0,0 },
  24. { 0,0,0,0,0,0,0,0,0 },
  25. { 0,0,0,0,0,0,0,0,0 },
  26. { 0,0,0,0,0,0,0,0,0 }
  27. //int gameBoard[9][9] = {
  28. //{ 0,0,4,1,0,0,0,0,2 },
  29. //{ 0,7,6,0,0,8,3,0,0 },
  30. //{ 0,1,0,0,3,2,0,0,0 },
  31. //{ 0,6,7,2,0,3,0,0,0 },
  32. //{ 0,2,8,4,0,1,7,6,0 },
  33. //{ 0,0,0,8,0,7,2,3,0 },
  34. //{ 0,0,0,3,2,0,0,9,0 },
  35. //{ 0,0,2,7,0,0,1,5,0 },
  36. //{ 5,0,0,0,0,9,4,0,0 }
  37. };
  38. int gameBoardStatic[9][9] = {
  39. { 0,0,4,1,0,0,0,0,2 },
  40. { 0,7,6,0,0,8,3,0,0 },
  41. { 0,1,0,0,3,2,0,0,0 },
  42. { 0,6,7,2,0,3,0,0,0 },
  43. { 0,2,8,4,0,1,7,6,0 },
  44. { 0,0,0,8,0,7,2,3,0 },
  45. { 0,0,0,3,2,0,0,9,0 },
  46. { 0,0,2,7,0,0,1,5,0 },
  47. { 5,0,0,0,0,9,4,0,0 }
  48. };
  49.  
  50. int row = 0; // Row
  51. int col = 0; // Column
  52.  
  53. const int MAXNUMBERS = 20; // max amount of numbers to be filled into the board
  54.  
  55. int main()
  56. {
  57. std::random_device rd; //Will be used to obtain a seed for the random number engine
  58. std::mt19937 gen(rd()); //Standard mersenne_twister_engine seeded with rd()
  59.  
  60. for (int i = 0; i < MAXNUMBERS; i++)
  61. {
  62. bool notAccepted = true;
  63.  
  64. while (notAccepted)
  65. {
  66. row = (rd() % 9); // Get a random row
  67. col = (rd() % 9); // Get a random column
  68.  
  69. if (gameBoard[row][col] == 0)
  70. notAccepted = false;
  71. }
  72.  
  73. cout << i << endl;
  74.  
  75. while (true)
  76. {
  77. int number = rd() % 9 + 1; // "+ 1" because we want numbers from 1-9, not 0-9
  78.  
  79. if (isValidInput(number)) // If the number does not violate any rules
  80. {
  81. gameBoard[row][col] = number; // Add 'k' to the cell
  82. break;
  83. }
  84. }
  85. }
  86.  
  87. // SET THE STATIC BOARD = NEWLY GENERATED BOARD
  88. for (int i = 0; i < 9; i++)
  89. {
  90. for (int j = 0; j < 9; j++)
  91. {
  92. gameBoardStatic[i][j] = gameBoard[i][j];
  93. }
  94. }
  95.  
  96. // JUST FOR PRINTOUT
  97. for (int i = 0; i < 9; i++)
  98. {
  99. for (int j = 0; j < 9; j++)
  100. {
  101. cout << gameBoard[i][j] << " ";
  102. }
  103. cout << "\n";
  104. }
  105. cout << "\n";
  106.  
  107.  
  108. row = col = 0; // Reset numbers for the backtracking algorithm
  109.  
  110. // BACKTRACKING ALGORITHM
  111. while (row < 9 && col < 9)
  112. {
  113. bool backTrack = true;
  114.  
  115. for (int k = gameBoard[row][col]; k <= 9; k++) // First go through all possible numbers 1-9
  116. {
  117. if (k == 0)
  118. k = 1;
  119.  
  120. if (isValidInput(k)) // If the number 'k' does not violate any rules
  121. {
  122. gameBoard[row][col] = k; // Add 'k' to the cell
  123. moveForward();
  124. backTrack = false;
  125. break;
  126. }
  127. }
  128.  
  129. if (backTrack) // If no numbers from 1-9 matches in the cell, start backtracking
  130. {
  131. gameBoard[row][col] = 0; // Clear the current cell in which there was no match
  132. moveBackward(); // Move to the previous cell
  133.  
  134. if (returnIncreasedNum() != 0) // If we can increase the number in the cell
  135. {
  136. gameBoard[row][col] = returnIncreasedNum(); // Increase the number by the next allowed number
  137. moveForward();
  138. }
  139. else
  140. {
  141. gameBoard[row][col] = 0;
  142. moveBackward();
  143. continue;
  144. }
  145. }
  146. }
  147. // JUST FOR PRINTOUT
  148. for (int i = 0; i < 9; i++)
  149. {
  150. for (int j = 0; j < 9; j++)
  151. {
  152. cout << gameBoard[i][j] << " ";
  153. }
  154. cout << "\n";
  155. }
  156. cout << "\n";
  157. }
  158.  
  159.  
  160. int returnIncreasedNum() // Checks if we can increase the number in the cell. If yes return number, if no return 0
  161. {
  162. if (gameBoard[row][col] == 9)
  163. return 0;
  164. else
  165. {
  166. for (int i = gameBoard[row][col] + 1; i <= 9; i++) // +1 to get the next number
  167. {
  168. if (isValidInput(i))
  169. return i; // Return the valid number
  170. }
  171. return 0;
  172. }
  173. }
  174.  
  175. void moveForward() // Moves to the next cell infront
  176. {
  177. bool isTemplate = true; // If we hit a cell from the template
  178.  
  179. while (isTemplate) // Continues as long as there is a blank space available
  180. {
  181. if (col == 8)
  182. {
  183. row++;
  184. col = 0;
  185. }
  186. else
  187. col++;
  188. if (gameBoardStatic[row][col] == 0)
  189. isTemplate = false;
  190. }
  191. }
  192.  
  193. void moveBackward() // Moves to the previous cell behind
  194. {
  195. bool isTemplate = true; // If we hit a cell from the template
  196.  
  197. while (isTemplate) // Continues as long as there is a blank space available
  198. {
  199. if (col == 0)
  200. {
  201. row--;
  202. col = 8;
  203. }
  204. else
  205. col--;
  206. if (gameBoardStatic[row][col] == 0)
  207. isTemplate = false;
  208. }
  209. }
  210.  
  211. bool isValidInput(int number) // Checks if the cell number does not violate any rules
  212. {
  213. //Check the box constraints:
  214. // Get's the top left position
  215. int newR = row - (row % 3);
  216. int newC = col - (col % 3);
  217.  
  218. for (int r = 0; r < 3; r++) // local row
  219. {
  220. newC = col - (col % 3);
  221. for (int c = 0; c < 3; c++) // local columnn
  222. if (gameBoard[newR + r][newC + c] == number && gameBoard[newR + r][newC + c] != gameBoard[row][col]) // If the number k already exists in the block
  223. return false;
  224. }
  225.  
  226. // Check if the same numbers exist on the row/column lines
  227. for (int l = 0; l < 9; l++) // For hver rad/kolonne
  228. if (number == gameBoard[l][col] || number == gameBoard[row][l]) // Sjekke om tallet k eksisterer i samme rad/kolonne
  229. return false;
  230.  
  231. return true;
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement