I have just started making my Sudoku game and I have made this function grid for creating a 6x6 Sudoku grid. I have used the rand() function for different numbers in each cell (currently it will only check rows for repetition of numbers). rand() is also used for random numbers of empty cells in each grid.
The problem is that sometimes the grid is perfect 6x6 and without any number repeating (in rows only), however, sometimes in some cells there are garbage values generated and sometimes the number of columns is increased. I don't understand what is causing the problem?
The Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void grid(void) {
int cell[6][6], row, col, s, i, j;
char in = 'A';
srand(time(NULL));
for (row = 0; row <= 5; row++) {
printf("\t\t\t[ |");
for (col = 0; col <= 5; col++) {
s = rand() % 6 + 1;
if (s % 2 == 0)
{
cell[row][col] = rand() % 6 + 1;
for (j = 0; j<col; j++) {
if (cell[row][j] == cell[row][col]) {
col--;
continue;
}
}
}
else { printf(" | ", in++); continue; }
printf(" %d | ", cell[row][col]);
}
printf("]\n\n");
}
}
int main()
{
grid();
}