Created
August 25, 2022 01:11
-
-
Save Cicolas/8a79f2d7062e3af8416ce72610e6cd5f to your computer and use it in GitHub Desktop.
sudoku cli
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Nícolas dos Santos Carvalho | |
Maringá, PR - Brasil | |
24/08/2022 | |
Sudoku CLI feito por Nícolas dos Santos Carvalho | |
https://github.com/Cicolas | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <time.h> | |
int dif = 3; | |
typedef struct { | |
int **fullMap; | |
int **gameMap; | |
} sudokuMap; | |
sudokuMap createMap() { | |
sudokuMap sm; | |
sm.fullMap = malloc(sizeof(int*)*9); | |
sm.gameMap = malloc(sizeof(int*)*9); | |
for (int i = 0; i < 9; i++) { | |
sm.fullMap[i] = malloc(sizeof(int)*9); | |
sm.gameMap[i] = malloc(sizeof(int)*9); | |
for (int j = 0; j < 9; j++) { | |
sm.fullMap[i][j] = 0; | |
sm.gameMap[i][j] = 0; | |
} | |
} | |
return sm; | |
} | |
void fillMap(sudokuMap* obj) { | |
for (int i = 0; i < 9; i++) { | |
for(int j = 0; j < 9; j++) { | |
int jmod = j%3, | |
imod = i%3, | |
jqad = j/3, // quad j | |
iqad = i/3; // quad i | |
int n = 0; | |
n = (jmod+iqad)%3 + 3*imod; | |
n = ((n+jqad)%3) + 3*((imod+jqad)%3); | |
// TODO: rotacionar e randomizar mais | |
obj->fullMap[i][j] = n+1; | |
int randomN = (rand()%100)>(100-(dif*25)); | |
obj->gameMap[i][j] = randomN?n+1:0; | |
} | |
} | |
} | |
sudokuMap map; | |
void load() { | |
srand(time(NULL)); | |
map = createMap(); | |
fillMap(&map); | |
// printf("\n"); | |
// for (int i = 0; i < 9; i++) { | |
// for (int j = 0; j < 9; j++) { | |
// printf("%s%i ", (j%3)==0?" ":"", map.fullMap[i][j]); | |
// } | |
// printf("%s\n", (i%3)==2?"\n":""); | |
// } | |
printf("\n\tS U D O K U\n\t+-------+-------+-------+\n\t"); | |
for (int i = 0; i < 9; i++) { | |
for (int j = 0; j < 9; j++) { | |
int n = map.gameMap[i][j]; | |
char s[10] = " "; | |
if (n > 0) | |
itoa(n, s, 10); | |
printf("%s%s ", (j%3)==0?"| ":"", s); | |
} | |
printf("%s\n\t", (i%3)==2?"|\n\t+-------+-------+-------+":"|"); | |
} | |
free(*map.fullMap); | |
free(map.fullMap); | |
printf("\t cicolas\n\n"); | |
} | |
int main(void) { | |
load(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment