Ok so I finally managed after many revisions to make a filled sudoku generator. It is not implemented as I originally planned and isn't very well made, but it seems to work, so whatever. Now the task is to make the sudoku generator somehow remove tiles in a way that works with a certain difficulty and has only one unique solution.
Here is the code:
Code:
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <stdio.h>
#include <stdlib.h>
const char* fileLoc = "easyLevels.txt";
FILE *myfile;
int *possibleMoves;
int board[9][9] = { { 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 } };
void initFile() {
if ((myfile = fopen(fileLoc, "w")) == NULL) {
printf("didnt open");
return;
}
}
void closeFile() {
fclose(myfile);
}
void printBoard() {
fprintf(myfile, ",\"");
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
fprintf(myfile, "%d", board[i][j]);
if (j == 8) {
if(i!=8)
fprintf(myfile, "//");
}
else
fprintf(myfile, ",");
}
}
fprintf(myfile, "\" ");
}
void getPossibleMoves(int *possibleMoves[], int x, int y) {
int cpX = x / 3;
cpX *= 3;
int cpY = y / 3;
cpY *= 3;
int cpB[3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++)
cpB[i][j] = board[cpX + i][cpY + j];
}
int psbleMoves[9] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 3; k++) {
if (psbleMoves[i] == cpB[j][k])
psbleMoves[i] = 0;
}
}
for (int j = 0; j < 9; j++) {
if (psbleMoves[i] == board[x][j] || psbleMoves[i] == board[j][y])
psbleMoves[i] = 0;
}
}
int numMoves = 0;
for (int i = 0; i < 9; i++) {
if (psbleMoves[i] != 0)
numMoves++;
}
delete(*possibleMoves);
*possibleMoves = new int[numMoves + 1];//(int*)malloc((numMoves+1)*sizeof(int));
(*possibleMoves)[0] = numMoves;
int prevInt = 1;
for (int i = 0; i < 9; i++) {
if (psbleMoves[i] != 0) {
(*possibleMoves)[prevInt] = psbleMoves[i];
prevInt++;
}
}
}
/*void generateBoard(int i, int j, int k, int h, int f) {
if (i < 81) {
board[i % 9][i / 9] = 0;
int *possibleMoves;
getPossibleMoves(&possibleMoves, i % 9, i / 9);
if (possibleMoves[0] > 0) {
board[i % 9][i / 9] = possibleMoves[rand() % possibleMoves[0] + 1];
delete(possibleMoves);
if (j < 10) {
if (i < h) {
generateBoard(i + 1, j, k - 1, h, f);
}
else {
generateBoard(i + 1, 0, 0, i + 1, 0);
}
}
else {
if (i == h) {
if (f < 9) {
for (int l = h; l > h - k; l--)
board[l % 9][l / 9] = 0;
generateBoard(h - k, 0, k + 1, h, f + 1);
}
else {
for (int l = h; l > h - k-1; l--)
board[l % 9][l / 9] = 0;
generateBoard(h - k - 1, 0, k + 1, h, 0);
}
}
else {
generateBoard(i - 1, 0, k + 1, h, f);
}
}
}
else {
delete(possibleMoves);
generateBoard(i - 1, j+1, k+1, h, f);
}
}
else {
return;
}
return;
}*/
void generateBoard(int i, int j, int k) {
if (i < 81) {
board[i % 9][i / 9] = 0;
getPossibleMoves(&possibleMoves, i % 9, i / 9);
if (possibleMoves[0] > 0) {
board[i % 9][i / 9] = possibleMoves[rand() % possibleMoves[0] + 1];
if (i == k) {
generateBoard(i + 1, 0, i + 1);
}
else {
generateBoard(i + 1, j, k);
}
}
else {
if (i == k) {
for (int l = i; l > i - j - 1; l--)
board[l % 9][l / 9] = 0;
generateBoard(i - j - 1, j + 1, k);
}
else {
board[i % 9][i / 9] = 0;
generateBoard(i - 1, 1, i);
}
}
}
return;
}
int main() {
initFile();
generateBoard(0, 0, 0);
printBoard();
closeFile();
return 0;
}