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   

Requesting help: Excel VBA Sudoku Grid Generator

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

Joined: 06 Nov 2005
Posts: 4
:

Items
PostPosted: Sun Nov 06, 2005 2:33 am    Post subject: Requesting help: Excel VBA Sudoku Grid Generator Reply with quote

Hello all,

I've read through the forums and looked at all the examples I could. I'm a new programmer and took it upon myself to work on a Sudoku creator for my intro class. Little did I know that I was out of my depth, but now I want to finish it.

As I haven't taken any algorithms classes, I'm not schooled on the differences and "logical" solutions they provide. I think I understand the concept of a backtracking algorithm but can't seem to implement it (with any success)

The way my program works is this: a string of 81 numbers is sequentially generated, number by number, by generating a random integer 1-9 and checking if it meets the "rules" (if it exists in the rows, columns, and box). Assuming it passes this constraint, that number is passed to the program, and it moves on to the next. The grid is then filled by taking that sequential list of numbers and filling in the rows left to right - like how you would read it if it were a paragraph of text.

All of this works fine. What happens, however, is the first 40 or 50 numbers lead it to a "dead end" which it cannot get past. This, I gather, is what the algorithm is intended to solve. I've tried various ways of having the program go "backward", clear the last few values, and try again - either with a static number, or by generating a random number of "Backward" steps - but none of these have amounted to a single solvable puzzle. In the end, the program just constantly generates new values for about the 5th or 6th line (this is usually where it "stalls").

I've tried to decipher examples in C and other languages, but they are so foreign to me that they don't seem to do any good. I was wondering if anyone could give me a hint on what the next logical progression would be for my program to check.

I appreciate any help anyone can give. I'll continue to read through all the information on this forum and TRY to find a solution.
Back to top
View user's profile Send private message
desenso

Joined: 06 Nov 2005
Posts: 4
:

Items
PostPosted: Sun Nov 06, 2005 3:45 am    Post subject: Reply with quote

Update:

A typo caused me to lose 8 hours of my life. It was in a part of the code that I took for granted, but a user error (in a simple addition, none the less) caused the program to fail 100% of the time.

Now that I've fixed it, I've generated a few "solvable" puzzles. However, after running the code for the last 10 to 15 minutes I have yet to come up with another working grid (I allow it 999 recursions, a recursion being stepping back a random number of cells (from 1 to 5), clearing, and trying to generate from that point).

SO I know that my solution will, mathematically, produce a working grid. The problem now is the chance of that happening is incredibly small, apparently. ANy tips?
Back to top
View user's profile Send private message
desenso

Joined: 06 Nov 2005
Posts: 4
:

Items
PostPosted: Sun Nov 06, 2005 4:52 am    Post subject: Reply with quote

Second update: the code now works much better. It generates in about 5 to 10 seconds in Excel. I know that my Algorithm is rough, and refining it might reduce this drastically, but for the time being I can work on other aspects of the program.

I'm still open to some tips if anyone has suggestions about how I could improve it, given what I've already said.
Back to top
View user's profile Send private message
Ruud
Site Admin
Joined: 17 Sep 2005
Posts: 708
:
Location: Netherlands

Items
PostPosted: Sun Nov 06, 2005 12:07 pm    Post subject: Reply with quote

Hi,

3 posts and no reply. Sad
This forum can be cruel sometimes.

Check my post in this topic:
http://www.setbb.com/phpbb/viewtopic.php?t=293&mforum=sudoku

Ruud.
Back to top
View user's profile Send private message Visit poster's website
dom

Joined: 10 Nov 2005
Posts: 42
:

Items
PostPosted: Fri Nov 11, 2005 11:53 am    Post subject: Reply with quote

There is an easy recursive algorithm if you're into such things...
Code:

PopulateBoard()
{
 PopulateCell(0, 0)
}

bool PopulateCell(index)
{
 if (index==81
  return true; // the board is full!!

 // try setting each possible value in cell
 set cellorder = {1..9} // Note this list contains 1..9 in a *random* order

 for (i=0; i<9; i++)
 {
  // set this test value
  set cell[index] = cellorder[i]

  // is this board still valid?
  if(BoardValid())
  {
   // it is so try to populate next cell
   if (PopulateCell(index+1))
   {
    // if this cell returned true, then the board is valid
    return true;
   }
  }
 }
 // rollback this cell
 return false;
}

bool BoardValid()
{
 // test constraints
 if (constraintsValid)
  return true;
 else
  return false;
}


Note the only tricky bit is cycling through the digits in random order.

For each iteration of the loop you want to try each possible value for the cell, but if you just did 1..9 you'd always get the same grid. So go 5,3,6,2,4,7,8,1,9 etc..

I'll leave that for you to work out though.
Back to top
View user's profile Send private message
Puzzled

Joined: 18 Nov 2005
Posts: 2
:

Items
PostPosted: Fri Nov 18, 2005 2:52 am    Post subject: Re: Requesting help: Excel VBA Sudoku Grid Generator Reply with quote

desenso wrote:
... As I haven't taken any algorithms classes, I'm not schooled on the differences and "logical" solutions they provide. I think I understand the concept of a backtracking algorithm but can't seem to implement it (with any success)...


Assuming the grid is B3:J11, this will get you started. You now need to adjust the grid to remove duplicates by swapping cell values. I make no warranty as to optimal design. Call Fill to load the grid.

Code:
Function Deal(P) As Integer
    Static strNum As String
    Dim N As Integer
    If P = 1 Then
        strNum = "123456789"
    Else
        N = Int(9 * Rnd + 1)
        Do
            If Mid(strNum, N, 1) <> " " Then Exit Do
            N = Int(9 * Rnd + 1)
        Loop
        Deal = N
        Mid(strNum, N, 1) = " "
    End If
End Function

Sub Fill()
    Deal (1)
    For Each I In Worksheets("Sheet1").Range("B3:D5")
        I.Value = Deal(0)
    Next I
    Deal (1)
    For Each I In Worksheets("Sheet1").Range("E3:G5")
        I.Value = Deal(0)
    Next I
    Deal (1)
    For Each I In Worksheets("Sheet1").Range("H3:J5")
        I.Value = Deal(0)
    Next I
    Deal (1)
    For Each I In Worksheets("Sheet1").Range("B6:D8")
        I.Value = Deal(0)
    Next I
    Deal (1)
    For Each I In Worksheets("Sheet1").Range("E6:G8")
        I.Value = Deal(0)
    Next I
    Deal (1)
    For Each I In Worksheets("Sheet1").Range("H6:J8")
        I.Value = Deal(0)
    Next I
    Deal (1)
    For Each I In Worksheets("Sheet1").Range("B9:D11")
        I.Value = Deal(0)
    Next I
    Deal (1)
    For Each I In Worksheets("Sheet1").Range("E9:G11")
        I.Value = Deal(0)
    Next I
    Deal (1)
    For Each I In Worksheets("Sheet1").Range("H9:J11")
        I.Value = Deal(0)
    Next I
End Sub
Back to top
View user's profile Send private message
desenso

Joined: 06 Nov 2005
Posts: 4
:

Items
PostPosted: Sun Nov 20, 2005 8:51 pm    Post subject: Reply with quote

Hey thanks for the replies Smile

My code all works.. the only thing that's not great at the moment is the time it takes to generate the grid... i took a break from the project.. went back and made a few modifications that has improved it a bit, but it still takes roughly 15 seconds to make a grid...
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