Sudoku: Difference between revisions

1,228 bytes added ,  14 years ago
→‎{{header|AutoHotkey}}: Added GUI, removed iterations (couldn't get it to work)
(→‎{{header|D}}: add implementation)
(→‎{{header|AutoHotkey}}: Added GUI, removed iterations (couldn't get it to work))
Line 4:
 
=={{header|AutoHotkey}}==
<lang AutoHotkey>puzzleEasySetBatchLines, =-1
SetTitleMatchMode, 3
( LTrim
Loop 9 {
394 @@2 67@
r := A_Index, y := r*17-8
@@@ 3@@ 4@@
Loop 9 {
5@@ 69@ @2@
c := A_Index, x := c*17+5
 
Gui, Add, Edit, x%x% y%y% w17 h17 v%r%%c% Center Number Limit1
@45 @@@ 9@@
}
6@@ @@@ @@7
}
@@7 @@@ 58@
Gui, Add, Button, vButton gSolve w175 x10 Center, Solve
 
Gui, Add, Text, vMsg, Enter Sudoku puzzle and click Solve
@1@ @67 @@8
Gui, Show,, Sudoku Solver
@@9 @@8 @@@
Return
@26 4@@ 735
Solve:
)
Gui, Submit, NoHide
Loop 9
{
r := A_Index
Loop 9
If (%r%%A_Index% = "")
puzzle .= "@"
Else
puzzle .= %r%%A_Index%
}
s := A_TickCount
answer := Sudoku(puzzle)
MsgBox % "Easy:`n" Sudoku( puzzleEasy ) "`nIterations: " ErrorLevel "`n`nSeconds: " (A_TickCount-s)/1000
e := A_TickCount
 
seconds := (e-s)/1000
 
StringSplit, a, answer, |
 
Loop 9
{
r := A_Index
Loop 9
{
b := (r*9)+A_Index-9
GuiControl,, %r%%A_Index%, % a%b%
GuiControl, +ReadOnly, %r%%A_Index%
}
}
GuiControl,, Msg, Solved! Time: %seconds%
GuiControl,, Button, Close
GuiControl, +gClose, Button
return
GuiClose:
Close:
ExitApp
#IfWinActive, Sudoku Solver
~Up::
GuiControlGet, f, focus
StringTrimLeft, f, f, 4
f := ((f >= 1 && f <= 9) ? f+72 : f-9)
GuiControl, Focus, Edit%f%
return
~Down::
GuiControlGet, f, focus
StringTrimLeft, f, f, 4
f := ((f >= 73 && f <= 81) ? f-72 : f + 9)
GuiControl, Focus, Edit%f%
return
~Left::
GuiControlGet, f, focus
StringTrimLeft, f, f, 4
copyf := f
While copyf > 0
copyf -= 9
f := ((copyf = 1) ? f+9 : f-1)
f := ((f < 1) ? 81 : f)
GuiControl, Focus, Edit%f%
return
~Right::
GuiControlGet, f, focus
StringTrimLeft, f, f, 4
copyf := f
While copyf >= 9
copyf -= 9
f := ((copyf = 9) ? f-9 : f+1)
f := Mod(f, 81)
GuiControl, Focus, Edit%f%
return
#IfWinActive
; Functions Start here
 
Sudoku( p ) { ;ErrorLevel contains the number of iterations
p := RegExReplace(p, "[^1-9@]"), ErrorLevel := 0 ;format puzzle as single line string
return Sudoku_Display(Sudoku_Solve(p))
}
 
Sudoku_Solve( p, d = 0 ) { ;d is 0-based
; http://www.autohotkey.com/forum/topic46679.html
Line 40 ⟶ 102:
; returns: 81 char string with non-givens replaced with valid solution
;
If (d >= 81), ErrorLevel++
return p ;this is 82nd iteration, so it has successfully finished iteration 81
If InStr( "123456789", SubStr(p, d+1, 1) ) ;this depth is a given, skip through
Line 56 ⟶ 118:
return 0
}
 
Sudoku_Constraints( ByRef p, d ) {
; returns a string of the constraints for a particular position
Line 74 ⟶ 136:
. SubStr(p, b, 3) SubStr(p, b+9, 3) SubStr(p, b+18, 3)
}
 
Sudoku_Display( p ) {
If StrLen(p) = 81
loop 81
r .= SubStr(p, A_Index, 1) (!Mod(A_Index, 9) ? "`n" : !Mod(A_Index,3) ? "`t" :. "|")
return r
}</lang>
Anonymous user