Sudoku problem..

Talk about your favorite PC games, Steam and building awesome custom rigs!

Moderator:Moderators

Post Reply
Gr4vity!
Posts:3
Joined:Thu Jan 08, 2009 6:40 am
Location:Germany-Cologne
Sudoku problem..

Post by Gr4vity! » Wed Aug 26, 2009 1:52 pm

Hello guy´s

I´ve a problem with the implementation of Sudoku in c#....

i wrote 2 for-loops, one that tests the collums, one that tests the rows if the numbers 1-9 are unique in a row/col... The 2 Loops are working perfect, but only if i use ONE of them.. The 2 loops are not working together , I think it´s a Overflow...
I try to interlace the for-loops, but i´m with stupid.. I have to ask for help in this forum

here is the rs-link for my program
http://rapidshare.com/files/271924188/sudoku.cs.html" onclick="window.open(this.href);return false;

Thanks for help, members of coding...
I´m sure, its only a LITTLE 1-MINUTE problem 4 you...

Thank again..^^
The "Whole" is more than the sum of its parts...

User avatar
nitro2k01
Posts:651
Joined:Tue Dec 19, 2006 12:41 pm

Re: Sudoku problem..

Post by nitro2k01 » Sun Aug 30, 2009 3:03 am

Your problem appears to be with this piece of code:

Code: Select all

                while (check_position(i) == 0)
                {
                    this.values_right[i] = (this.values_right[i] + 1) % 10;

                }
This loop will keep going as long as check_position(i) equals 0. And the statement in the loop obviously tries one value at a time. To see which one is correct. The problem is that in a regular sudoku, unless it's retarded kid level, most positions won't have enough information to determine the number that is supposed to go into that position. In that case your script will think that all values are wrong, and your loop will go on forever. Not to mention the fact that you're looping through one value too much, the 0 value.

Try something like this:

Code: Select all

        private int[] posible_values = new int[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
...
            int j;
            if(this.values_right[i] == 0){
                for (j = 0; j < 10; j++)
                {
                    this.values_right[i] = posible_values[j];
                    if(check_position(i) != 0)
                        break;
                }
            }

This code goes through the values 1-9, or until check_position(i) returns something but 0. Notice that I expanded posible_values array to fit a last value of zero (which is what you use to represent an empty box, no?) to reset that square in case all 9 values fail. The if checks that the box isn't currently filled in.

Then you'd have to iterate through the 81 squares until you find a solution, or until the algorithm fails. (Which is when no new correct numbers were filled in during when looping through the 81 numbers)

But even then your algorithm will only be able to solve very simple sudoku.
My blog
ASM Retro <- Gameboy Classic Backlight

Being the sadistic bastard I am, I have covered Frog's left eye with a Santa hat.

Last edited by nitro2k01 tomorrow, 1:48 pm; edited 1 time in total

Post Reply