PDA

View Full Version : first puzzle : sudoku



TomLebowski
09-01-2010, 15:39
don`t know if you have knowledge about puzzle and sudoku ? :)
here comes a little sudoku gui example. adepted this from two other scripts and it take some hours to find best ui method with cells and number input (1-9). perhaps anybody likes it. part one is ready.


' Empty GUI script created on 01-09-2010 04:32:40 by (ThinAIR)
'---------------------------------------------------------------
'---------SUDOKU EXAMPLE UI THINBASIC---------------------------'

Uses "console", "ui"

%IDD_DIALOG1 = 201
%IDC_BUTTON1 = 202
%IDC_LABEL1 = 203

'--------------------------------------------------'
Function TBMAIN() As Long
Local hdlg As Long
ShowDialog1 hdlg
End Function
'--------------------------------------------------'

''--------------------------------------------------
CallBack Function ShowDialog1Proc()
Local i As Long
Static hFont As Long
Static txt As String, s As String
Select Case CBMSG
Case %WM_INITDIALOG
' The height and width of this font is designed to fit one number in each cell
hFont = CreateFont(-23,20,0,0,700,0,0,0,0,3,2,1,82,"Arial")
For i=1 To 81
Control Set Text CBHNDL, i+1000, ""
Control Send CBHNDL, i+1000, %WM_SETFONT, hFont, %TRUE
Control Send CBHNDL, i+1000, %EM_SETLIMITTEXT, 1, 0
Next
Control Set Focus CBHNDL, 1001
Case %WM_COMMAND
' Process control notifications
Select Case CBCTL
Case 1001 To 1081 ' any textbox
If CBCTLMSG=%EN_CHANGE Then
Control Get Text CBHNDL, CBCTL To txt
If Len(txt) = 1 Then
If Verify(txt, "123456789 ") = 0 Then ' only these characters are valid
If CBCTL < 1081 Then
Control Set Focus CBHNDL, CBCTL + 1 ' advance to next cell
Else
Control Set Focus CBHNDL, 1001 ' set to first cell
End If
Else
Control Set Text CBHNDL, CBCTL, ""
End If
End If
End If
Case %IDC_BUTTON1 ' Finished
If CBCTLMSG = %BN_CLICKED Or CBCTLMSG = 1 Then
s=""
For i = 1 To 81
Control Get Text CBHNDL, i+1000 To txt
If Len(txt)=0 Then txt="0"
s=s+txt
Next
Replace$( " ",s, With "0")
's = Replace$(sMainString, [Any] sMatchString, [With] sNewString)

MsgBox 0, "Length of string is"+Str$(Len(s))+" characters "+$CRLF+$CRLF+"String is:"+$CRLF+$CRLF+s
Dialog End CBHNDL
End If
End Select
End Select
End Function

'--------------------------------------------------
Function ShowDialog1(ByVal hParent As DWord) As Long
Local lRslt As Long
Local hDlg As DWord
Local i As Long, j As Long, ID As Long, x As Long, y As Long, k As Long
Dialog New Pixels, hParent, "Enter Sudoku Puzzle", -1, 1-, 394, 530, _
%WS_POPUP Or %WS_BORDER Or %WS_DLGFRAME Or %WS_SYSMENU Or _
%WS_CLIPSIBLINGS Or %WS_VISIBLE Or %DS_MODALFRAME Or %DS_3DLOOK Or _
%DS_NOFAILCREATE Or %DS_SETFONT, %WS_EX_CONTROLPARENT Or %WS_EX_LEFT _
Or %WS_EX_LTRREADING Or %WS_EX_RIGHTSCROLLBAR, To hDlg
Control Add Button, hDlg, %IDC_BUTTON1, "Finished", 130, 478, 130, 30
Control Add Label, hDlg, %IDC_LABEL1, "Enter one line " + _
"at a time. Use 1 to 9 or Space to enter. Moves automatically to " + _
"next cell. You can also click on each cell and then enter the " + _
"number. Use Del or Backspace to delete a number or a space character.", 4, 8, 386, 68, _
%WS_CHILD Or %WS_VISIBLE Or %SS_CENTER, %WS_EX_LEFT Or %WS_EX_LTRREADING
'
' make textboxes
ID = 1000
For i= 0 To 8
y=85+i*40
If i>2 Then y=y+10
If i>5 Then y=y+10
For j = 0 To 8
Incr ID
x=10+j*40
If j>2 Then x=x+10
If j>5 Then x=x+10
Control Add Textbox, hDlg, ID, "", x, y, 30, 30
Next
Next
'
' make frames
ID = 2000
For i= 0 To 2
y= 79+i*130
For j = 0 To 2
x=4+j*130
For k = 0 To 1
Incr ID
Control Add Line, hDlg, ID, "", x-k, y-k, 122+2*k, 122+2*k, %WS_CHILD Or %WS_VISIBLE Or %SS_BLACKFRAME
Next
Next
Next
Dialog Show Modal hDlg, Call ShowDialog1Proc
Function = lRslt
End Function
'--------------------------------------------------end of file

no number in lines and blocks and columns should be the same ;)

cheers, tom

Lionheart008
10-01-2010, 11:59
great and nice example with your "sudoku" example! tom, very, very nice one! I have tested this morning and here's working everything fine! :)

one suggest: perhaps you can predefine (pro block three numbers?) some numbers so it's becoming a "real" sudoku game I know from newspapers or magazines ? thanks for sharing, best regards, Frank