;- Norm's Sodoku Project IncludeFile "sodokudlg.pbi" IncludeFile "sodokumath.pbi" IncludeFile "sodokuhlp.pbi" IncludeFile "Helper.pbi" SetHelpFile("sodoku.htm","Waffle's Sodoku Help") Open_Window_Sodoku() Macro SetState(ID,State) SendMessage_(GadgetID(ID),#BM_SETSTATE,state,0) EndMacro Macro GetState(ID) SendMessage_(GadgetID(ID),#BM_GETSTATE,0,0) EndMacro ;- Game Vars Global GameWon.l=0 Global IsDirty.l=0 Global TextColor.l=Color(0) SetGadgetColor(#Text_0, #PB_Gadget_FrontColor, TextColor) SetState(#BImage_0,1) Global TextNumber.l=1 SetGadgetState(#BNum_0,1) CreateMode.l=0 Global Marking.l=0 ;for multiple undos .... ;set gid to -1 to mark batch-undo Structure Move GID.l VS.s Color.l EndStructure Global NewList Undo.Move() ;- LoadPreferences InitSound() OpenPreferences(RunPath+"WSodoku.ini") Global Skill.l=ReadPreferenceLong("Skill",0) SetMenuItemState(#MenuBar_0,#Game_L1+Skill,1) Sound_Mode=Val(ReadPreferenceString("Sound",Str(#Sound_Default))) If Sound_Mode=#Sound_On Sound_Mode=#Sound_Default EndIf Sound_Custom=ReadPreferenceString("Custom","*.wav") Select Sound_Mode Case #Sound_Off SetMenuItemState(#Menubar_0,#Sound_Off,1) SetMenuItemState(#Menubar_0,#Sound_On,0) Case #Sound_Custom SetMenuItemState(#Menubar_0,#Sound_Off,0) SetMenuItemState(#Menubar_0,#Sound_On,1) If Sound_Custom<>"*.wav" If LoadSound(0,Sound_Custom)=0 MessageRequester("Waffle's Sodoku","Unable to load sound"+Chr(10)+Sound_Custom,#MB_ICONSTOP) Sound_Mode=#Sound_Default SetMenuItemState(#Menubar_0,#Sound_Default,1) SetMenuItemState(#Menubar_0,#Sound_Custom,0) Else SetMenuItemState(#Menubar_0,#Sound_Default,0) SetMenuItemState(#Menubar_0,#Sound_Custom,1) EndIf Else Sound_Mode=#Sound_Default SetMenuItemState(#Menubar_0,#Sound_Default,1) SetMenuItemState(#Menubar_0,#Sound_Custom,0) EndIf Case #Sound_Default SetMenuItemState(#Menubar_0,#Sound_Off,0) SetMenuItemState(#Menubar_0,#Sound_On,1) SetMenuItemState(#Menubar_0,#Sound_Default,1) SetMenuItemState(#Menubar_0,#Sound_Custom,0) EndSelect Global Win_Mode.l=Val(ReadPreferenceString("WinSound",Str(#Sound_WOff))) Global Win_Custom.s=ReadPreferenceString("WinCustom","*.wav") Select Win_Mode Case #Sound_WOff SetMenuItemState(#Menubar_0,#Sound_WOff,1) Case #Sound_WON SetMenuItemState(#Menubar_0,#Sound_WON,1) Case #Sound_WCustom If LoadSound(1,Win_Custom) ;SetMenuItemState(#Menubar_0,#Sound_WON,1) SetMenuItemState(#Menubar_0,#Sound_WCustom,1) Else MessageRequester("Waffle's Sodoku","Unable to load sound"+Chr(10)+Win_Custom,#MB_ICONSTOP) Win_Mode=#Sound_WON SetMenuItemState(#Menubar_0,#Sound_WON,1) SetMenuItemState(#Menubar_0,#Sound_WCustom,0) EndIf EndSelect ;delay to let windows initialize Repeat Delay(1) event=WindowEvent() Until event=0 Width.l=Val(ReadPreferenceString("Width","0")) Height.l=Val(ReadPreferenceString("Height","0")) ;Debug "Resizing to "+Str(Width)+","+Str(Height) If Width ResizeWindow(#Window_Sodoku,#PB_Ignore,#PB_Ignore,width,height) ResizeGame(Width,Height) EndIf ;delay to let windows initialize Repeat Delay(1) event=WindowEvent() Until event=0 ;Global DefColor.l=GetGadgetColor(#BString_0,#PB_Gadget_BackColor) Global DefColor.l=Val(ReadPreferenceString("DefColor",Str(GetGadgetColor(#BString_0,#PB_Gadget_BackColor)))) Global HIColor.l=Val(ReadPreferenceString("HIColor",Str(RGB(129,129,129)))) Global MarkColor.l=Val(ReadPreferenceString("MarkColor",Str(RGB(255,0,0)))) Global MasterColor.l=Val(ReadPreferenceString("MasterColor",Str(DefColor))) Global NumberColor.l=Val(ReadPreferenceString("NumberColor",Str(DefColor))) ;Global MasterSave.s=Runpath+"Masters\" ;Global GameSave.s=RunPath+"Saves\" MoveFiles=Val(ReadPreferenceString("MoveFiles","1")) ;MoveFiles Flag 0 = Use RunPath, And do not ask again ; 1 = Use RunPath, And remember to ask again (possible error) ; 2 = Use Master and Save , But directory not yet created ; 3 = Use Master and Save , Directories are ready If MoveFiles=1 If ExamineDirectory(1,RunPath,"*.*") msg.s="Old files detected, before version 1.8"+Chr(10) msg=msg+"Shall I move all game files to propper directories?" MoveFiles=2 While NextDirectoryEntry(1) If DirectoryEntryType(1) = #PB_DirectoryEntry_File fn.s=DirectoryEntryName(1) ext.s=UCase(GetExtensionPart(fn)) If ext="SMS" If MoveFiles=2 If MessageRequester("Waffle Sodoku",msg,#MB_YESNO | #MB_ICONQUESTION)=#IDYES MoveFiles=3 If CreateDirectory(MasterSave)=0 MessageRequester("Waffle Sodoku Error","Unable to create Master directory"+Chr(10)+MasterSave,#MB_ICONSTOP) MoveFiles=1 Break 1 EndIf If CreateDirectory(GameSave)=0 MessageRequester("Waffle Sodoku Error","Unable to create Save directory"+Chr(10)+GameSave,#MB_ICONSTOP) MoveFiles=1 Break 1 EndIf Else MoveFiles=0 Break 1 EndIf EndIf ;move file If CopyFile(RunPath+fn,MasterSave+fn) ;Debug "file "+fn+" copied" DeleteFile(RunPath+fn) EndIf ElseIf ext="SSV" If MoveFiles=2 If MessageRequester("Waffle Sodoku",msg,#MB_YESNO | #MB_ICONQUESTION)=#IDYES MoveFiles=3 If CreateDirectory(MasterSave)=0 MessageRequester("Waffle Sodoku Error","Unable to create Master directory"+Chr(10)+MasterSave,#MB_ICONSTOP) MoveFiles=1 Break 1 EndIf If CreateDirectory(GameSave)=0 MessageRequester("Waffle Sodoku Error","Unable to create Save directory"+Chr(10)+GameSave,#MB_ICONSTOP) MoveFiles=1 Break 1 EndIf Else MoveFiles=0 Break 1 EndIf EndIf ;move file If CopyFile(RunPath+fn,GameSave+fn) ;Debug "file "+fn+" copied" DeleteFile(RunPath+fn) EndIf EndIf EndIf Wend If MoveFiles=2 MoveFiles=3 If CreateDirectory(MasterSave)=0 MessageRequester("Waffle Sodoku Error","Unable to create Master directory"+Chr(10)+MasterSave,#MB_ICONSTOP) MoveFiles=1 EndIf If CreateDirectory(GameSave)=0 MessageRequester("Waffle Sodoku Error","Unable to create Save directory"+Chr(10)+GameSave,#MB_ICONSTOP) MoveFiles=1 EndIf EndIf WritePreferenceString("MoveFiles",Str(MoveFiles)) FinishDirectory(1) Else MessageRequester("Waffle Sodoku Error","Unable to examine directory "+Chr(10)+RunPath,#MB_ICONSTOP) MoveFiles=0 EndIf EndIf If MoveFiles<2 MasterSave=RunPath GameSave=RunPath EndIf ClosePreferences() Procedure SaveString(Key.s,S.s) ;this wraps preferences for easy use OpenPreferences(RunPath+"WSodoku.ini") WritePreferenceString(Key,S) ;Debug "Saving "+Key+","+S ClosePreferences() EndProcedure Procedure.l CheckMove(gid,v) xy=gid-#BString_0 y=Int(xy)/9 x=xy-(9*y) For c2=0 To 8 g=((9*y)+c2)+#BString_0 If g<>gid If Val(GetGadgetText(G))=v ProcedureReturn #False EndIf EndIf g=((9*c2)+x)+#BString_0 If g<>gid If Val(GetGadgetText(G))=v ProcedureReturn #False EndIf EndIf Next c2 ;check if box is free of value gx=Int(x/3) gy=Int(y/3) bx=gx*3 by=gy*3 ex=bx+2 ey=by+2 ;Debug "Box Test X="+Str(bx)+Str(ex)+" Y="+Str(by)+Str(ey) For tx=bx To ex For ty=by To ey g=((9*ty)+tx)+#BString_0 If g<>gid If Val(GetGadgetText(G))=v ProcedureReturn #False EndIf EndIf Next ty Next tx ProcedureReturn #True EndProcedure Procedure ClearGadgets() ;clear string gadgets For SG=#BString_0 To #BString_80 SetGadgetText(SG,"") SetGadgetColor(SG,#PB_Gadget_BackColor,DefColor) SetGadgetColor(SG, #PB_Gadget_FrontColor, 0) SetGadgetData(SG,0) Next SG ;reset buttons For BG=#BSide_0 To #BSide_8 SetGadgetState(BG,0) Next bg For BG=#BTOP_0 To #BTop_8 SetGadgetState(BG,0) Next BG ;and erase undos ClearList(Undo()) EndProcedure Procedure ShowGrid() ;Copy Grid data to Srting Gadgets SG=#BString_0 For Y=0 To 8 For X=0 To 8 If Grid(x,y)\Value SetGadgetText(SG,Str(Grid(x,y)\Value)) If Grid(x,y)\flag SetGadgetColor(SG, #PB_Gadget_FrontColor, 0) SetGadgetColor(SG,#PB_Gadget_BackColor,MasterColor) SetGadgetData(SG,0-Grid(x,y)\Value) Else If Grid(x,y)\Color SetGadgetColor(SG, #PB_Gadget_FrontColor, Grid(x,y)\Color) SetGadgetColor(SG,#PB_Gadget_BackColor,NumberColor) SetGadgetData(SG,0) EndIf EndIf Else SetGadgetText(SG,"") SetGadgetColor(SG,#PB_Gadget_BackColor,DefColor) SetGadgetData(SG,0) EndIf SG + 1 Next x Next y ;DebugGrid() ;CallDebugger EndProcedure Procedure CopyGrid(flag.l) ;copy String Gadgets to Grid ;set flag to 1 if in create mode SG=#BString_0 For Y=0 To 8 For X=0 To 8 Grid(x,y)\Value=Val(GetGadgetText(SG)) If Grid(x,y)\Value If flag Grid(x,y)\Flag=1 Grid(x,y)\Color=0 SetGadgetColor(SG, #PB_Gadget_FrontColor, 0) SetGadgetData(SG,0-Grid(x,y)\Value) SetGadgetColor(SG,#PB_Gadget_BackColor,MasterColor) Else Grid(x,y)\Color=GetGadgetColor(SG,#PB_Gadget_FrontColor) If Grid(x,y)\color=0 Grid(x,y)\flag=1 SetGadgetData(SG,0-Grid(x,y)\Value) SetGadgetColor(SG,#PB_Gadget_BackColor,MasterColor) Else Grid(x,y)\flag=0 SetGadgetData(SG,0) SetGadgetColor(SG,#PB_Gadget_BackColor,NumberColor) EndIf EndIf Else SetGadgetColor(SG,#PB_Gadget_BackColor,DefColor) EndIf SG + 1 Next x Next y ;DebugGrid() ;CallDebugger EndProcedure Procedure UpdateColors() For BG=#BString_0 To #BString_80 V=Val(GetGadgetText(BG)) If V>0 If Marking=0 V=999 EndIf If V=TextNumber SetGadgetColor(BG,#PB_Gadget_BackColor,MarkColor) ElseIf GetGadgetData(BG)<0 SetGadgetColor(BG,#PB_Gadget_BackColor,MasterColor) Else SetGadgetColor(BG,#PB_Gadget_BackColor,NumberColor) EndIf EndIf Next BG EndProcedure Procedure CheckWin() Dim N(9) For GID=#BString_0 To #BString_80 ; If Val(GetGadgetText(GID))<1 ; ProcedureReturn ; EndIf V=Val(GetGadgetText(GID)) N(V)+1 Next GID W=0 For V=1 To 9 If N(V)=9 W+1 SetGadgetText(#BNUM_0 + (V-1),"*"+Str(V)+"*") Else SetGadgetText(#BNUM_0 + (V-1),Str(V)) EndIf Next V If W<>9 ProcedureReturn EndIf GameWon=#True If Win_Mode=#Sound_WON MessageBeep_(#MB_ICONQUESTION) ElseIf Win_Mode=#Sound_WCustom PlaySound(1) EndIf EndProcedure Procedure ChangeGadget(gid,num) Static oldgid Static oldnum If GetGadgetData(gid)<0 ;this is a master reserved spot If Val(GetGadgetText(gid))<>0-GetGadgetData(gid) SetGadgetText(gid,Str(0-GetGadgetData(gid))) EndIf If Sound_Mode<>#Sound_Off If Sound_Mode=#Sound_Custom PlaySound(0) Else MessageBeep_(-1) EndIf EndIf ProcedureReturn EndIf If oldgid=gid If oldnum=num If Val(GetGadgetText(gid))=num ;not changing number ProcedureReturn EndIf EndIf Else oldnum=99 EndIf oldgid=gid If num=10 If GetGadgetText(gid)<>"" LastElement(Undo()) AddElement(Undo()) Undo()\VS=GetGadgetText(gid) Undo()\GID=gid Undo()\Color=GetGadgetColor(gid,#PB_Gadget_FrontColor) SetGadgetText(gid,"") SetGadgetData(gid,0) SetGadgetColor(gid,#PB_Gadget_BackColor,DefColor) IsDirty=#True ProcedureReturn EndIf EndIf If CheckMove(gid,num) ;prepare for undo ;If oldsg<>GadgetID If Str(num)<>GetGadgetText(gid) LastElement(Undo()) AddElement(Undo()) Undo()\VS=GetGadgetText(gid) Undo()\GID=gid Undo()\Color=GetGadgetColor(gid,#PB_Gadget_FrontColor) ;Debug "Mark "+Str(GadgetID)+" "+Undo()\VS ;oldgid=gid If num>9 SetGadgetText(gid,"") oldnum=0 Else oldnum=num SetGadgetText(gid,Str(num)) SetGadgetColor(gid, #PB_Gadget_FrontColor, TextColor) ;SetGadgetColor(GadgetID,#PB_Gadget_BackColor,DefColor) If Marking SetGadgetColor(gid,#PB_Gadget_BackColor,MarkColor) Else SetGadgetColor(gid,#PB_Gadget_BackColor,NumberColor) EndIf SetGadgetData(gid,0) EndIf ;Delay(200) IsDirty=#True CheckWin() EndIf ;EndIf Else If oldnum<>num If Sound_Mode<>#Sound_Off If Sound_Mode=#Sound_Custom PlaySound(0) Else MessageBeep_(-1) EndIf EndIf EndIf oldgid=gid oldnum=num EndIf EndProcedure ;- Event Loop SaveSize.l=0 FlashTime.l=GetTickCount_() Repeat ; Start of the event loop ;if size gets changed, set savesize to last time of resize Event = WaitWindowEvent(1000) ; This line waits until an event is received from Windows WindowID = EventWindow() ; The Window where the event is generated, can be used in the gadget procedures GadgetID = EventGadget() ; Is it a gadget event? EventType = EventType() ; The event type ;You can place code here, and use the result as parameters for the procedures ;- Game Menu If Event = #PB_Event_Menu MenuID = EventMenu() If MenuID = #Game_New If IsDirty CopyGrid(CreateMode) If PromptForSave(CreateMode) IsDirty=0 EndIf EndIf If IsDirty=0 ClearGadgets() LoadGame(1) ShowGrid() CreateMode=0 GameWon=0 CheckWin() EndIf ElseIf MenuID = #Game_Load If IsDirty CopyGrid(CreateMode) If PromptForSave(CreateMode) IsDirty=0 EndIf EndIf If IsDirty=0 ClearGadgets() LoadGame(0) ShowGrid() CreateMode=0 GameWon=0 CheckWin() EndIf ElseIf MenuID = #Game_Save If IsDirty CopyGrid(CreateMode) EndIf If SaveGame(CreateMode) IsDirty=0 CreateMode=0 EndIf ;- Skill Level ElseIf MenuID = #Game_L1 Skill=0 SaveString("Skill","0") SetMenuItemState(#MenuBar_0,#Game_L1,1) SetMenuItemState(#MenuBar_0,#Game_L2,0) SetMenuItemState(#MenuBar_0,#Game_L3,0) SetMenuItemState(#MenuBar_0,#Game_L4,0) ElseIf MenuID = #Game_L2 Skill=1 SaveString("Skill","1") SetMenuItemState(#MenuBar_0,#Game_L1,0) SetMenuItemState(#MenuBar_0,#Game_L2,1) SetMenuItemState(#MenuBar_0,#Game_L3,0) SetMenuItemState(#MenuBar_0,#Game_L4,0) ElseIf MenuID = #Game_L3 Skill=2 SaveString("Skill","2") SetMenuItemState(#MenuBar_0,#Game_L1,0) SetMenuItemState(#MenuBar_0,#Game_L2,0) SetMenuItemState(#MenuBar_0,#Game_L3,1) SetMenuItemState(#MenuBar_0,#Game_L4,0) ElseIf MenuID = #Game_L4 Skill=3 SaveString("Skill","3") SetMenuItemState(#MenuBar_0,#Game_L1,0) SetMenuItemState(#MenuBar_0,#Game_L2,0) SetMenuItemState(#MenuBar_0,#Game_L3,0) SetMenuItemState(#MenuBar_0,#Game_L4,1) ;- User Colors ElseIf MenuID = #Game_DBColor ;empty back color cs.l=ColorRequester(DefColor) If cs>=0 SaveString("DefColor",Str(cs)) DefColor=cs UpdateColors() EndIf ElseIf MenuID = #Game_OColor ;master number back color cs.l=ColorRequester(MasterColor) If cs>=0 SaveString("MasterColor",Str(cs)) MasterColor=cs UpdateColors() EndIf ElseIf MenuID = #Game_NColor ;number back color cs.l=ColorRequester(NumberColor) If cs>=0 SaveString("NumberColor",Str(cs)) NumberColor=cs UpdateColors() EndIf ElseIf MenuID = #Game_HBColor cs=ColorRequester(HIColor) If cs>=0 SaveString("HIColor",Str(cs)) HIColor=cs UpdateColors() EndIf ElseIf MenuID = #Game_MBColor cs=ColorRequester(MarkColor) If cs>=0 SaveString("MarkColor",Str(cs)) MarkColor=cs UpdateColors() EndIf ElseIf MenuID = #Game_Create If IsDirty CopyGrid(CreateMode) If PromptForSave(CreateMode) IsDirty=0 EndIf EndIf If IsDirty=0 ClearGadgets() NewGame() ShowGrid() CreateMode=0 GameWon=0 CheckWin() EndIf ElseIf MenuID = #Game_Enter If IsDirty CopyGrid(CreateMode) If PromptForSave(CreateMode) IsDirty=0 EndIf EndIf If IsDirty=0 ClearGadgets() CreateMode=#True GameWon=0 CheckWin() EndIf ElseIf MenuID = #Game_Solve If IsDirty CopyGrid(CreateMode) If PromptForSave(CreateMode) IsDirty=0 EndIf EndIf If IsDirty=0 If SolveGame() MessageRequester("Waffle's Sodoku","Game solved") ShowGrid() GameWon=#True Else MessageRequester("Waffle's Sodoku","No Solution Found") CopyGrid(CreateMode) GameWon=0 EndIf CreateMode=#False EndIf ElseIf MenuID = #Game_Exit If IsDirty CopyGrid(CreateMode) If PromptForSave(CreateMode) IsDirty=0 EndIf EndIf If IsDirty=0 ;CloseWindow(#Window_Sodoku) Break 1 EndIf ;- Undo Menu ElseIf MenuID = #Undo_Undo If LastElement(Undo()) SG=Undo()\GID If SG<0 ;this is a block undo DeleteElement(Undo()) While LastElement(Undo()) SG=Undo()\GID If sg<0 DeleteElement(Undo()) Break 1 EndIf SetGadgetText(SG,Undo()\VS) If Val(Undo()\VS) SetGadgetColor(SG,#PB_Gadget_BackColor,NumberColor) Else SetGadgetColor(SG,#PB_Gadget_BackColor,DefColor) EndIf SetGadgetColor(sg,#PB_Gadget_FrontColor,Undo()\Color) SetGadgetData(SG,0) DeleteElement(Undo()) OldSG=SG Wend Else ;Debug "Undo "+Str(SG)+" "+GetGadgetText(SG)+" to "+Undo()\VS SetGadgetText(SG,Undo()\VS) If Val(Undo()\VS) SetGadgetColor(SG,#PB_Gadget_BackColor,NumberColor) Else SetGadgetColor(SG,#PB_Gadget_BackColor,DefColor) EndIf SetGadgetColor(sg,#PB_Gadget_FrontColor,Undo()\Color) SetGadgetData(SG,0) DeleteElement(Undo()) OldSG=SG EndIf IsDirty=#True GameWon=0 CheckWin() EndIf ElseIf MenuID = #Undo_Delete ;prepare for block undo LastElement(Undo()) AddElement(Undo()) Undo()\GID=-1 For GID=#BString_0 To #BString_80 If TextNumber=Val(GetGadgetText(GID)) If GetGadgetData(GID)>=0 LastElement(Undo()) AddElement(Undo()) Undo()\VS=GetGadgetText(gid) Undo()\GID=gid Undo()\Color=GetGadgetColor(gid,#PB_Gadget_FrontColor) SetGadgetText(GID,"") SetGadgetColor(GID,#PB_Gadget_BackColor,DefColor) SetGadgetData(GID,0) IsDirty=#True EndIf EndIf Next GID LastElement(Undo()) AddElement(Undo()) Undo()\GID=-1 GameWon=0 CheckWin() ;ClearList(Undo()) ElseIf MenuID = #Undo_Color LastElement(Undo()) AddElement(Undo()) Undo()\GID=-1 For GID=#BString_0 To #BString_80 If GetGadgetColor(GID,#PB_Gadget_FrontColor)=TextColor If GetGadgetData(GID)>=0 LastElement(Undo()) AddElement(Undo()) Undo()\VS=GetGadgetText(gid) Undo()\GID=gid Undo()\Color=GetGadgetColor(gid,#PB_Gadget_FrontColor) SetGadgetText(GID,"") SetGadgetColor(GID,#PB_Gadget_BackColor,DefColor) SetGadgetData(GID,0) IsDirty=#True EndIf EndIf Next GID LastElement(Undo()) AddElement(Undo()) Undo()\GID=-1 GameWon=0 CheckWin() ;ClearList(Undo()) ElseIf MenuID = #Undo_Reset LastElement(Undo()) AddElement(Undo()) Undo()\GID=-1 For GID=#BString_0 To #BString_80 If GetGadgetData(GID)>=0 LastElement(Undo()) AddElement(Undo()) Undo()\VS=GetGadgetText(gid) Undo()\GID=gid Undo()\Color=GetGadgetColor(gid,#PB_Gadget_FrontColor) SetGadgetText(GID,"") SetGadgetColor(GID,#PB_Gadget_BackColor,DefColor) SetGadgetData(GID,0) IsDirty=#True EndIf Next GID ;ClearList(Undo()) LastElement(Undo()) AddElement(Undo()) Undo()\GID=-1 GameWon=0 CheckWin() ;- Sound Menu ElseIf MenuID = #Sound_On ;Sound_Mode=MenuID If GetMenuItemState(#Menubar_0,#Sound_Custom) Sound_Mode=#Sound_Custom Else Sound_Mode=#Sound_Default SetMenuItemState(#Menubar_0,#Sound_Default,1) EndIf SetMenuItemState(#Menubar_0,MenuID,1) SetMenuItemState(#Menubar_0,#Sound_off,0) SaveString("Sound",Str(Sound_Mode)) ElseIf MenuID = #Sound_Off Sound_Mode=MenuID SetMenuItemState(#Menubar_0,MenuID,1) SetMenuItemState(#Menubar_0,#Sound_On,0) If IsSound(0) StopSound(0) EndIf SaveString("Sound",Str(Sound_Mode)) Win_Mode=#Sound_WOff SetMenuItemState(#Menubar_0,#Sound_WOff,1) SetMenuItemState(#Menubar_0,#Sound_WON,0) SetMenuItemState(#Menubar_0,#Sound_WCustom,0) If IsSound(1) StopSound(1) EndIf SaveString("WinSound",Str(Win_Mode)) ElseIf MenuID = #Sound_Custom s.s=OpenFileRequester("Select Sound to use",Sound_Custom,"*.wav|*.wav|*.*|*.*",0) If s If LoadSound(0,s) Sound_Custom=s Sound_Mode=MenuID SetMenuItemState(#Menubar_0,MenuID,1) SetMenuItemState(#Menubar_0,#Sound_On,1) SetMenuItemState(#Menubar_0,#Sound_Default,0) SaveString("Sound",Str(Sound_Mode)) SaveString("Custom",Sound_Custom) Else MessageRequester("Waffle's Sodoku","Unable to load sound"+Chr(10)+s,#MB_ICONSTOP) EndIf EndIf ElseIf MenuID = #Sound_Default Sound_Mode=MenuID SetMenuItemState(#Menubar_0,MenuID,1) SetMenuItemState(#Menubar_0,#Sound_On,1) SetMenuItemState(#Menubar_0,#Sound_Custom,0) SaveString("Sound",Str(Sound_Mode)) If IsSound(0) StopSound(0) EndIf ElseIf MenuID = #Sound_WOff Win_Mode=MenuID SetMenuItemState(#Menubar_0,#Sound_WOff,1) SetMenuItemState(#Menubar_0,#Sound_WON,0) SetMenuItemState(#Menubar_0,#Sound_WCustom,0) If IsSound(1) StopSound(1) EndIf SaveString("WinSound",Str(Win_Mode)) ElseIf MenuID = #Sound_WON Win_Mode=MenuID SetMenuItemState(#Menubar_0,#Sound_WOff,0) SetMenuItemState(#Menubar_0,#Sound_WON,1) SetMenuItemState(#Menubar_0,#Sound_WCustom,0) If IsSound(1) StopSound(1) EndIf SaveString("WinSound",Str(Win_Mode)) ElseIf MenuID = #Sound_WCustom s.s=OpenFileRequester("Select Sound to use",Win_Custom,"*.wav|*.wav|*.*|*.*",0) If s If LoadSound(1,s) Win_Custom=s Win_Mode=MenuID SetMenuItemState(#Menubar_0,#Sound_WOff,0) SetMenuItemState(#Menubar_0,#Sound_WON,0) SetMenuItemState(#Menubar_0,#Sound_WCustom,1) SaveString("WinSound",Str(Win_Mode)) SaveString("WinCustom",Win_Custom) Else MessageRequester("Waffle's Sodoku","Unable to load sound"+Chr(10)+s,#MB_ICONSTOP) EndIf EndIf ;- Help Menu ElseIf MenuID = #Help_Rules ;ShowRules() GetHelp("#Rules") ElseIf MenuID = #Help_Help ;ShowHelp() GetHelp("#Contents") ElseIf MenuID = #Help_Menu ;ShowMenu() GetHelp("#Game") ElseIf MenuID = #Help_About CallDebugger ShowAbout() EndIf EndIf ;- String Grid If Event = #PB_Event_Gadget If ( GadgetID >= #BString_0 ) And ( GadgetID<= #BString_80 ) ;If (eventtype=#PB_EventType_LeftClick) Or (eventtype=#PB_EventType_Change) ;If TextNumber=0 ; CallDebugger ;EndIf ;If TextNumber=10 ; CallDebugger ;EndIf If eventtype=#PB_EventType_Focus ;CallDebugger ChangeGadget(GadgetID,TextNumber) ElseIf eventtype=#PB_EventType_Change ;CallDebugger ;ensure only numeric data fixflag.l=0 gt.s=GetGadgetText(GadgetID) If Len(gt)>1 gt=Left(gt,1) fixflag=1 EndIf If Val(gt)=0 If gt<>"" gt="" fixflag=1 EndIf EndIf If fixflag SetGadgetText(GadgetID,gt) EndIf ChangeGadget(GadgetID,Val(gt)) EndIf ;- Vert highlight ElseIf ( GadgetID >= #BTop_0 ) And ( GadgetID<= #BTop_8 ) ;vertical highlight SG=(GadgetID-#BTop_0)+#BString_0 HS=GetGadgetState(GadgetID) For x=1 To 9 If GetGadgetData(SG)>=0 GD=(GetGadgetData(SG)&2)+HS SetGadgetData(SG,GD) If GD If GetGadgetText(SG)="" SetGadgetColor(SG,#PB_Gadget_BackColor,HIColor) Else SetGadgetData(SG,0) EndIf Else If GetGadgetData(SG)<0 SetGadgetColor(SG,#PB_Gadget_BackColor,MasterColor) ElseIf Val(GetGadgetText(SG)) SetGadgetColor(SG,#PB_Gadget_BackColor,NumberColor) Else SetGadgetColor(SG,#PB_Gadget_BackColor,DefColor) EndIf EndIf EndIf SG + 9 Next x ;- Horizontal Highlight ElseIf ( GadgetID >= #BSide_0 ) And ( GadgetID<= #BSide_8 ) ;horizontal highlight SG=((GadgetID-#BSide_0)*9)+#BString_0 HS=2*GetGadgetState(GadgetID) For x=1 To 9 If GetGadgetData(SG)>=0 GD=(GetGadgetData(SG)&1)+HS SetGadgetData(SG,GD) If GD If GetGadgetText(SG)="" SetGadgetColor(SG,#PB_Gadget_BackColor,HIColor) Else SetGadgetData(SG,0) EndIf Else If GetGadgetData(SG)<0 SetGadgetColor(SG,#PB_Gadget_BackColor,MasterColor) ElseIf Val(GetGadgetText(SG)) SetGadgetColor(SG,#PB_Gadget_BackColor,NumberColor) Else SetGadgetColor(SG,#PB_Gadget_BackColor,DefColor) EndIf EndIf EndIf SG + 1 Next x ;- Number Select ElseIf ( GadgetID >= #BNUM_0 ) And ( GadgetID<= #BNUM_8 ) For BG=#BNum_0 To #BNum_8 If GadgetID=BG TextNumber.l=(GadgetID-#BNum_0)+1 ;SetGadgetColor(GadgetID,#PB_Gadget_BackColor,Color(6)) SetGadgetState(GadgetID,1) Else SetGadgetState(BG,0) ;SetGadgetColor(GadgetID,#PB_Gadget_BackColor,1) EndIf Next BG ;and some extra hilights ... If TextNumber>0 For BG=#BString_0 To #BString_80 V=Val(GetGadgetText(BG)) If V>0 If Marking=0 V=999 EndIf If V=TextNumber SetGadgetColor(BG,#PB_Gadget_BackColor,MarkColor) ElseIf GetGadgetData(BG)<0 SetGadgetColor(BG,#PB_Gadget_BackColor,MasterColor) Else SetGadgetColor(BG,#PB_Gadget_BackColor,NumberColor) EndIf Else SetGadgetColor(BG,#PB_Gadget_BackColor,DefColor) SetGadgetData(BG,0) EndIf Next BG EndIf ;and undo all highlight buttons For BG=#BTop_0 To #BTop_8 SetGadgetState(BG,0) Next BG For BG=#BSide_0 To #BSide_8 SetGadgetState(BG,0) Next BG ;- Color Select ElseIf ( GadgetID >= #BImage_0 ) And ( GadgetID<= #BImage_8 ) TextColor.l=Color(GadgetID-#BImage_0) SetGadgetColor(#Text_0, #PB_Gadget_FrontColor, TextColor) For BG=#BImage_0 To #BImage_8 If GadgetID=BG SetState(GadgetID,1) Else SetState(BG,0) ;SetGadgetColor(GadgetID,#PB_Gadget_BackColor,1) EndIf Next BG ElseIf GadgetID=#BMark Marking=GetGadgetState(GadgetID) If TextNumber>0 For BG=#BString_0 To #BString_80 V=Val(GetGadgetText(BG)) If V>0 If Marking=0 V=999 EndIf If V=TextNumber SetGadgetColor(BG,#PB_Gadget_BackColor,MarkColor) ElseIf GetGadgetData(BG)<0 SetGadgetColor(BG,#PB_Gadget_BackColor,MasterColor) Else SetGadgetColor(BG,#PB_Gadget_BackColor,NumberColor) EndIf EndIf Next BG EndIf EndIf EndIf If Event=#PB_Event_SizeWindow ;CallDebugger SaveSize=GetTickCount_() ;Debug "SaveSize set to "+Str(SaveSize) ResizeGame(0,0) ElseIf SaveSize ;CallDebugger TT.l=Abs(GetTickCount_()-SaveSize) If TT>1000 SaveSize=0 ;Debug "SaveSize set to 0" SaveString("Width",Str(WindowWidth(#Window_Sodoku))) SaveString("Height",Str(WindowHeight(#Window_Sodoku))) EndIf EndIf ;- Game Won Loop If GameWon If FlashTime9 LastNumber=1 EndIf For BG=#BString_0 To #BString_80 V=Val(GetGadgetText(BG)) If V=LastNumber SetGadgetColor(BG,#PB_Gadget_BackColor,MarkColor) ElseIf GetGadgetData(BG)<0 SetGadgetColor(BG,#PB_Gadget_BackColor,MasterColor) Else SetGadgetColor(BG,#PB_Gadget_BackColor,NumberColor) EndIf Next BG EndIf EndIf If Event=#PB_Event_CloseWindow If IsDirty CopyGrid(CreateMode) If PromptForSave(CreateMode)=0 Event=0 EndIf EndIf EndIf Until Event = #PB_Event_CloseWindow ; End of the event loop CloseHelper() End