After a lot of trial & errors, I've managed to create an array as follow :

xxx xxx xxx
xxx 000 000
xxx 000 000

x00 000 000
x00 000 000
x00 000 000

x00 000 000
x00 000 000
x00 000 000

Now, when a user enters a value in any of the 0, I'll need to check to make sure that there isn't any repetition within the same row, column and box.

Can someone give me a basic idea on how to check the boxes and what did I do wrong with my code while checking the row & column?(The checking part is done is the void user_input( int ar[arraySize][arraySize]) function).

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

const int arraySize = 9;


void initarrays( int ar[arraySize][arraySize]);         //to initilize the first box of the array
void scramble( int ar[arraySize][arraySize]);           //to scramble the first box of the array
void initarrays_row( int ar[arraySize][arraySize]);     //to initilize the first row of the array
void scramble_row( int ar[arraySize][arraySize]);       //to scramble the first row of the array
void initarrays_column( int ar[arraySize][arraySize]);  //to initilize the first column of the array
void scramble_col( int ar[arraySize][arraySize]);       //to scramble the first column of the array
void showgrid( int ar[arraySize][arraySize]);           //printing the array
void user_input( int ar[arraySize][arraySize]);         //user inputting a value into a blank array


int main ()
{
        srand( time( NULL ) );
        int grid[9][9]={};


        initarrays( grid);

        printf("Starting grid:\n");
        showgrid(grid);

        scramble( grid);

        initarrays_row( grid);
        scramble_row( grid);

        initarrays_column( grid);
        printf("\nScrambled grid:\n");

        showgrid( grid);

        user_input( grid);
        showgrid( grid);

}

void user_input( int ar[arraySize][arraySize])
{
   int x, y, num,row,col;
   int check_x;
   int check_y;
   int count = -1;

        printf("Please enter the x axis :");
        scanf("%d", &x);
        printf("\nPlease enter the y axis :");
        scanf("%d", &y);
        printf("\nPlease enter the number :");
        scanf("%d", &num);

    printf("\n\nx=%d\t\ty=%d",x,y);

    for(row=0; row<9; col++){
       for(col=0; col<9; col++){
            check_x = ar[row][y];
            check_y = ar[x][col];
            if(check_x == num || check_y == num)
            {
            count++;
            }
       }
            if(count<=0)
            {
                ar[x][y]=num;

                printf("\n%d\n",ar[x][y]);
                break;
            }
    }




}

void initarrays( int ar[arraySize][arraySize]) //to initilize the first box of the array
{
        int num = 1;
        int row, col;
        for (row = 0; row < 3; row++) {
                for (col = 0; col < 3; col++) {
                        ar[row][col] = num;
                        num++;
                }
        }
}

void scramble( int ar[arraySize][arraySize])     //to scramble the first box of the array
{
        int tmp, swprw, swpcl, row, col;
        for (row = 0; row < 3; row++) {
                for (col = 0; col < 3; col++) {
                        swprw = rand() % 3;
                        swpcl = rand() % 3;
                        tmp = ar[swprw][swpcl];
                        ar[swprw][swpcl] = ar[row][col];
                        ar[row][col] = tmp;
                }
        }
}

void initarrays_row( int ar[arraySize][arraySize])  //to initilize the first row of the array
{
        int row, col, newcol=3;
        int temp;
        for (row = 1; row < 3; row++) {
                for (col = 0; col < 3; col++){
                        temp = ar[row][col];
                        ar[0][newcol] = temp;
                        newcol++;
                }
        }
}

void scramble_row( int ar[arraySize][arraySize])     //to scramble the first row of the array
{
        int tmp,swpcl,col;
                for (col = 3; col < 9; col++) {
                    swpcl = rand() % 9;
                        if(swpcl!=0 && swpcl!=1 && swpcl!=2)
                        {
                        tmp = ar[0][swpcl];
                        ar[0][swpcl] = ar[0][col];
                        ar[0][col] = tmp;
                        }
                }

}

void initarrays_column( int ar[arraySize][arraySize])   //to initilize the first column of the array
{
        int row, col, newrow=3;
        int temp;
        for (row = 0; row < 3; row++) {
                for (col = 1; col < 3; col++){
                        temp = ar[row][col];
                        ar[newrow][0] = temp;
                        newrow++;
                }
        }
}

void scramble_col( int ar[arraySize][arraySize])        //to scramble the first column of the array
{
        int tmp, swprw, row;
                for (row = 3; row < 9; row++) {
                    swprw = rand() % 9;
                        if(swprw!=0 && swprw!=1 && swprw!=2)
                        {
                        tmp = ar[swprw][0];
                        ar[swprw][0] = ar[row][0];
                        ar[row][0] = tmp;
                        }
                }

}

void showgrid( int ar[arraySize][arraySize])    //printing the array
{
        int row, col;
        for (row = 0; row < 9; row++) {
                for (col = 0; col < 9; col++) {
                        printf("%d",ar[row][col]);
                if(col==8)
                printf("\n");
                if(col==2 || col==5)
                printf(" ");
                if(row==2 && col == 8 || row==5 && col == 8)
                printf("\n");
                }

        }
}

Recommended Answers

All 8 Replies

The method used by you to check if the num exists in that particular row or column is correct (though inefficient )

Checking if the number exists in that box is a bit tricky ..... You want some thing of this sort

// All the boxes are 3 * 3 in size
When you input the row number there are 3 possibilities .

Possibility 1: Row_num is 1, 4,7 
You identify these cases because row_num % 3 produces the same result ie 1. If the row_num is in this case you have to consider the rows,
row_num, row_num +1, row_num +2
 
Possibility 2: Row_num is 2, 5,8 
You identify these cases because row_num % 3 produces the same result ie 2. If the row_num is in this case you have to consider the rows,
row_num-1, row_num , row_num+1

Possibility 3: Row_num is 3, 6,9 
You identify these cases because row_num % 3 produces the same result ie 0. If the row_num is in this case you have to consider the rows,
row_num-2, row_num-1 , row_num 

You do the same thing for col_num

Once you get the beginning and the ending of the rows and columns... Then it is easy

for(row_begin........ row_end)
{
     for(col_begin ........ col_end)
      {
             // Do the checking here

       }
}
commented: yeah, i like this guy. +7
commented: Great guy, been a great help and has great patience. :) +1

The method used by you to check if the num exists in that particular row or column is correct (though inefficient )

Checking if the number exists in that box is a bit tricky ..... You want some thing of this sort

Hmm, let me clarify something first. Your saying that the method I used to check is correct? How come I'm not getting a proper output? Sorry if I'm getting on anyone's nerves. Kinda nervous considering I'm short on time for this. All help is appreciated(Been spending almost 24 hours in total on this program). :(

In the sudoku game you have to check 3 things .
1. The number exists in the row
2. The number exists in the column
3. The number exists in the box

You have written the code for the first 2 conditions...
In my previous post I have given you the pseudo code to check for the 3rd condition

This show one way of checking a peer group (row, column, or box), in Sudoku. (At least, it's a version that will make sense to you. The faster versions are rather opaque.)

int TestRows(int nudigit, int r)	{
	int c = 0;

	while(nudigit != A[r][++c] && c < 10);				  
	if(c < 10)
		return 0;  //nudigit failed
	return 1;

}	
/* adjust this to fit your own matrix */

The way it works is you only test one row at a time - if the nudigit fails, then there's no need to test any other rows, and no columns or boxes will be tested at all.

I haven't got a chance to study your code yet, but at first glance, it looks like you have a simple error, so don't gut it out and replace it with this code, until I get back and have some time to study it.

Box code requires the starting row and column and an small bit of arithmetic is all. More on that, later also. There are 9 boxes, and a small array tells you what boxNum fits which sqr. (a sqr is row * 9 + column), except the first row is just column).

switch (boxNum)
case 1: lrow = 0; lcol = 0; etc.

kind of a thing. I'll explain it later this evening.

Sudoku is an amazingly complex puzzle, despite it's simple outward appearance. The logical solving solutions are extremely subtle once you get past naked singles and other obvious methods.

Thanks for all the replies! :)

Anyway, I've found out that the error in my previous code was a very very obvious one but somehow I missed it.

for(row=0; row<9; col++){
       for(col=0; col<9; col++){

I wrote col++ instead of row++.


Now I've written the code to check for the boxes and it works. Probably my last question, how do I make the program repeat the input function until all the arrays are filled with non-zero?

Do I use a for loop that goes to all the 9 rows and 9 columns and use a counter to check it or is there some better approach?

for(row_box = row_begin ; row_box<=row_end ; row_box++){
            for(col_box = col_begin ; col_box<=col_end ; col_box++){
                check_box_x = ar[row_box][col_begin];
                check_box_y = ar[row_begin][col_box];
                if(check_box_x == num || check_box_y == num)
                {
                count++;
                break;
                ar[row_box][col_box] = 0;
                }

           }
        }

Sudoku is a 9*9 matrix ... That means there will be 81 entries in all.
Out of these 81 entries some of these entries are filled when the grid is presented to the user for the first time. Let this number be temp.

So now for the sudoku box to be complete the user has to correctly enter 81- temp entries.
So the check to terminate the game will be when the user has entered 81- temp correct entries .

Lots of nice ways to handle this. A simple approach is just a counter:

int unsolved = 81 - number of given numbers

Then when you solve another square: unsolved--;

That gives you a simple

while (unsolved) {

/* all your other code in here */

}

Your program looks very different than other Sudoku programs that I've seen.

Very interesting - no mention of "possible" numbers for any square, and no customary solving logic, either.

I'm very curious what the solving times for this will be!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.