You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

622 lines
20 KiB

11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4. #include <string.h>
  5. #include "sudoku.h"
  6. #define SIZE_OF_GAMEBORD_AXIS_X 9
  7. #define SIZE_OF_GAMEBORD_AXIS_Y 9
  8. #define AVAILABLE_DIFFICULTIES 3
  9. #define AVAILABLE_LEVELS 3
  10. #define EMPTY 0
  11. #define LEVEL_NUMBER 3
  12. int selected_difficulty;
  13. int selected_level;
  14. bool check_solved;
  15. bool test_help = false;
  16. int test_row_e;
  17. int test_col_e;
  18. int test_num;
  19. void Game_loop(); //is instead of main
  20. void Level_Pool(int selected_difficulty);
  21. void Level_Selection(int Sudoku_grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y]);
  22. void initializeGrid(int Sudoku_grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y]);
  23. void create_playing_field(int Sudoku_grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y], int selected_difficulty, int level);
  24. void Player_actions_for_playing(int Sudoku_grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y]);
  25. void giving_hints_to_player(int Sudoku_grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y]);
  26. void write_userinput_into_Sudoku(int Sudoku_grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y]);
  27. void printGrid(int Sudoku_grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y]);
  28. void check_if_Sudoku_solved(int Sudoku_grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y]);
  29. int availableLevels[AVAILABLE_DIFFICULTIES][AVAILABLE_LEVELS][SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y] = {
  30. { //easy
  31. {{0, 0, 3, 0, 2, 1, 8, 0, 0},
  32. {6, 0, 0, 0, 7, 0, 1, 3, 2},
  33. {9, 2, 1, 5, 0, 0, 7, 6, 4},
  34. {7, 6, 0, 3, 0, 0, 4, 0, 0},
  35. {8, 5, 4, 1, 9, 7, 0, 0, 0},
  36. {1, 0, 0, 4, 0, 6, 0, 0, 0},
  37. {2, 9, 0, 8, 1, 5, 0, 0, 6},
  38. {0, 4, 8, 2, 0, 9, 5, 0, 0},
  39. {5, 0, 0, 7, 0, 3, 2, 0, 0}},
  40. {{7, 0, 2, 0, 0, 0, 9, 5, 0},
  41. {0, 8, 0, 7, 5, 0, 2, 1, 6},
  42. {0, 0, 5, 5, 2, 8, 7, 0, 3},
  43. {5, 1, 8, 4, 3, 0, 6, 0, 0},
  44. {0, 0, 6, 1, 0, 0, 5, 3, 0},
  45. {0, 0, 0, 0, 6, 2, 0, 8, 1},
  46. {0, 9, 0, 2, 0, 4, 0, 0, 5},
  47. {0, 2, 0, 0, 9, 5, 0, 7, 4},
  48. {0, 5, 1, 0, 0, 0, 0, 9, 2}},
  49. {{4, 2, 5, 0, 9, 7, 1, 3, 0},
  50. {0, 7, 0, 1, 0, 3, 0, 0, 6},
  51. {0, 6, 1, 4, 8, 2, 0, 0, 0},
  52. {1, 0, 0, 0, 3, 0, 0, 6, 0},
  53. {7, 0, 8, 0, 2, 0, 0, 1, 3},
  54. {9, 3, 0, 0, 4, 1, 5, 7, 2},
  55. {0, 0, 7, 9, 6, 5, 0, 0, 0},
  56. {0, 0, 4, 3, 7, 0, 2, 0, 5},
  57. {0, 0, 0, 2, 1, 0, 0, 0, 0}}},
  58. {//medium
  59. {{0, 0, 3, 0, 2, 1, 8, 0, 0},
  60. {6, 0, 0, 0, 0, 0, 1, 3, 2},
  61. {0, 2, 1, 0, 0, 0, 7, 6, 4},
  62. {7, 6, 0, 3, 0, 0, 4, 0, 0},
  63. {0, 5, 0, 1, 0, 7, 0, 0, 0},
  64. {1, 0, 0, 4, 0, 6, 0, 0, 0},
  65. {2, 0, 0, 8, 0, 0, 0, 0, 6},
  66. {0, 4, 0, 0, 0, 9, 5, 0, 0},
  67. {5, 0, 0, 7, 0, 3, 2, 0, 0}},
  68. {{7, 0, 2, 0, 0, 0, 9, 5, 0},
  69. {0, 0, 0, 7, 5, 0, 2, 0, 6},
  70. {0, 0, 5, 0, 2, 8, 7, 0, 3},
  71. {5, 1, 8, 0, 3, 0, 6, 0, 0},
  72. {0, 0, 6, 1, 0, 0, 0, 3, 0},
  73. {0, 0, 0, 0, 6, 2, 0, 8, 1},
  74. {0, 9, 0, 2, 0, 4, 0, 0, 5},
  75. {0, 0, 0, 0, 9, 0, 0, 0, 4},
  76. {0, 5, 1, 0, 0, 0, 0, 9, 2}},
  77. {{4, 2, 5, 0, 9, 7, 1, 3, 0},
  78. {0, 0, 0, 1, 0, 3, 0, 0, 6},
  79. {0, 6, 0, 4, 0, 2, 0, 0, 0},
  80. {1, 0, 0, 0, 3, 0, 0, 6, 0},
  81. {7, 0, 8, 0, 2, 0, 0, 1, 3},
  82. {0, 3, 0, 0, 0, 1, 0, 7, 2},
  83. {0, 0, 7, 9, 6, 0, 0, 0, 0},
  84. {0, 0, 4, 3, 7, 0, 2, 0, 5},
  85. {0, 0, 0, 2, 1, 0, 0, 0, 0}}
  86. },
  87. {//hard
  88. {{0, 0, 3, 0, 2, 0, 8, 0, 0},
  89. {6, 0, 0, 0, 0, 0, 0, 3, 0},
  90. {9, 2, 1, 5, 0, 0, 7, 6, 4},
  91. {0, 0, 0, 3, 0, 0, 0, 0, 0},
  92. {0, 5, 4, 1, 0, 7, 0, 0, 0},
  93. {1, 0, 0, 0, 0, 0, 0, 0, 0},
  94. {2, 0, 0, 0, 1, 5, 0, 0, 6},
  95. {0, 4, 0, 2, 0, 0, 0, 0, 0},
  96. {5, 0, 0, 0, 0, 3, 2, 0, 0}},
  97. {{7, 0, 2, 0, 0, 0, 9, 0, 0},
  98. {0, 0, 0, 7, 0, 0, 0, 1, 6},
  99. {0, 0, 5, 0, 0, 8, 7, 0, 3},
  100. {5, 0, 0, 4, 0, 0, 6, 0, 0},
  101. {0, 0, 6, 1, 0, 0, 5, 3, 0},
  102. {0, 0, 0, 0, 0, 2, 0, 0, 1},
  103. {0, 9, 0, 2, 0, 4, 0, 0, 0},
  104. {0, 2, 0, 0, 0, 5, 0, 7, 0},
  105. {0, 5, 0, 0, 0, 0, 0, 9, 2}},
  106. {{4, 0, 5, 0, 9, 7, 1, 3, 0},
  107. {0, 0, 0, 0, 0, 3, 0, 0, 6},
  108. {0, 6, 1, 4, 0, 2, 0, 0, 0},
  109. {1, 0, 0, 0, 3, 0, 0, 6, 0},
  110. {0, 0, 8, 0, 0, 0, 0, 0, 3},
  111. {9, 3, 0, 0, 4, 1, 5, 7, 2},
  112. {0, 0, 7, 9, 0, 5, 0, 0, 0},
  113. {0, 0, 4, 3, 0, 0, 2, 0, 5},
  114. {0, 0, 0, 2, 1, 0, 0, 0, 0}}
  115. }
  116. };
  117. int solutionLevels[AVAILABLE_DIFFICULTIES][AVAILABLE_LEVELS][SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y] = {
  118. { //easy
  119. {{4, 7, 3, 6, 2, 1, 8, 5, 9},
  120. {6, 8, 5, 9, 7, 4, 1, 3, 2},
  121. {9, 2, 1, 5, 3, 8, 7, 6, 4},
  122. {7, 6, 9, 3, 5, 2, 4, 8, 1},
  123. {8, 5, 4, 1, 9, 7, 6, 2, 3},
  124. {1, 3, 2, 4, 8, 6, 9, 7, 5},
  125. {2, 9, 7, 8, 1, 5, 3, 4, 6},
  126. {3, 4, 8, 2, 6, 9, 5, 1, 7},
  127. {5, 1, 6, 7, 4, 3, 2, 9, 8}},
  128. {{7, 3, 2, 6, 4, 1, 9, 5, 8},
  129. {9, 8, 4, 7, 5, 3, 2, 1, 6},
  130. {1, 6, 5, 9, 2, 8, 7, 4, 3},
  131. {5, 1, 8, 4, 3, 7, 6, 2, 9},
  132. {2, 4, 6, 1, 8, 9, 5, 3, 7},
  133. {3, 7, 9, 5, 6, 2, 4, 8, 1},
  134. {8, 9, 7, 2, 1, 4, 3, 6, 5},
  135. {6, 2, 3, 8, 9, 5, 1, 7, 4},
  136. {4, 5, 1, 3, 7, 6, 8, 9, 2}},
  137. {{4, 2, 5, 6, 9, 7, 1, 3, 8},
  138. {8, 7, 9, 1, 5, 3, 4, 2, 6},
  139. {3, 6, 1, 4, 8, 2, 7, 5, 9},
  140. {1, 5, 2, 7, 3, 9, 8, 6, 4},
  141. {7, 4, 8, 5, 2, 6, 9, 1, 3},
  142. {9, 3, 6, 8, 4, 1, 5, 7, 2},
  143. {2, 8, 7, 9, 6, 5, 3, 4, 1},
  144. {6, 1, 4, 3, 7, 8, 2, 9, 5},
  145. {5, 9, 3, 2, 1, 4, 6, 8, 7}}},
  146. {//medium
  147. {{4, 7, 3, 6, 2, 1, 8, 5, 9},
  148. {6, 8, 5, 9, 7, 4, 1, 3, 2},
  149. {9, 2, 1, 5, 3, 8, 7, 6, 4},
  150. {7, 6, 9, 3, 5, 2, 4, 8, 1},
  151. {8, 5, 4, 1, 9, 7, 6, 2, 3},
  152. {1, 3, 2, 4, 8, 6, 9, 7, 5},
  153. {2, 9, 7, 8, 1, 5, 3, 4, 6},
  154. {3, 4, 8, 2, 6, 9, 5, 1, 7},
  155. {5, 1, 6, 7, 4, 3, 2, 9, 8}},
  156. {{7, 3, 2, 6, 4, 1, 9, 5, 8},
  157. {9, 8, 4, 7, 5, 3, 2, 1, 6},
  158. {1, 6, 5, 9, 2, 8, 7, 4, 3},
  159. {5, 1, 8, 4, 3, 7, 6, 2, 9},
  160. {2, 4, 6, 1, 8, 9, 5, 3, 7},
  161. {3, 7, 9, 5, 6, 2, 4, 8, 1},
  162. {8, 9, 7, 2, 1, 4, 3, 6, 5},
  163. {6, 2, 3, 8, 9, 5, 1, 7, 4},
  164. {4, 5, 1, 3, 7, 6, 8, 9, 2}},
  165. {{4, 2, 5, 6, 9, 7, 1, 3, 8},
  166. {8, 7, 9, 1, 5, 3, 4, 2, 6},
  167. {3, 6, 1, 4, 8, 2, 7, 5, 9},
  168. {1, 5, 2, 7, 3, 9, 8, 6, 4},
  169. {7, 4, 8, 5, 2, 6, 9, 1, 3},
  170. {9, 3, 6, 8, 4, 1, 5, 7, 2},
  171. {2, 8, 7, 9, 6, 5, 3, 4, 1},
  172. {6, 1, 4, 3, 7, 8, 2, 9, 5},
  173. {5, 9, 3, 2, 1, 4, 6, 8, 7}}
  174. },
  175. {//hard
  176. {{4, 7, 3, 6, 2, 1, 8, 5, 9},
  177. {6, 8, 5, 9, 7, 4, 1, 3, 2},
  178. {9, 2, 1, 5, 3, 8, 7, 6, 4},
  179. {7, 6, 9, 3, 5, 2, 4, 8, 1},
  180. {8, 5, 4, 1, 9, 7, 6, 2, 3},
  181. {1, 3, 2, 4, 8, 6, 9, 7, 5},
  182. {2, 9, 7, 8, 1, 5, 3, 4, 6},
  183. {3, 4, 8, 2, 6, 9, 5, 1, 7},
  184. {5, 1, 6, 7, 4, 3, 2, 9, 8}},
  185. {{7, 3, 2, 6, 4, 1, 9, 5, 8},
  186. {9, 8, 4, 7, 5, 3, 2, 1, 6},
  187. {1, 6, 5, 9, 2, 8, 7, 4, 3},
  188. {5, 1, 8, 4, 3, 7, 6, 2, 9},
  189. {2, 4, 6, 1, 8, 9, 5, 3, 7},
  190. {3, 7, 9, 5, 6, 2, 4, 8, 1},
  191. {8, 9, 7, 2, 1, 4, 3, 6, 5},
  192. {6, 2, 3, 8, 9, 5, 1, 7, 4},
  193. {4, 5, 1, 3, 7, 6, 8, 9, 2}},
  194. {{4, 2, 5, 6, 9, 7, 1, 3, 8},
  195. {8, 7, 9, 1, 5, 3, 4, 2, 6},
  196. {3, 6, 1, 4, 8, 2, 7, 5, 9},
  197. {1, 5, 2, 7, 3, 9, 8, 6, 4},
  198. {7, 4, 8, 5, 2, 6, 9, 1, 3},
  199. {9, 3, 6, 8, 4, 1, 5, 7, 2},
  200. {2, 8, 7, 9, 6, 5, 3, 4, 1},
  201. {6, 1, 4, 3, 7, 8, 2, 9, 5},
  202. {5, 9, 3, 2, 1, 4, 6, 8, 7}}
  203. }
  204. };
  205. void Game_loop() {
  206. int Sudoku_grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y];
  207. selected_level = 0;
  208. while (1) {
  209. printf("\nDifficulty Function - Choose difficulty:\n");
  210. printf("1. Easy\n2. Medium\n3. Hard\n");
  211. printf("Enter the corresponding number or type 'quit' to exit: ");
  212. if(!test_help){
  213. char input[10];
  214. scanf("%s", input);
  215. if (strcmp(input, "quit") == 0) {
  216. break;
  217. }
  218. selected_difficulty = input[0] - '0'; // Convert the first character to an integer
  219. if ((selected_difficulty >= 1 && selected_difficulty <= 3) && input[1] == '\0') {
  220. Level_Pool(selected_difficulty);
  221. Level_Selection(Sudoku_grid);
  222. Player_actions_for_playing(Sudoku_grid);
  223. } else {
  224. printf("Invalid input. Please enter a number between 1 and 3.\n");
  225. }
  226. }
  227. if(test_help){
  228. Level_Pool(selected_difficulty);
  229. break;
  230. }
  231. }
  232. }
  233. void Level_Pool(int selected_difficulty) {
  234. printf("\nAvailable Levels for Difficulty %d:\n", selected_difficulty);
  235. for (int i = 0; i < LEVEL_NUMBER; i++) {
  236. printf("%d. Level %d\n", i + 1, i + 1);
  237. }
  238. }
  239. void Level_Selection(int Sudoku_grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y]) {
  240. int level = 0;
  241. printf("\nSelect a level:\n");
  242. while (true){
  243. if(!test_help){
  244. char level_select[10];
  245. scanf("%s", level_select);
  246. level = level_select[0] - '0'; // Convert the first character to an integer
  247. if ((level >= 1 && level <= 3) && level_select[1] == '\0') {
  248. selected_level = level;
  249. level--; // Adjust to 0-based index
  250. create_playing_field(Sudoku_grid, selected_difficulty, selected_level);
  251. break;
  252. } else {
  253. printf("Invalid input. Please enter a number between 1 and 3.\n");
  254. }
  255. }
  256. if(test_help){
  257. selected_level = level;
  258. level--;
  259. create_playing_field(Sudoku_grid, selected_difficulty, selected_level);
  260. break;
  261. }
  262. }
  263. }
  264. void initializeGrid(int Sudoku_grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y]) {
  265. for (int i = 0; i < SIZE_OF_GAMEBORD_AXIS_X; i++) {
  266. for (int j = 0; j < SIZE_OF_GAMEBORD_AXIS_Y; j++) {
  267. Sudoku_grid[i][j] = EMPTY;
  268. }
  269. }
  270. }
  271. void create_playing_field(int Sudoku_grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y], int selected_difficulty, int level) {
  272. initializeGrid(Sudoku_grid);
  273. if(!test_help){
  274. for (int i = 0; i < SIZE_OF_GAMEBORD_AXIS_X; i++) {
  275. for (int j = 0; j < SIZE_OF_GAMEBORD_AXIS_Y; j++) {
  276. Sudoku_grid[i][j] = availableLevels[selected_difficulty - 1][level - 1][i][j];
  277. }
  278. }
  279. }
  280. for (int i = 0; i < SIZE_OF_GAMEBORD_AXIS_X; i++) {
  281. for (int j = 0; j < SIZE_OF_GAMEBORD_AXIS_Y; j++) {
  282. printf("%d", Sudoku_grid[i][j]);
  283. }
  284. printf("\n");
  285. }
  286. }
  287. void Player_actions_for_playing(int Sudoku_grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y]) {
  288. while (1) {
  289. printf("\nTurn function - Choose an action:\n");
  290. printf("1. Hints\n");
  291. printf("2. Insert Number into Game\n");
  292. printf("3. Print Sudoku grid\n");
  293. printf("4. Check if your Solution is correct\n");
  294. printf("5. Select level\n");
  295. printf("6. Quit\n");
  296. int action;
  297. if(test_help){
  298. printGrid(Sudoku_grid);
  299. check_if_Sudoku_solved(Sudoku_grid);
  300. break;
  301. }
  302. if(!test_help){
  303. while (true){
  304. char action_str[10];
  305. scanf("%s", action_str);
  306. action = action_str[0] - '0'; // Convert the first character to an integer
  307. if ((action >= 1 && action <= 6) && action_str[1] == '\0') {
  308. break;
  309. } else {
  310. printf("Invalid input. Please enter a number between 1 and 6.\n");
  311. }
  312. }
  313. switch (action) {
  314. case 1:
  315. giving_hints_to_player(Sudoku_grid);
  316. break;
  317. case 2:
  318. write_userinput_into_Sudoku(Sudoku_grid);
  319. break;
  320. case 3:
  321. printGrid(Sudoku_grid);
  322. break;
  323. case 4:
  324. check_if_Sudoku_solved(Sudoku_grid);
  325. break;
  326. case 5:
  327. Level_Selection(Sudoku_grid);
  328. break;
  329. case 6:
  330. printf("Exiting Sudoku program.\n");
  331. exit(0);
  332. default:
  333. printf("Invalid input. Please enter a number between 1 and 6.\n");
  334. }
  335. }
  336. }
  337. }
  338. void giving_hints_to_player(int Sudoku_grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y]) {
  339. printf("\nTip function - Choose an option:\n");
  340. printf("1. Set the user-specified cell to the right value\n");
  341. printf("2. Set the user-specified 3x3 field to the right values\n");
  342. printf("3. Solve the entire puzzle for the current level\n");
  343. if(test_help){
  344. initializeGrid(Sudoku_grid);
  345. for (int i = 0; i < SIZE_OF_GAMEBORD_AXIS_X; i++) {
  346. for (int j = 0; j < SIZE_OF_GAMEBORD_AXIS_Y; j++) {
  347. Sudoku_grid[i][j] = solutionLevels[selected_difficulty - 1][selected_level - 1][i][j];
  348. }
  349. }
  350. printf("Puzzle solved. \n");
  351. }
  352. int option = 0;
  353. if(!test_help){
  354. while (true){
  355. char tip_str[10];
  356. scanf("%s", tip_str);
  357. option = tip_str[0] - '0'; // Convert the first character to an integer
  358. if ((option >= 1 && option <= 3) && tip_str[1] == '\0') {
  359. break;
  360. } else {
  361. printf("Invalid input. Please enter a number between 1 and 3.\n");
  362. }
  363. }
  364. switch (option) {
  365. case 1:
  366. printf("Enter the coordinates (row and column) separated by space:\n");
  367. int row, col;
  368. while (true){
  369. char row_str[10];
  370. char col_str[10];
  371. scanf("%s %s", row_str, col_str);
  372. row = row_str[0] - '0'; // Convert the first character to an integer
  373. col = col_str[0] - '0'; // Convert the first character to an integer
  374. if (((row >= 1 && row <= 9) && row_str[1] == '\0') && ((col >= 1 && col <= 9) && col_str[1] == '\0')) {
  375. break;
  376. } else {
  377. printf("Invalid input. Please enter a number between 1 and 9.\n");
  378. }
  379. }
  380. Sudoku_grid[row - 1][col - 1] = solutionLevels[selected_difficulty - 1][selected_level - 1][row - 1][col - 1];
  381. printf("Value set successfully.\n");
  382. break;
  383. case 2:
  384. printf("Enter the coordinates (top-left cell of the 3x3 field) separated by space:\n");
  385. int startRow, startCol;
  386. while (true){
  387. char row_squ[10];
  388. char col_squ[10];
  389. scanf("%s %s", row_squ, col_squ);
  390. startRow = row_squ[0] - '0'; // Convert the first character to an integer
  391. startCol = col_squ[0] - '0'; // Convert the first character to an integer
  392. if (((startRow >= 1 && startRow <= 9) && row_squ[1] == '\0') && ((startCol >= 1 && startCol <= 9) && col_squ[1] == '\0')) {
  393. break;
  394. } else {
  395. printf("Invalid input. Please enter a number between 1 and 9.\n");
  396. }
  397. }
  398. for (int i = 0; i < 3; i++) {
  399. for (int j = 0; j < 3; j++) {
  400. Sudoku_grid[startRow - 1 + i][startCol - 1 + j] =
  401. solutionLevels[selected_difficulty - 1][selected_level - 1][startRow - 1 + i][startCol - 1 + j];
  402. }
  403. }
  404. printf("3x3 field set successfully.\n");
  405. break;
  406. case 3:
  407. for (int i = 0; i < SIZE_OF_GAMEBORD_AXIS_X; i++) {
  408. for (int j = 0; j < SIZE_OF_GAMEBORD_AXIS_Y; j++) {
  409. Sudoku_grid[i][j] = solutionLevels[selected_difficulty - 1][selected_level - 1][i][j];
  410. }
  411. }
  412. printf("Puzzle solved. \n");
  413. break;
  414. default:
  415. printf("Invalid option. Please enter a number between 1 and 3.\n");
  416. }
  417. }
  418. }
  419. void write_userinput_into_Sudoku(int Sudoku_grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y]) {
  420. printf("\nInput function - Choose an action:\n");
  421. printf("1. Insert value in an empty field\n");
  422. printf("2. Clear an already filled field\n");
  423. if(test_help){
  424. initializeGrid(Sudoku_grid);
  425. Sudoku_grid[test_row_e -1][test_col_e -1] = test_num;
  426. printGrid(Sudoku_grid);
  427. }
  428. int action;
  429. if(!test_help){
  430. while (true){
  431. char in_str[10];
  432. scanf("%s", in_str);
  433. action = in_str[0] - '0'; // Convert the first character to an integer
  434. if ((action >= 1 && action <= 2) && in_str[1] == '\0') {
  435. break;
  436. } else {
  437. printf("Invalid input. Please enter a number between 1 and 2.\n");
  438. }
  439. }
  440. switch (action) {
  441. case 1:
  442. printf("Enter the coordinates (row and column) separated by space:\n");
  443. int row_e, col_e, num;
  444. while (true){
  445. char rowe_str[10];
  446. char cole_str[10];
  447. scanf("%s %s", rowe_str, cole_str);
  448. row_e = rowe_str[0] - '0'; // Convert the first character to an integer
  449. col_e = cole_str[0] - '0'; // Convert the first character to an integer
  450. if (((row_e >= 1 && row_e <= 9) && rowe_str[1] == '\0') && ((col_e >= 1 && col_e <= 9) && cole_str[1] == '\0')) {
  451. break;
  452. } else {
  453. printf("Invalid input. Please enter a number between 1 and 9.\n");
  454. }
  455. }
  456. printf("Enter the value to insert (1-9):\n");
  457. while (true){
  458. char nume_str[10];
  459. scanf("%s", nume_str);
  460. num = nume_str[0] - '0'; // Convert the first character to an integer
  461. if ((num >= 1 && num <= 9) && nume_str[1] == '\0') {
  462. Sudoku_grid[row_e - 1][col_e - 1] = num;
  463. printf("Value inserted successfully.\n");
  464. break;
  465. } else {
  466. printf("Invalid input. Please enter a number between 1 and 9.\n");
  467. }
  468. }
  469. break;
  470. case 2:
  471. printf("Enter the coordinates (row and column) separated by space:\n");
  472. int row_d, col_d;
  473. while (true){
  474. char rowd_str[10];
  475. char cold_str[10];
  476. scanf("%s %s", rowd_str, cold_str);
  477. row_d = rowd_str[0] - '0'; // Convert the first character to an integer
  478. col_d = cold_str[0] - '0'; // Convert the first character to an integer
  479. if (((row_d >= 1 && row_d <= 9) && rowd_str[1] == '\0') && ((col_d >= 1 && col_d <= 9) && cold_str[1] == '\0')) {
  480. Sudoku_grid[row_d - 1][col_d - 1] = 0;
  481. printf("Cell cleared successfully.\n");
  482. break;
  483. } else {
  484. printf("Invalid input. Please enter a number between 1 and 9.\n");
  485. }
  486. }
  487. break;
  488. default:
  489. printf("Invalid input. Please enter 1 or 2.\n");
  490. }
  491. printGrid(Sudoku_grid);
  492. }
  493. }
  494. void printGrid(int Sudoku_grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y]) {
  495. printf("\nSudoku Grid:\n");
  496. for (int i = 0; i < SIZE_OF_GAMEBORD_AXIS_X; i++) {
  497. for (int j = 0; j < SIZE_OF_GAMEBORD_AXIS_Y; j++) {
  498. printf("%d ", Sudoku_grid[i][j]);
  499. }
  500. printf("\n");
  501. }
  502. }
  503. void check_if_Sudoku_solved(int Sudoku_grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y]) {
  504. printf("\nDone function - Checking if the solution is correct...\n");
  505. for (int i = 0; i < SIZE_OF_GAMEBORD_AXIS_X; i++) {
  506. for (int j = 0; j < SIZE_OF_GAMEBORD_AXIS_Y; j++) {
  507. if (Sudoku_grid[i][j] != solutionLevels[selected_difficulty - 1][selected_level - 1][i][j]) {
  508. printf("Incorrect solution. Keep trying!\n");
  509. check_solved = false;
  510. return;
  511. }
  512. }
  513. }
  514. check_solved = true;
  515. printf("Congratulations! Sudoku is solved correctly.\n");
  516. }
  517. int mainn(){
  518. Game_loop();
  519. return 0;
  520. }