0
votes

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();
}
2
The Pico indentation style you're using may be fine for Pico, but C is not Pico. Please use an orthodox C indentation style — I prefer Allman but many people prefer some variant of 1TBS. Stick with one or the other of those and you'll not go far wrong. - Jonathan Leffler
In a 6x6 Sudoku board, you normally have 6 sub-cells of 2 rows and 3 columns, and you have to preserve the uniqueness of the 6 numbers in each sub-cell as well as in each line and each column. Your code doesn't seem to be doing enough to match those criteria. In a 9x9 board, you'd have 9 subcells, each of size 3x3, with the numbers unique in each subcell as well as in each row and column. I wonder if you'd do better with randomly permuting an array of 6 items? (I've not coded a Sudoku generator; I don't know the best way to do it. But there are almost 700 questions on SO tagged sudoku!) - Jonathan Leffler

2 Answers

0
votes

They are too many syntax errors in the code you post, and the format is quite horrible. Try editing it so we can help you !

col--; Maybe this is your problem, because if you col--; in your for (col = 0; col < 6; col++) loop, you will do more than 6 iterations.

0
votes

I know this question is old, but a commenter here did say to wait at least 1 hour...

To answer your question directly:

  1. You get garbage values because you do not set values for cell when !(s % 2 == 0).
  2. You get more than 6 entries per row because your c-- statement causes the col loop to execute more than 6 times (and you printf each time).

If you want to maintain the general structure of your logic, then do this in two passes - fill cell completely and then print the whole thing. However, there are other problems. You probably want to remove the s % 2 == 0 check completely, and the first continue could be a break (or a goto if you really want to make people angry).