Anonymous user
Sudoku: Difference between revisions
→{{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>
SetTitleMatchMode, 3
Loop 9 {
r := A_Index, y := r*17-8
Loop 9 {
c := A_Index, x := c*17+5
Gui, Add, Edit, x%x% y%y% w17 h17 v%r%%c% Center Number Limit1
}
}
Gui, Add, Button, vButton gSolve w175 x10 Center, Solve
Gui, Add, Text, vMsg, Enter Sudoku puzzle and click Solve
Gui, Show,, Sudoku Solver
Return
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)
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@]")
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)
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)
return r
}</lang>
|