Hi
. I am currently working on sudoku for the ti nspire, but I only have 2 levels for it right now, and don't want to post it until at least a good bit of levels have been made. I want to use a custom level generator,which outputs a file, the contents of which I will copy and past into my lua script for the game, or it that I started writing in c(well c++ technically, as I use the new and delete keywords cause im lazy), but I have little clue as to how to generate random sudoku generators. My first attempt at writing a complete sudoku board(withiout all the numbers missing in a puzzle, and that is something else I need help with) compeletely doesn't work, and I still am not sure as to how to improve it. I know why it doesn't work, but I have zero idea as to how to do it better. Anyways, here is my starting code for the generator. I am still going to try to fix it myself, but it would be great if someone could help me create a legit sudoku board generator.
Here is my code, which is horrible(and isnt random yet, it just uses the first value it can), but anyways:
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <stdio.h>
#include <stdlib.h>
const char* fileLoc = "C:\\Users\\Andrew\\vs\\sudoku\\easyLevels.txt";
FILE *myfile;
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++;
}
*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() {
for (int i = 0; i < 9; i++)
board[0][i] = i+1;
for (int i = 1; i < 9; i++) {
for (int j = 0; j < 9; j++)
board[i][j] = 0;
}
int prevMove;
int *possibleMoves=new int[10];
int *prevMoves=new int[10];
for (int i = 1; i < 9; i++) {
for (int j = 0; j < 9; j++) {
prevMove = 0;
getPossibleMoves(&possibleMoves, i, j);
while (possibleMoves[0] == 0) {
prevMove++;
if (prevMoves[0] < prevMove) {
//generateBoard();
return;
}
else {
if (j != 0)
board[i][j-1] = prevMoves[prevMove + 1];
else
board[i - 1][8] = prevMoves[prevMove+1];
getPossibleMoves(&possibleMoves, i, j);
}
}
board[i][j] = possibleMoves[1];
prevMove = 0;
delete(prevMoves);
prevMoves = possibleMoves;
}
}
delete(possibleMoves);
}
int main() {
initFile();
generateBoard();
printBoard();
closeFile();
return 0;
}
By the way the output is ,"1,2,3,4,5,6,7,8,9//4,5,6,1,2,7,-33686019,-33686019,3//7,8,9,-33686019,-33686019,3,1,2,4//2,1,4,3,6,5,8,9,7//3,6,5,2,1,8,-33686019,-33686019,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"
so it doesn't finish and it doesn't have proper values at all.
Attached are what that looks like on the calculator(the really low negative values mess it up a lot), and an actual level. This is not a showcase of how the final game will look like, although I will not change it, but it just shows how the string converts to the look on the screen.
Btw here is the string for the working level that is attached: "1,0,0,0,0,0,4,8,0//5,0,0,3,0,0,0,0,2//2,0,9,0,0,4,3,5,0//8,0,6,0,5,0,0,4,3//4,2,3,0,0,0,5,7,0//0,1,5,4,0,0,9,0,0//0,0,0,0,8,0,0,0,9//6,5,0,0,9,3,0,0,4//0,8,2,6,4,0,0,0,5"
As you may be able to tell, the commas divide each element of a top down ninth of the board, and the //shes divide the top down pieces.
As you may also be able to tell, even the piece of the sudoku board that IS generated is not a legit sudoku board.