Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdbool.h>
- #include <stddef.h>
- #include <stdint.h>
- #include <tice.h>
- #include <math.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <graphx.h>
- #include <keypadc.h>
- #include <fileioc.h>
- #include <fontlibc.h>
- #include "font/font.h"
- #define BLACK 0
- #define BLUE 24 //INSET numbers - numbers you can't change
- #define GRAY 222 //Pause menu background?
- #define RED 224
- #define WHITE 255
- #define SUDOKUDRAWX 20 //Draw Position of Sudoku Grid.
- #define SUDOKUDRAWY 46
- #define UNASSIGNED 0 //Unassigned locations will be labeled as a 0
- #define GRID_WIDTH 9 //Width (and height) of the Sudoku Puzzle grid
- #define BOX_WIDTH 3 //Size of each "box" in the grid
- void menu(void);
- void play(void);
- bool FindUnassignedLocation(uint8_t grid[GRID_WIDTH][GRID_WIDTH], uint8_t *rowp, uint8_t *colp);
- bool isSafe(uint8_t grid[GRID_WIDTH][GRID_WIDTH], uint8_t row, uint8_t col, uint8_t num);
- void randSeq(uint8_t *seq);
- bool SolveSudoku(uint8_t *grid);
- bool UsedInBox(uint8_t grid[GRID_WIDTH][GRID_WIDTH], uint8_t boxStartRow, uint8_t boxStartCol, uint8_t num);
- bool UsedInCol(uint8_t grid[GRID_WIDTH][GRID_WIDTH], uint8_t col, uint8_t num);
- bool UsedInRow(uint8_t grid[GRID_WIDTH][GRID_WIDTH], uint8_t row, uint8_t num);
- void displayGrid(uint8_t playGrid[GRID_WIDTH][GRID_WIDTH], uint8_t solvedGrid[GRID_WIDTH][GRID_WIDTH]);
- void GenerateSudoku(uint8_t *playGrid, uint8_t solvedGrid[GRID_WIDTH][GRID_WIDTH]);
- void main()
- {
- srand(rtc_Time());
- fontlib_SetWindowFullScreen();
- fontlib_SetFont(font20, 0);
- menu();
- }
- void menu(void)
- {
- gfx_Begin();
- play();
- gfx_End();
- }
- void play(void) {
- int row, col;
- uint8_t playGrid[GRID_WIDTH][GRID_WIDTH] = {
- {0,0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,0,0,0}
- };
- uint8_t solvedGrid[GRID_WIDTH][GRID_WIDTH] = {
- {0,0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,0,0,0}
- };
- SolveSudoku(&solvedGrid);
- GenerateSudoku(playGrid, &solvedGrid);
- displayGrid(playGrid, solvedGrid);
- while (!os_GetCSC());
- }
- bool FindUnassignedLocation(uint8_t grid[GRID_WIDTH][GRID_WIDTH], uint8_t *rowp, uint8_t *colp) { //row pointer; column pointer
- uint8_t row, col;
- for (row = 0; row < GRID_WIDTH; row ++)
- for (col = 0; col < GRID_WIDTH; col ++)
- if (grid[row][col] == UNASSIGNED) {
- *rowp = row;
- *colp = col;
- return true;
- }
- return false;
- }
- bool isSafe(uint8_t grid[GRID_WIDTH][GRID_WIDTH], uint8_t row, uint8_t col, uint8_t num) {
- return !UsedInRow(grid, row, num) &&
- !UsedInCol(grid, col, num) &&
- !UsedInBox(grid, row - row%BOX_WIDTH, col - col%BOX_WIDTH, num) &&
- grid[row][col] == UNASSIGNED;
- }
- void randSeq(uint8_t *seq) {
- uint8_t a, b, i, rand;
- for(i = 0; i <= 8; i ++) {
- rand = randInt(0,8);
- a = seq[0];
- b = seq[rand];
- seq[0] = b;
- seq[rand] = a;
- }
- }
- bool SolveSudoku(uint8_t *grid) {
- uint8_t row = 0, col = 0, num, sequence;
- uint8_t seq[9] = {1,4,7,3,6,9,2,5,8}; //used for generating puzzles
- if (!FindUnassignedLocation(grid, &row, &col)) //No unassigned locations = solved
- return true;
- randSeq(seq);
- for (sequence = 0; sequence <= N - 1; sequence ++) {
- num = seq[sequence];
- if (isSafe(grid, row, col, num)) {
- grid[row][col] = num;
- if (SolveSudoku(grid))
- return true; //woohoo! We solved it!
- grid[row][col] = UNASSIGNED; //failure, unmake & try again
- }
- }
- return false; // this triggers backtrackiing
- }
- bool UsedInBox(uint8_t grid[GRID_WIDTH][GRID_WIDTH], uint8_t boxStartRow, uint8_t boxStartCol, uint8_t num) {
- uint8_t row, col;
- for (row = 0; row < BOX_WIDTH; row ++)
- for (col = 0; col < BOX_WIDTH; col ++)
- if (grid[row + boxStartRow][col + boxStartCol] == num)
- return true;
- return false;
- }
- bool UsedInCol(uint8_t grid[GRID_WIDTH][GRID_WIDTH], uint8_t col, uint8_t num) {
- uint8_t row;
- for (row = 0; row < GRID_WIDTH; row ++)
- if (grid[row][col] == num)
- return true;
- return false;
- }
- bool UsedInRow(uint8_t grid[GRID_WIDTH][GRID_WIDTH], uint8_t row, uint8_t num) {
- uint8_t col;
- for (col = 0; col < GRID_WIDTH; col ++)
- if (grid[row][col] == num)
- return true;
- return false;
- }
- void displayGrid(uint8_t playGrid[GRID_WIDTH][GRID_WIDTH], uint8_t solvedGrid[GRID_WIDTH][GRID_WIDTH]) {
- uint8_t row, col;
- uint8_t i, j;
- //Make every other box gray
- gfx_SetColor(GRAY);
- gfx_FillRectangle_NoClip(SUDOKUDRAWX + 60, SUDOKUDRAWY, 60, 60);
- gfx_FillRectangle_NoClip(SUDOKUDRAWX, SUDOKUDRAWY + 60, 60, 60);
- gfx_FillRectangle_NoClip(SUDOKUDRAWX + 120, SUDOKUDRAWY + 60, 60, 60);
- gfx_FillRectangle_NoClip(SUDOKUDRAWX + 60, SUDOKUDRAWY + 120, 60, 60);
- for (row = 0; row < GRID_WIDTH; row ++) {
- for (col = 0; col < GRID_WIDTH; col ++) {
- //if box is white, make text background white; if gray background - gray text background
- fontlib_SetColors(BLACK, WHITE);
- if ((row / 3 + 1 == 2 && (col / 3 + 1 == 1 || col / 3 + 1 == 3)) ||
- (col / 3 + 1 == 2 && (row / 3 + 1 == 1 || row / 3 + 1 == 3)))
- fontlib_SetBackgroundColor(GRAY);
- if (playGrid[row][col] != UNASSIGNED)
- {
- if (solvedGrid[row][col] != UNASSIGNED)
- fontlib_SetForegroundColor(BLUE);
- fontlib_SetCursorPosition(col*20+SUDOKUDRAWX, row*20+SUDOKUDRAWY);
- fontlib_DrawUInt(playGrid[row][col], 1);
- }
- }
- }
- //Draw the grid lines
- gfx_SetColor(BLACK);
- for(i = 1; i <= 10; i ++) {
- gfx_Line_NoClip(20*i + SUDOKUDRAWX - 20, SUDOKUDRAWY, 20*i + SUDOKUDRAWX - 20, SUDOKUDRAWY + 180);
- }
- for(j = 1; j <= 10; j ++) {
- gfx_Line_NoClip(SUDOKUDRAWX, 20*j + SUDOKUDRAWY - 20, SUDOKUDRAWX + 180, 20*j + SUDOKUDRAWY - 20);
- }
- }
- void GenerateSudoku(uint8_t *playGrid, uint8_t solvedGrid[GRID_WIDTH][GRID_WIDTH]) {
- uint8_t i, row, col;
- for(i = 0; i <= LEVEL; i ++) {
- row = randInt(0,8);
- col = randInt(0,8);
- // This makes sure the puzzle follows the same setup as regular Sudoku: rotational symmetry
- playGrid[row][col] = solvedGrid[row][col];
- playGrid[8-row][8-col] = solvedGrid[8-row][8-col];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement