0

I tried to make the 'Sudoku' game using C language. First, I should randomize all the numbers to be solved in my game, with criteria of none of my numbers I randomized is same. And here is my code

#include<stdio.h>
#include<time.h>
#include<stdlib.h>

int main() {
 int arr_Sudoku[9][9];
 int a, b;
 int fault;
 int i,x;

 srand(time(NULL));
for (a=0; a<9; a++)
    for (b=0; b<9; b++) arr_Sudoku[a][b]=0;

//RANDOMIZE NUMBERS 
 for (a=0; a<9; a++) {
    for (b=0; b<9; b++) {
        do {
            fault=0;
            i=rand()%9+1;

            for (x=0; x<9; x++) {
                if (i==arr_Sudoku[a][x]) fault++;
                if (fault>3) break;
                if (i==arr_Sudoku[x][b]) fault++;
            }               

        } while (fault!=0);

        arr_Sudoku[a][b]=i;
    }
}

for (a=0; a<9; a++) {
    for (b=0; b<9; b++) printf("%d ", arr_Sudoku[a][b]);
    printf("\n"); }

return 0;
 }

Then, I compiled it. But the console showed nothing but black. So I tried to change the code under //RANDOMIZE NUMBERS into this:

for (a=0; a<4; a++)

And the console showed me the randomize code like this:

5 8 2 4 3 7 1 9 6
8 1 4 5 7 9 3 6 2
6 4 1 7 9 8 2 3 5
4 3 5 8 2 1 6 7 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 

It worked ! But I need to randomize 9x9 array. Is there any way to make the randomizing process faster ?

Williams Perdana
  • 169
  • 1
  • 3
  • 16
  • Don't do it like this. You should do it by shuffling. See http://stackoverflow.com/questions/6924216/how-to-generate-sudoku-boards-with-unique-solutions – Bathsheba Dec 10 '15 at 15:01
  • 1
    The algorithm that you are trying to do could well take millions of years to terminate. You need a fundamentally different approach. – John Coleman Dec 10 '15 at 15:01

0 Answers0