PDA

View Full Version : Sudoku setup



Aussiebear
01-21-2007, 03:54 AM
I'm trying to experiment with a Sudoku type layout and whilst it was easy to get the initial setup done, I'm struggling with finding a way to insert the correct numbers into the defined range.

With six sets of game data residing on the second sheet, I'm looking for a method of when a new game is selected by the player, the range B2:J10 is initially cleared, then according to the value in the position row, the correct cell is selected and the corresponding number value is entered.

Example, if game one was selected, then cell C2 is selected and the value 7 is entered, Cell F2 is selected and the value 5 is entered. This continues until all the nominated positions are filled with their associated data value.

How am I able to do this?

Ted

Bob Phillips
01-21-2007, 04:25 AM
.

Aussiebear
01-21-2007, 04:39 AM
Bob, You could have taken a bit longer to arrive at the solution. I'm a shattered poster. I reall didn't expect an answer for 24 hours.
:bow:

Thank you (once again)

Bob Phillips
01-21-2007, 04:50 AM
Sorry!

Ken Puls
01-21-2007, 10:47 AM
ROFL!

I remember that Dick Kusleika had a Sudoka template on his blog a year or two ago. It's been pulled down now for some reason, but the interface looked a bit like the attached. I found that, when I was first learning to play it, it was really easy to mark off the numbers you knew around the edges, rather than clutter the main squares.

Of course, he had it coded and formula'd up so that it made the things we too easy on the computer screen. I printed it off and used it on paper.

Eventually, though, I found I didn't need it. :)

I've become a bit more interested in Kakuro, myself, now. ;)

Norie
01-21-2007, 10:52 AM
Doesn't Andy Pope have a Sudoku thing on his site?

Here it is http://www.andypope.info/fun/sudoku.htm

When Sudoku became popular I was contemplating doing some code or whatever for it, then I saw this.:)

PS I still can't solve them on paper.:bug::doh::(

Zack Barresse
01-21-2007, 10:56 AM
I've always used Andy's as well. A fun bit of code. :)

Bob Phillips
01-21-2007, 11:18 AM
I think the best version of Sudoku is not the point. Ted is creating his own, as a learning tool in VBA I would guess.

Ken, I don't know Kakuro. Got a link?

Ken Puls
01-21-2007, 04:00 PM
Hi Bob,

Kakuro is also know as "Cross Sums". It's been around for a few years under that name, and simple versions were featured in most variety puzzle books that I came across.

Kakuro can get much more difficult though, and certainly is more challenging than Sudoku. Here's a link to Kakuro.com (http://www.kakuro.com/). I haven't been to their site before, but it does have a "how to play" section. :)

Bob Phillips
01-21-2007, 05:25 PM
Thanks Ken.

So where do you know it from?

Ivan F Moala
01-21-2007, 07:07 PM
[QUOTE=xld]I think the best version of Sudoku is not the point. Ted is creating his own, as a learning tool in VBA I would guess.

snipQUOTE]

YES!, that is exactly right. Even though it may have been done before it is always good to try yourself.

Aussiebear
01-22-2007, 03:34 AM
Yes, There are plenty of commercial Sudoku softwares currently available however I am trying to do this as a learning exercise.

Bob, Can you detail the code for me please?


Public Sub SetupGame(ByValue As Long)
Dim i As Long

With wsStarts
wsSudoku.Range("Sudoku").ClearContents
For i = 2 To .Range("GameData").Columns.Count
wsSudoku.Range(.Cells(Game *2, i).Value = .Cells(Game *2+1, i).Value
Next i
End With
End Sub


As I understand this


wsSudoku.Range("Sudoku").ClearContents

clears the contents of the range named Sudoku on the sheet Sudoku.


For i = 2 To .Range("GameData").Columns.Count

My guess is... that defining the row count as two in each column in the named range Game Data on Starts sheet?


wsSudoku.Range(.Cells(Game*2, i).Value = .Cells(Game*2+1, i).Value

Sorry can't follow this bit.

Ted

Bob Phillips
01-22-2007, 04:00 AM
Public Sub SetupGame(ByValue As Long)
Dim i As Long

With wsStarts
wsSudoku.Range("Sudoku").ClearContents
For i = 2 To .Range("GameData").Columns.Count
wsSudoku.Range(.Cells(Game *2, i).Value = .Cells(Game *2+1, i).Value
Next i
End With
End Sub

First thing to be aware of is that I have set up various range names used in the code. The SuDoKu grid is named SuDoKu (original eh?).



wsSudoku.Range("Sudoku").ClearContents clears the contents of the range named Sudoku on the sheet Sudoku.
Yes, that clears the SuDoKu grid.



For i = 2 To .Range("GameData").Columns.Count
My guess is... that defining the row count as two in each column in the named range Game Data on Starts sheet?

GameData is a name that covers all of the possible game setups.

This is looping through the columns, not the rows. I start at column 2, as column 1 is just blank. I count the number of columns using the columns count of the data range (for this reason, GameData should include clumn 1).



wsSudoku.Range(.Cells(Game*2, i).Value = .Cells(Game*2+1, i).Value
Sorry can't follow this bit.

This is the core of it. Your data consists of a cell address with the value below it. As I am iterating through the columns, i points at the next column to be processed, that is the next cell ref/value pair. Game is the selected game number, so I multiply that by 2 to get the actual row the cell ref is in (2,4,6, etc.)

wsSudoku.Range(.Cells(Game*2, i).Value

The value is in the same column, just 1 row below.Cells(Game*2+1, i).Value

Aussiebear
01-22-2007, 04:31 AM
OKay. that makes sence, now for one other question. Normally the initial layout has a varying number of values included. Obviousily the more numbers inserted by the system the easier it becomes to complete the puzzle. How do I protect the values the system enters, so that the person playing the game cannot overwrite them?

Can I lock the cells in the range ("Sudoku")where the value <>"", then protect the sheet? And if I do that then I need to unprotect the sheet, then unlock the cells, to enable the range to be cleared before a new game can be effected?

Ted

Bob Phillips
01-22-2007, 04:52 AM
This variation of SetupGame will lock each cell as it is loaded



Public Sub SetupGame(ByVal Game As Long)
Dim i As Long
Dim cell As Range

With wsStarts
wsSudoku.Unprotect Password:="Ted"
wsSudoku.Range("SuDoKu").ClearContents
For Each cell In wsSudoku.Range("SuDoKu")
cell.Locked = False
Next cell
For i = 2 To .Range("GameData").Columns.Count
wsSudoku.Range(.Cells(Game * 2, i).Value).Value = .Cells(Game * 2 + 1, i).Value
wsSudoku.Range(.Cells(Game * 2, i).Value).Locked = True
Next i
wsSudoku.Protect Password:="Ted"
End With

End Sub

Ken Puls
01-22-2007, 10:33 AM
Thanks Ken.

So where do you know it from?

I became a bit of a Sudoku addict. In a couple of the books I purchased, a few sample Kakuro puzzles were in the back. I got a bit bored with Sudoku, so eventually made the foray into Kakuro.

Must admit that I hated it at first, as I couldn't get anywhere. I finally found a table of "know combinations" (i.e. 6 over 3 cells must be 1,2,3). It gives you a place to make a foothold, and after that I could break into them and started to quite enjoy it. :)

Aussiebear
01-23-2007, 12:30 AM
Thanks Bob.

Aussiebear
01-23-2007, 01:09 AM
Bob, just had to make one change.



Public Sub SetupGame(ByVal Game As Long)
Dim i As Long
Dim cell As Range

With wsStarts
wsSudoku.Unprotect Password:="Ted"
wsSudoku.Range("SuDoKu").ClearContents
For Each cell In wsSudoku.Range("SuDoKu")
cell.Locked = False
Next cell
For i = 2 To .Range("GameData").Columns.Count
wsSudoku.Range(.Cells(Game * 2, i).Value).Value = .Cells(Game * 2 + 1, i).Value
wsSudoku.Range(.Cells(Game * 2, i).Value).Locked = True
wsSudoku.Range("GameNum").Locked = False
Next i
wsSudoku.Protect Password:="Ted"
End With

End Sub


Needed to be able to unlock the GameNum cell to enable selection of the game. Otherwise your code is excellant for what I need

Ted

Bob Phillips
01-23-2007, 02:49 AM
Good point :doh:

Bob Phillips
01-23-2007, 02:51 AM
Just thinking about this, you probably don't need to do it in code, just un protect the sheet, unlock that cell, and all should be okay, as it never gets explicitly locked in code, it is not part of the SuDoKu range.

mail2bharath
02-05-2007, 05:32 AM
thankzz for a good topic

noddy
07-24-2007, 02:04 AM
Hi Aussiebear, I have written, all in VAB a program that will solve most, but the very hardest.
a great learning exercise.
started by putting all possible numbers in the cells that didn't have a number in it, and then went from there.
if you want a copy, just say so. But DO go on with your program.

unmarkedhelicopter
07-24-2007, 09:41 AM
I have code that will solve ALL, even Samurai Master Soduko (Multiple overlapping Soduko's (The Times))
If it can not solve it definitively, it will ask you if it can take a guess, if so it picks one of the cells with the fewest possibles and picks one, it will keep on going until it can not proceed any further and then take another guess, if any of the guesses result in an error it backs up a guess and takes the next number, multiple fork tracking. It will also look at Soduko with 4,6,8,9,10,12,16,20 number squares (not just the standard 9) (uses A-J) when it runs out of numbers). Also have separate code (from same base but developed differently) to solve 3D Soduko (i.e. cubes and cuboids),(non multiples though).
If you allow guessing, either one, will create full puzzles for you and tehn back off untill there are just enough numbers to definitively finish the puzzle.
Just so you know what to aim for ;)

SWE321
12-29-2009, 12:27 PM
I am an sudoku addict myself. I have coded a generator solver myself. The program does the same thing but work with the cells is have a different approach. You can find the link to my vba site in my details.

Let me know if you have a solver ready soon maybe we can run a competition which solver is the fastest. ;))