-
Sep 4th, 2011, 04:01 PM
#1
Thread Starter
Addicted Member
-
Sep 4th, 2011, 09:24 PM
#2
Re: Solving sudoku like puzzle with .NET
This hardly constitutes a puzzle IMO.
If the only variable is the topmost, right corner (labeled i0), then why not just store 9 possible solutions and display the appropriate one, when i0 changes? This will a) make your code execute faster, b) take up less space and c) be much simpler to implement (9 lines of coding for initialization and a couple of additional lines to respond to changes in i0).
On the other hand, if the user is able to use any block as i0, the puzzle may not have a solution at all, since the only available choices for the center block are 9 and 1 (obviously since it has to be a number with at most 1 neighbour).
Feel free to implement and post a method to solve {i0 in the topmost, right corner} not relying on any stored solutions. You can try recursion based on stacks or bubbling similar to the age-old bubble-sort, or even just random guesses. My guess is though, that before you're done, you'll realize that this simple problem, dosen't deserve a complicated solution.
In truth, a mature man who uses hair-oil, unless medicinally , that man has probably got a quoggy spot in him somewhere. As a general rule, he can't amount to much in his totality. (Melville: Moby Dick)
-
Sep 4th, 2011, 09:58 PM
#3
Thread Starter
Addicted Member
Re: Solving sudoku like puzzle with .NET
My goal is to use the pc to place a random number in each box so to meet the standards of x-1 or x+1.
I want the process to repeat in loop until success. The puzzle is writing the application :-)
-
Sep 4th, 2011, 10:12 PM
#4
Re: Solving sudoku like puzzle with .NET
Originally Posted by condonethis
My goal is to use the pc to place a random number in each box so to meet the standards of x-1 or x+1.
I want the process to repeat in loop until success. The puzzle is writing the application :-)
So your process is:
1) Put all of {1, 2, ..., 9} into 9 boxes randomly, 1 in each.
2) Compare box-values in 6 horizontal, 5 vertical and 8 diagonal directions to ensure that 2 values aren't neighbours. If they are: go to 1)
(which is likely the slowest possible approach of solving this problem).
It dosen't get much more simple than that. Randomly filling the boxes can be achieved through a list of references to the boxes, that is shuffled and filled with {1, 2, ..., 9} in order. Writing 19 if-sentences to compare box-values cannot be all that complicated either. I fail to see a problem here.
In truth, a mature man who uses hair-oil, unless medicinally , that man has probably got a quoggy spot in him somewhere. As a general rule, he can't amount to much in his totality. (Melville: Moby Dick)
-
Sep 5th, 2011, 10:48 AM
#5
Thread Starter
Addicted Member
Re: Solving sudoku like puzzle with .NET
Originally Posted by ThomasJohnsen
So your process is:
1) Put all of {1, 2, ..., 9} into 9 boxes randomly, 1 in each.
2) Compare box-values in 6 horizontal, 5 vertical and 8 diagonal directions to ensure that 2 values aren't neighbours. If they are: go to 1)
(which is likely the slowest possible approach of solving this problem).
It dosen't get much more simple than that. Randomly filling the boxes can be achieved through a list of references to the boxes, that is shuffled and filled with {1, 2, ..., 9} in order. Writing 19 if-sentences to compare box-values cannot be all that complicated either. I fail to see a problem here.
Would i just assign the values at random to three arrays and compare values of, for example, arr1(1) & arr2(1). I'm having trouble determining how to make the comparison in all the places being as they can't touch in any manner.
Also, how can I pick only one integer from a string at random and then remove the value from the string of possible random choices until the loop completes circuit, so i don't end up with the same number in more than one box?
-
Sep 5th, 2011, 04:10 PM
#6
Thread Starter
Addicted Member
Re: Solving sudoku like puzzle with .NET
vb.net Code:
Dim x1(2) as array
Dim x2(3)
Dim x3(4)
Dim nums(9) as integer
nums(0) = 1
nums(1) = 2
nums(2) = 3
nums(3) = 4
nums(4) = 5
nums(5) = 6
nums(6) = 7
nums(7) = 8
nums(8) = 9
Dim randomnum as New Random
Dim randval as integer
randval = randomnum.next(0,nums.length)
x1(0) = randval.tostring
anyone know how i can disable the nums(i) depending on is it was selected as randval?
-
Sep 5th, 2011, 06:02 PM
#7
Re: Solving sudoku like puzzle with .NET
I think the simplest way to implement this would be to simply declare the boxes as integers (like you did in your example):
vb Code:
'The Boxes making up the puzzle.
Private Box11, Box21 As Integer '[ ][ ]
Private Box12, Box22, Box32 As Integer '[ ][ ][ ]
Private Box13, Box23, Box33, Box43 As Integer '[ ][ ][ ][ ]
To fill the first box with i0 and the rest with random numbers, you could use:
vb Code:
'Fills the boxes with random number 1 to 9 - i0 goes into box11.
Private Sub FillBoxesWithNumbers1to9Randomly(ByVal i0 As Integer)
Dim L As New List(Of Integer)
Dim Shuffled As New List(Of Integer)
Dim Rnd As New Random
'Fill L with numbers 1-9 without value i0
For i As Integer = 1 To 9
If i <> i0 Then L.Add(i)
Next
'Keep moving random elements from L to Shuffled until L is empty.
While L.Count > 0
Dim index As Integer = Rnd.Next(0, L.Count)
Shuffled.Add(L(index))
L.RemoveAt(index)
End While
'-----------------------------------------------------
'At this point L is empty and Shuffled contains a list
'of numbers {1, 2, ..., 9} without i0 in random order.
'Put the shuffled integers into the boxes - i0 goes in the first box.
Box11 = i0
Box21 = Shuffled(0)
Box12 = Shuffled(1)
Box22 = Shuffled(2)
Box32 = Shuffled(3)
Box13 = Shuffled(4)
Box23 = Shuffled(5)
Box33 = Shuffled(6)
Box43 = Shuffled(7)
End Sub 'FillBoxesWithNumbers1to9Randomly
And finally to check if a puzzle is valid:
vb Code:
'Checks boxes to see if the current puzzle is valid.
Private Function IsPuzzleValid() As Boolean
'Make 6 horizontal checks.
If Math.Abs(Box11 - Box21) = 1 Then Return False
If Math.Abs(Box12 - Box22) = 1 Then Return False
If Math.Abs(Box22 - Box32) = 1 Then Return False
If Math.Abs(Box13 - Box23) = 1 Then Return False
If Math.Abs(Box23 - Box33) = 1 Then Return False
If Math.Abs(Box33 - Box43) = 1 Then Return False
'Make 5 vertical checks.
If Math.Abs(Box11 - Box12) = 1 Then Return False
If Math.Abs(Box21 - Box22) = 1 Then Return False
If Math.Abs(Box12 - Box13) = 1 Then Return False
If Math.Abs(Box22 - Box23) = 1 Then Return False
If Math.Abs(Box32 - Box33) = 1 Then Return False
'Make 8 diagonal checks
If Math.Abs(Box11 - Box22) = 1 Then Return False
If Math.Abs(Box21 - Box12) = 1 Then Return False
If Math.Abs(Box21 - Box32) = 1 Then Return False
If Math.Abs(Box12 - Box23) = 1 Then Return False
If Math.Abs(Box13 - Box22) = 1 Then Return False
If Math.Abs(Box22 - Box33) = 1 Then Return False
If Math.Abs(Box23 - Box32) = 1 Then Return False
If Math.Abs(Box32 - Box43) = 1 Then Return False
Return True
End Function 'IsPuzzleValid
To display a puzzle in the console window:
vb Code:
'Displays the current result
Private Sub DisplayPuzzle()
Console.WriteLine("[{0}][{1}]", Box11, Box21)
Console.WriteLine("[{0}][{1}][{2}]", Box12, Box22, Box32)
Console.WriteLine("[{0}][{1}][{2}][{3}]", Box13, Box23, Box33, Box43)
Console.WriteLine("Puzzle is " & If(IsPuzzleValid, "solved", "not valid"))
Console.WriteLine("-------------------------")
End Sub 'DisplayPuzzle
Be warned though, that solving the puzzle as outlined by you, simply by guessing will take the machine quite a while (like I mentioned above). Assuming i0 has been chosen to 1, something like:
vb Code:
Do
FillBoxesWithNumbers1to9Randomly(1)
Loop Until IsPuzzleValid()
never guessed a solution on my computer within 2 mins.
In truth, a mature man who uses hair-oil, unless medicinally , that man has probably got a quoggy spot in him somewhere. As a general rule, he can't amount to much in his totality. (Melville: Moby Dick)
-
Sep 7th, 2011, 10:00 PM
#8
Thread Starter
Addicted Member
Re: Solving sudoku like puzzle with .NET
Thanks so much for this! That was amazing!
-
Sep 7th, 2011, 10:37 PM
#9
Thread Starter
Addicted Member
Re: Solving sudoku like puzzle with .NET
is there anyway to output the answer to a textbox? I'm not sure how I can easily present the answer using console.writeline.
cake and eating it too?
-
Sep 8th, 2011, 09:07 AM
#10
Re: Solving sudoku like puzzle with .NET
Originally Posted by condonethis
is there anyway to output the answer to a textbox? I'm not sure how I can easily present the answer using console.writeline.
cake and eating it too?
You can use pretty much the same code, as long as you replace:
with
vb Code:
{NameOfTextbox}.AppendText(String.Format(....) & ControlChars.NewLine)
In truth, a mature man who uses hair-oil, unless medicinally , that man has probably got a quoggy spot in him somewhere. As a general rule, he can't amount to much in his totality. (Melville: Moby Dick)
-
Sep 8th, 2011, 05:43 PM
#11
Thread Starter
Addicted Member
Re: Solving sudoku like puzzle with .NET
Thanks!
vb.net Code:
'Displays the current result Private Sub DisplayPuzzle() 'MsgBox("[{0}][{1}]" + Box11 + ", " + Box21 + Environment.NewLine + "[{0}][{1}][{2}]" + " , " + Box12 + ", " + Box22 + ", " + Box32 + Environment.NewLine + "[{0}][{1}][{2}][{3}]" + ", " + Box13 + ", " + Box23 + ", " + Box33 + ", " + Box43) TextBox1.AppendText(String.Format("[{0}][{1}]", Box11, Box21) & ControlChars.NewLine) TextBox1.AppendText(String.Format("[{0}][{1}][{2}]", Box12, Box22, Box32) & ControlChars.NewLine) TextBox1.AppendText(String.Format("[{0}][{1}][{2}][{3}]", Box13, Box23, Box33, Box43) & ControlChars.NewLine) TextBox1.AppendText(String.Format("Puzzle is " & If(IsPuzzleValid, "solved", "not valid") & ControlChars.NewLine)) End Sub 'DisplayPuzzle Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click MsgBox("this might take a while") Me.WindowState = FormWindowState.Minimized Dim howmany As Integer = 1 Me.Opacity = 0 Do Until howmany = 10 Do FillBoxesWithNumbers1to9Randomly(howmany) Loop Until IsPuzzleValid() DisplayPuzzle() howmany = howmany + 1 Loop TextBox1.AppendText(String.Format("Puzzle is " & If(IsPuzzleValid, "solved", "not valid") & ControlChars.NewLine)) Me.WindowState = FormWindowState.Normal Me.Opacity = 1 Me.Focus() End Sub End Class
Last edited by condonethis; Sep 8th, 2011 at 09:38 PM.
Reason: DUH...
-
Sep 8th, 2011, 06:09 PM
#12
Thread Starter
Addicted Member
Re: Solving sudoku like puzzle with .NET
[1][3]
[5][9][6]
[7][2][4][8]
[2][7]
[4][9][5]
[6][1][3][8]
[3][1]
[5][9][4]
[7][2][6][8]
[4][6]
[2][9][3]
[5][7][1][8]
[5][3]
[1][9][6]
[7][4][2][8]
[6][8]
[3][1][4]
[5][7][9][2]
[7][4]
[9][1][8]
[5][3][6][2]
[8][3]
[6][1][5]
[4][9][7][2]
[9][7]
[3][1][4]
[5][8][6][2]
[9][7]
[3][1][4]
[5][8][6][2]
(done in 3 mins)
What did your honor student do today?
Last edited by condonethis; Sep 8th, 2011 at 06:20 PM.
-
Sep 8th, 2011, 08:05 PM
#13
Re: Solving sudoku like puzzle with .NET
Originally Posted by condonethis
What did your honor student do today?
Cool so the code actually worked .
My patience didn't allow for it to ever finish on ye olde antique in my possession.
In truth, a mature man who uses hair-oil, unless medicinally , that man has probably got a quoggy spot in him somewhere. As a general rule, he can't amount to much in his totality. (Melville: Moby Dick)
-
Sep 9th, 2011, 11:19 PM
#14
Thread Starter
Addicted Member
Re: Solving sudoku like puzzle with .NET
-
Sep 10th, 2011, 01:12 AM
#15
Re: Solving sudoku like puzzle with .NET
Originally Posted by condonethis
What does it do ?
If my post was helpful to you, then express your gratitude using Rate this Post.
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
-
Sep 12th, 2011, 12:42 AM
#16
Thread Starter
Addicted Member
Re: Solving sudoku like puzzle with .NET
Originally Posted by akhileshbc
What does it do ?
http://en.wikipedia.org/wiki/Twelf
It is a language for mathematic problem solving. Although the method used to obtain the answer for the issue is great, it is nothing more than brute force. Twelf will allow you to problem solve using strategy and deduction, whereas this vb solution is achieved by sheer chance and probability. Twelf can solve a problem in milliseconds that takes minutes in VB.Net
Code:
cell : type.
1 : cell.
2 : cell.
3 : cell.
4 : cell.
5 : cell.
6 : cell.
7 : cell.
8 : cell.
9 : cell.
nat : type.
z : nat.
s : nat -> nat. %prefix 5 s.
nine = s s s s s s s s s z.
cells : nat -> type.
// : cell -> cells N -> cells (s N).
%infix right 5 //.
nil : cells z.
cellss : nat -> type.
/// : cells nine -> cellss N -> cellss (s N).
%infix right 4 ///.
nill : cellss z.
remove : cells (s N) -> cell -> cells N -> type.
%mode remove +L +C -L'.
rem-hit : remove (X // C) X C.
okay that we can also skip this one
rem-miss : remove (X // C) Y (X // C')
<- remove C Y C'.
%worlds () (remove _ _ _).
%terminates D (remove D _ _).
nodups : cells X -> cells Y -> type.
%mode nodups +OK +L.
nd-nil : nodups _ nil.
nd-hit : nodups OK (X // C)
<- remove OK X OK'
<- nodups OK' C.
%worlds () (nodups _ _).
%terminates D (nodups _ D).
valid if there are no duplicates
allcells = 1 // 2 // 3 //
4 // 5 // 6 //
7 // 8 // 9 // nil.
unitok : cells nine -> type.
uo : unitok C
<- nodups allcells C.
the nine rows
%abbrev board = cellss nine.
extract three boxes from three rows
boxes : cells nine -> cells nine -> cells nine ->
cells nine -> cells nine -> cells nine -> type.
%mode boxes +A +B +C -A' -B' -C'.
b : boxes
(A1 // B1 // C1 // D1 // E1 // F1 // G1 // H1 // I1 // nil)
(A2 // B2 // C2 // D2 // E2 // F2 // G2 // H2 // I2 // nil)
(A3 // B3 // C3 // D3 // E3 // F3 // G3 // H3 // I3 // nil)
(A1 // B1 // C1 //
A2 // B2 // C2 //
A3 // B3 // C3 // nil)
(D1 // E1 // F1 //
D2 // E2 // F2 //
D3 // E3 // F3 // nil)
(G1 // H1 // I1 //
G2 // H2 // I2 //
G3 // H3 // I3 // nil).
%worlds () (boxes _ _ _ _ _ _).
%total {} (boxes _ _ _ _ _ _).
transpose a whole board
transpose : board -> board -> type.
%mode transpose +B -B'.
tr : transpose ((A1 // B1 // C1 // D1 // E1 // F1 // G1 // H1 // I1 // nil) ///
(A2 // B2 // C2 // D2 // E2 // F2 // G2 // H2 // I2 // nil) ///
(A3 // B3 // C3 // D3 // E3 // F3 // G3 // H3 // I3 // nil) ///
(A4 // B4 // C4 // D4 // E4 // F4 // G4 // H4 // I4 // nil) ///
(A5 // B5 // C5 // D5 // E5 // F5 // G5 // H5 // I5 // nil) ///
(A6 // B6 // C6 // D6 // E6 // F6 // G6 // H6 // I6 // nil) ///
(A7 // B7 // C7 // D7 // E7 // F7 // G7 // H7 // I7 // nil) ///
(A8 // B8 // C8 // D8 // E8 // F8 // G8 // H8 // I8 // nil) ///
(A9 // B9 // C9 // D9 // E9 // F9 // G9 // H9 // I9 // nil) /// nill)
((A1 // A2 // A3 // A4 // A5 // A6 // A7 // A8 // A9 // nil) ///
(B1 // B2 // B3 // B4 // B5 // B6 // B7 // B8 // B9 // nil) ///
(C1 // C2 // C3 // C4 // C5 // C6 // C7 // C8 // C9 // nil) ///
(D1 // D2 // D3 // D4 // D5 // D6 // D7 // D8 // D9 // nil) ///
(E1 // E2 // E3 // E4 // E5 // E6 // E7 // E8 // E9 // nil) ///
(F1 // F2 // F3 // F4 // F5 // F6 // F7 // F8 // F9 // nil) ///
(G1 // G2 // G3 // G4 // G5 // G6 // G7 // G8 // G9 // nil) ///
(H1 // H2 // H3 // H4 // H5 // H6 // H7 // H8 // H9 // nil) ///
(I1 // I2 // I3 // I4 // I5 // I6 // I7 // I8 // I9 // nil) /// nill).
%worlds () (transpose _ _).
%total {} (transpose C _).
boardok : board -> type.
bo : boardok
(A /// B /// C /// D /// E /// F /// G /// H /// I /// nill)
rows
<- unitok A <- unitok B <- unitok C
<- unitok D <- unitok E <- unitok F
<- unitok G <- unitok H <- unitok I
boxes
<- boxes A B C Ab Bb Cb
<- unitok Ab <- unitok Bb <- unitok Cb
<- boxes D E F Db Eb Fb
<- unitok Da <- unitok Ea <- unitok Fa
<- boxes G H I Gb Hb Ib
<- unitok Gb <- unitok Hb <- unitok Ib
cols
<- transpose (A /// B /// C /// D /// E /// F /// G /// H /// I /// nill)
(At /// Bt /// Ct /// Dt /// Et /// Ft /// Gt /// Ht /// It /// nill)
<- unitok At <- unitok Bt <- unitok Ct
<- unitok Dt <- unitok Et <- unitok Ft
<- unitok Gt <- unitok Ht <- unitok It.
%solve _ : boardok
(
(8 // 2 // 6 // 7 // 1 // 4 // 9 // 5 // 3 // nil) ///
(3 // 4 // 5 // 8 // 6 // 9 // 2 // 1 // 7 // nil) ///
(9 // 7 // 1 // 2 // 5 // 3 // 4 // 8 // 6 // nil) ///
(1 // 6 // 3 // 9 // 2 // 5 // 7 // 4 // 8 // nil) ///
(5 // 9 // 4 // 6 // 7 // 8 // 1 // 3 // 2 // nil) ///
(2 // 8 // 7 // 3 // 4 // 1 // 5 // 6 // 9 // nil) ///
(7 // 5 // 9 // 4 // 3 // 6 // 8 // 2 // 1 // nil) ///
(4 // 3 // 2 // 1 // 8 // 7 // 6 // 9 // 5 // nil) ///
(6 // 1 // 8 // 5 // 9 // 2 // 3 // 7 // 4 // nil) /// nill).
%solve _ : boardok
(
(8 // 2 // 6 // 7 // 1 // 4 // 9 // 5 // 3 // nil) ///
(3 // 4 // 5 // 8 // 6 // 9 // 2 // 1 // 7 // nil) ///
(9 // 7 // 1 // 2 // 5 // 3 // 4 // 8 // 6 // nil) ///
(1 // 6 // 3 // _ // 2 // 5 // 7 // 4 // 8 // nil) ///
(5 // 9 // 4 // _ // 7 // 8 // 1 // 3 // 2 // nil) ///
(2 // 8 // 7 // _ // _ // 1 // 5 // 6 // 9 // nil) ///
(7 // 5 // 9 // 4 // 3 // 6 // 8 // 2 // 1 // nil) ///
(4 // 3 // 2 // 1 // 8 // 7 // 6 // 9 // 5 // nil) ///
(6 // 1 // 8 // 5 // 9 // 2 // 3 // 7 // 4 // nil) /// nill).
-http://twelf.org/wiki/Sudoku
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|