* Login 
* FAQ    * Search

All times are UTC - 6 hours




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Sudoku problem..
PostPosted: Wed Aug 26, 2009 1:52 pm 
Offline

Joined: Thu Jan 08, 2009 6:40 am
Posts: 3
Location: Germany-Cologne
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

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...


Top
 Profile  
 
 Post subject: Re: Sudoku problem..
PostPosted: Sun Aug 30, 2009 3:03 am 
Offline
User avatar

Joined: Tue Dec 19, 2006 12:41 pm
Posts: 641
Your problem appears to be with this piece of code:
Code:
                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:
        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
Nonelectronics <- 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


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group