You will find that filling a Sudoku grid with random numbers is not going to cut it. Yes they're random, but they'll also repeat... violating the Sudoku rules.
You will find that filling a Sudoku grid with random numbers is not going to cut it. Yes they're random, but they'll also repeat... violating the Sudoku rules.
I don't think so, because arrays start at 0.
I've solved the problem of the large numbers with this:
[code]
if (sudoku[a][b]>9)
{
sudoku[a][b]=0;
}
[\code]
But the dimensions of the produced grid is still fluctuating.
ED: I know. I've noticed that. But I can fix that. This is my current code:I am getting weird outputs, like [ ][ ][9] or [6][6]. Never more than one row.Code:#include <stdio.h> #include <stdlib.h> #include <time.h> int check(int* x[8][8]); int main() { int sudoku[8][8] = {0}; int i; int a; int b=0; int n; int m=0; int p; srand(time(NULL)); for(a=0;a<9;a++) { p=0; for(;b<9;b++) { p=0; i=rand() %20; if (i<10 && i!=0) { sudoku[a][b]=i; } if (sudoku[a][b]>9) { sudoku[a][b]=0; } if (sudoku[a][b]!=0) { for (n=a;n<9;n++) { if(n==a) {continue;} if(sudoku[a][b]==sudoku[n][b]) { m=1; break; } } if (m==1) { b--; p=1; } for (n=a;n>=0;n--) { if(n==a) {continue;} if(sudoku[a][b]==sudoku[n][b]) { m=1; break; } } if (m==1 && p==0) { b--; p=1; } for (n=b;n<9;n++) { if(n==b) {continue;} if(sudoku[a][b]==sudoku[a][n]) { m=1; break; } } if (m==1 && p==0) { b--; p=1; } for (n=b;n>=0;n--) { if(n==b) {continue;} if(sudoku[a][b]==sudoku[a][n]) { m=1; break; } } if (m==1 && p==0) { b--; p==1; } } if (sudoku[a][b]==0 && p==0) { printf("[ ]"); } else { if (p==0) { printf("[%d]", sudoku[a][b]); } } p=0; } printf("\n"); } getchar(); return 0; }
Last edited by mortalc; 03-12-2011 at 04:46 AM.
I've edited my last post to show my full code.
Are you trying to generate a Sudoku puzzle grid?
That is harder than solving the puzzle, because:
1) lots of grids that look OK originally, just can't be a valid puzzle grid. They lack the required characteristics to be a Sudoku puzzle.
2) Real Sudoku fans don't consider puzzle grids with multiple solutions, to be valid.
You can download tens of thousands of valid Puzzle grids, so generating your own grids is not efficient.
A lot of posts were lost in a recent virus attack, but these guys & gals have forgotten more about Sudoku programming, than most people will ever learn:
Sudoku Programmers :: Index
Last edited by Adak; 03-12-2011 at 06:20 AM.
Hrm... Maybe you're right....
How about this. Say I'm going for a solver. Basically this is my idea:
1. Open Xcel, or another grid program (I think I can do this with fopen())
2. User writes in the starting numbers into the first 81 squares (from A1 - I9).
3. After a key press or something, the values are transferred to sudoku[9][9] (yes I realized my mistake)
4. It is solved (I can do this bit).
5. The solution is printed (I can do this) or opened as an Xcel or another grid program (preferable, not sure how to do this.)