Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.26 KB | None | 0 0
  1. #include <stdbool.h>
  2. #include <stddef.h>
  3. #include <stdint.h>
  4. #include <tice.h>
  5.  
  6. #include <math.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9.  
  10. #include <graphx.h>
  11. #include <keypadc.h>
  12. #include <fileioc.h>
  13. #include <fontlibc.h>
  14.  
  15. #include "font/font.h"
  16.  
  17. #define BLACK 0
  18. #define BLUE 24 //INSET numbers - numbers you can't change
  19. #define GRAY 222 //Pause menu background?
  20. #define RED 224
  21. #define WHITE 255
  22.  
  23. #define SUDOKUDRAWX 20 //Draw Position of Sudoku Grid.
  24. #define SUDOKUDRAWY 46
  25.  
  26. #define UNASSIGNED 0 //Unassigned locations will be labeled as a 0
  27. #define GRID_WIDTH 9 //Width (and height) of the Sudoku Puzzle grid
  28. #define BOX_WIDTH 3 //Size of each "box" in the grid
  29.  
  30. void menu(void);
  31. void play(void);
  32.  
  33. bool FindUnassignedLocation(uint8_t grid[GRID_WIDTH][GRID_WIDTH], uint8_t *rowp, uint8_t *colp);
  34. bool isSafe(uint8_t grid[GRID_WIDTH][GRID_WIDTH], uint8_t row, uint8_t col, uint8_t num);
  35. void randSeq(uint8_t *seq);
  36. bool SolveSudoku(uint8_t *grid);
  37. bool UsedInBox(uint8_t grid[GRID_WIDTH][GRID_WIDTH], uint8_t boxStartRow, uint8_t boxStartCol, uint8_t num);
  38. bool UsedInCol(uint8_t grid[GRID_WIDTH][GRID_WIDTH], uint8_t col, uint8_t num);
  39. bool UsedInRow(uint8_t grid[GRID_WIDTH][GRID_WIDTH], uint8_t row, uint8_t num);
  40.  
  41. void displayGrid(uint8_t playGrid[GRID_WIDTH][GRID_WIDTH], uint8_t solvedGrid[GRID_WIDTH][GRID_WIDTH]);
  42. void GenerateSudoku(uint8_t *playGrid, uint8_t solvedGrid[GRID_WIDTH][GRID_WIDTH]);
  43.  
  44.  
  45. void main()
  46. {
  47. srand(rtc_Time());
  48. fontlib_SetWindowFullScreen();
  49. fontlib_SetFont(font20, 0);
  50. menu();
  51. }
  52.  
  53. void menu(void)
  54. {
  55. gfx_Begin();
  56. play();
  57. gfx_End();
  58. }
  59.  
  60. void play(void) {
  61. int row, col;
  62. uint8_t playGrid[GRID_WIDTH][GRID_WIDTH] = {
  63. {0,0,0,0,0,0,0,0,0},
  64. {0,0,0,0,0,0,0,0,0},
  65. {0,0,0,0,0,0,0,0,0},
  66. {0,0,0,0,0,0,0,0,0},
  67. {0,0,0,0,0,0,0,0,0},
  68. {0,0,0,0,0,0,0,0,0},
  69. {0,0,0,0,0,0,0,0,0},
  70. {0,0,0,0,0,0,0,0,0},
  71. {0,0,0,0,0,0,0,0,0}
  72. };
  73. uint8_t solvedGrid[GRID_WIDTH][GRID_WIDTH] = {
  74. {0,0,0,0,0,0,0,0,0},
  75. {0,0,0,0,0,0,0,0,0},
  76. {0,0,0,0,0,0,0,0,0},
  77. {0,0,0,0,0,0,0,0,0},
  78. {0,0,0,0,0,0,0,0,0},
  79. {0,0,0,0,0,0,0,0,0},
  80. {0,0,0,0,0,0,0,0,0},
  81. {0,0,0,0,0,0,0,0,0},
  82. {0,0,0,0,0,0,0,0,0}
  83. };
  84.  
  85. SolveSudoku(&solvedGrid);
  86. GenerateSudoku(playGrid, &solvedGrid);
  87.  
  88. displayGrid(playGrid, solvedGrid);
  89. while (!os_GetCSC());
  90. }
  91.  
  92. bool FindUnassignedLocation(uint8_t grid[GRID_WIDTH][GRID_WIDTH], uint8_t *rowp, uint8_t *colp) { //row pointer; column pointer
  93. uint8_t row, col;
  94. for (row = 0; row < GRID_WIDTH; row ++)
  95. for (col = 0; col < GRID_WIDTH; col ++)
  96. if (grid[row][col] == UNASSIGNED) {
  97. *rowp = row;
  98. *colp = col;
  99. return true;
  100. }
  101. return false;
  102. }
  103.  
  104. bool isSafe(uint8_t grid[GRID_WIDTH][GRID_WIDTH], uint8_t row, uint8_t col, uint8_t num) {
  105. return !UsedInRow(grid, row, num) &&
  106. !UsedInCol(grid, col, num) &&
  107. !UsedInBox(grid, row - row%BOX_WIDTH, col - col%BOX_WIDTH, num) &&
  108. grid[row][col] == UNASSIGNED;
  109. }
  110.  
  111. void randSeq(uint8_t *seq) {
  112. uint8_t a, b, i, rand;
  113. for(i = 0; i <= 8; i ++) {
  114. rand = randInt(0,8);
  115. a = seq[0];
  116. b = seq[rand];
  117. seq[0] = b;
  118. seq[rand] = a;
  119. }
  120. }
  121.  
  122. bool SolveSudoku(uint8_t *grid) {
  123. uint8_t row = 0, col = 0, num, sequence;
  124. uint8_t seq[9] = {1,4,7,3,6,9,2,5,8}; //used for generating puzzles
  125.  
  126. if (!FindUnassignedLocation(grid, &row, &col)) //No unassigned locations = solved
  127. return true;
  128.  
  129. randSeq(seq);
  130. for (sequence = 0; sequence <= N - 1; sequence ++) {
  131.  
  132. num = seq[sequence];
  133.  
  134. if (isSafe(grid, row, col, num)) {
  135. grid[row][col] = num;
  136.  
  137. if (SolveSudoku(grid))
  138. return true; //woohoo! We solved it!
  139.  
  140. grid[row][col] = UNASSIGNED; //failure, unmake & try again
  141. }
  142. }
  143. return false; // this triggers backtrackiing
  144. }
  145.  
  146. bool UsedInBox(uint8_t grid[GRID_WIDTH][GRID_WIDTH], uint8_t boxStartRow, uint8_t boxStartCol, uint8_t num) {
  147. uint8_t row, col;
  148. for (row = 0; row < BOX_WIDTH; row ++)
  149. for (col = 0; col < BOX_WIDTH; col ++)
  150. if (grid[row + boxStartRow][col + boxStartCol] == num)
  151. return true;
  152. return false;
  153. }
  154.  
  155. bool UsedInCol(uint8_t grid[GRID_WIDTH][GRID_WIDTH], uint8_t col, uint8_t num) {
  156. uint8_t row;
  157. for (row = 0; row < GRID_WIDTH; row ++)
  158. if (grid[row][col] == num)
  159. return true;
  160. return false;
  161. }
  162.  
  163. bool UsedInRow(uint8_t grid[GRID_WIDTH][GRID_WIDTH], uint8_t row, uint8_t num) {
  164. uint8_t col;
  165. for (col = 0; col < GRID_WIDTH; col ++)
  166. if (grid[row][col] == num)
  167. return true;
  168. return false;
  169. }
  170.  
  171. void displayGrid(uint8_t playGrid[GRID_WIDTH][GRID_WIDTH], uint8_t solvedGrid[GRID_WIDTH][GRID_WIDTH]) {
  172.  
  173. uint8_t row, col;
  174. uint8_t i, j;
  175.  
  176. //Make every other box gray
  177. gfx_SetColor(GRAY);
  178. gfx_FillRectangle_NoClip(SUDOKUDRAWX + 60, SUDOKUDRAWY, 60, 60);
  179. gfx_FillRectangle_NoClip(SUDOKUDRAWX, SUDOKUDRAWY + 60, 60, 60);
  180. gfx_FillRectangle_NoClip(SUDOKUDRAWX + 120, SUDOKUDRAWY + 60, 60, 60);
  181. gfx_FillRectangle_NoClip(SUDOKUDRAWX + 60, SUDOKUDRAWY + 120, 60, 60);
  182.  
  183. for (row = 0; row < GRID_WIDTH; row ++) {
  184. for (col = 0; col < GRID_WIDTH; col ++) {
  185.  
  186. //if box is white, make text background white; if gray background - gray text background
  187. fontlib_SetColors(BLACK, WHITE);
  188.  
  189. if ((row / 3 + 1 == 2 && (col / 3 + 1 == 1 || col / 3 + 1 == 3)) ||
  190. (col / 3 + 1 == 2 && (row / 3 + 1 == 1 || row / 3 + 1 == 3)))
  191. fontlib_SetBackgroundColor(GRAY);
  192.  
  193. if (playGrid[row][col] != UNASSIGNED)
  194. {
  195. if (solvedGrid[row][col] != UNASSIGNED)
  196. fontlib_SetForegroundColor(BLUE);
  197.  
  198. fontlib_SetCursorPosition(col*20+SUDOKUDRAWX, row*20+SUDOKUDRAWY);
  199. fontlib_DrawUInt(playGrid[row][col], 1);
  200. }
  201. }
  202. }
  203.  
  204. //Draw the grid lines
  205. gfx_SetColor(BLACK);
  206. for(i = 1; i <= 10; i ++) {
  207. gfx_Line_NoClip(20*i + SUDOKUDRAWX - 20, SUDOKUDRAWY, 20*i + SUDOKUDRAWX - 20, SUDOKUDRAWY + 180);
  208. }
  209. for(j = 1; j <= 10; j ++) {
  210. gfx_Line_NoClip(SUDOKUDRAWX, 20*j + SUDOKUDRAWY - 20, SUDOKUDRAWX + 180, 20*j + SUDOKUDRAWY - 20);
  211. }
  212. }
  213.  
  214. void GenerateSudoku(uint8_t *playGrid, uint8_t solvedGrid[GRID_WIDTH][GRID_WIDTH]) {
  215. uint8_t i, row, col;
  216. for(i = 0; i <= LEVEL; i ++) {
  217. row = randInt(0,8);
  218. col = randInt(0,8);
  219.  
  220. // This makes sure the puzzle follows the same setup as regular Sudoku: rotational symmetry
  221. playGrid[row][col] = solvedGrid[row][col];
  222. playGrid[8-row][8-col] = solvedGrid[8-row][8-col];
  223. }
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement