Sudoku Programmers Forum Index

 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister   ProfileProfile   Log inLog in          Games  Calendar

Log in to check your private messagesLog in to check your private messages   

SuDoKu Generator

 
Post new topic   Reply to topic    Sudoku Programmers Forum Index -> Programming sudoku
View previous topic :: View next topic  
Author Message
Jonauz

Joined: 15 Jan 2008
Posts: 3
:
Location: Lithuania

Items
PostPosted: Tue Jan 15, 2008 3:08 pm    Post subject: SuDoKu Generator Reply with quote

Hi everyone..
I tryed to make my one sudoku puzle generator. I don't know why, but it still not work.

I have seen that there is a lot of explaining about sudoku. Also tips, hints and so on.. But haven't found any normal source that would work, and it would be easy to understant for thouse who only trying to creat one.

So, hellp me in this. I'm a student, and tried to do SuDoKu, to get a better mark..
Back to top
View user's profile Send private message
Jonauz

Joined: 15 Jan 2008
Posts: 3
:
Location: Lithuania

Items
PostPosted: Tue Jan 15, 2008 3:12 pm    Post subject: Here is a code of my work: Reply with quote

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Random rnd = new Random();
int[,] bord = new int[100, 100];
int sk, salyga = 0, operacijuSK=0, netikraSal=0;

#region Kvadratelis
do
{
#region
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
bord[i, j] = 0;
}
}
#endregion
for (int x1 = 0; x1 < 3; x1++)
{
for (int y1 = 0; y1 < 3; y1++)
{
for (int i = x1 * 3; i < x1 * 3 + 3; i++)
{
for (int j = y1 * 3; j < y1 * 3 + 3; j++)
{
operacijuSK = 0;
do
{
sk = rnd.Next(1, 10);
operacijuSK += 1;
//System.Console.WriteLine(sk);
#region ROW
for (int b = 0; b < 9; b++)
{
if (sk == bord[i, b])
{
b = 10;
salyga = 0;
}
else salyga = 1;
}
#endregion
#region SQUARE
if (salyga == 1)
{
for (int x = x1 * 3; x < x1 * 3 + 3; x++)
{
for (int y = y1 * 3; y < y1 * 3 + 3; y++)
{
if (sk == bord[x, y])
{
x = 100;
y = 100;
salyga = 0;
}
else salyga = 1;
}
}
}
#endregion
#region COLUM
if (salyga == 1)
{
for (int a = 0; a < 9; a++)
{
if (sk == bord[a, j])
{
a = 10;
salyga = 0;
}
}
}
#endregion
#region
if (operacijuSK == 100)
{
System.Console.WriteLine(" 100 !");
salyga = 1;
netikraSal = 1;
x1 = 3;
y1 = 3;
i = 9;
j = 9;
}
else netikraSal = 0;
#endregion
}
while (salyga == 0);
bord[i, j] = sk;
//Console.Write(bord[i, j]);

}
//Console.WriteLine();
}

#region
for (int x = 0; x < 9; x++)
{
for (int y = 0; y < 9; y++)
{
System.Console.Write(bord[x, y]);
}
System.Console.WriteLine();
}
System.Console.WriteLine();
#endregion
}
}
} while (netikraSal == 1);
#endregion

}
}
}
Back to top
View user's profile Send private message
Ruud
Site Admin
Joined: 17 Sep 2005
Posts: 708
:
Location: Netherlands

Items
PostPosted: Tue Jan 15, 2008 6:30 pm    Post subject: Reply with quote

Hi,

there are a few problems in your code:

- the variable names are in Lithuanian Wink

- you're mixing up the dimensions, a Sudoku 9x9 requires an array bord = new int[9,9] and you should compare the iterator (operacijuSK == 81)

- regions are nice to structure large code files, but here you would probably get better marks by reorganizing your code into a few functions. It would certainly make the code more readable.

- Finally, the most important issue is that your generator has no backtracking. When you operate in forward mode only, at some point the program will hang because no number is allowed in the next cell. Backtracking allows you to take a step back if the generator gets stuck. To implement backtracking, you need a control structure other than your for loops. Also, you need to keep track of the sk (values) you already tried, so you know when all possibilities have been exhausted. An alternative to backtracking is a complete restart, but that is much slower.

Even with these changes, the program will be slow, because it will often have to backtrack or restart.

Ruud
_________________
Meet me at sudocue.net
Back to top
View user's profile Send private message Visit poster's website
Jonauz

Joined: 15 Jan 2008
Posts: 3
:
Location: Lithuania

Items
PostPosted: Wed Dec 02, 2009 11:55 pm    Post subject: Reply with quote

Thanks for your answer Wink

So if my code is not good enough, maybe there is some king of free source generator, that I could get, and learn from it?
Back to top
View user's profile Send private message
wapati

Joined: 12 Jun 2007
Posts: 622
:
Location: Canada

Items
PostPosted: Thu Dec 03, 2009 7:36 am    Post subject: Reply with quote

Jonauz wrote:
Thanks for your answer Wink

So if my code is not good enough, maybe there is some king of free source generator, that I could get, and learn from it?


Try the code from openoffice. Very good source.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Sudoku Programmers Forum Index -> Programming sudoku All times are GMT
Page 1 of 1

 
Jump to:  
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 vote in polls in this forum
Sudoku Programmers topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group

Igloo Theme Version 1.0 :: Created By: Andrew Charron