1 Attachment(s)
Solving sudoku like puzzle with .NET
Attempting to try to use dot net to solve a math puzzle where
list = "123456789"
row0.length = 2
row1.length = 3
row2.length = 4
none of the integers from the given list can touch each other in a horizontal, vertical, or diagonal manner that makes i +- 1 = true
want to use a random integer in an array so that during that loop the
list.replace("i","")
see image for further illustration
brain teaser?:wave:
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.
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 :-)
Re: Solving sudoku like puzzle with .NET
Quote:
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.
Re: Solving sudoku like puzzle with .NET
Quote:
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?
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?
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.
Re: Solving sudoku like puzzle with .NET
Thanks so much for this! That was amazing!
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?
Re: Solving sudoku like puzzle with .NET
Quote:
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)
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
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?
Re: Solving sudoku like puzzle with .NET
Quote:
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.
Re: Solving sudoku like puzzle with .NET
Re: Solving sudoku like puzzle with .NET
Quote:
Originally Posted by
condonethis
What does it do ? :confused:
Re: Solving sudoku like puzzle with .NET
Quote:
Originally Posted by
akhileshbc
What does it do ? :confused:
http://en.wikipedia.org/wiki/Twelf :thumb:
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