Microsoft Small Basic

Program Listing: GLK004-0
'this is to demonstrate the major malfunctions of the "global scope"
' variables, that have just been checked for a certain condition (if)
' change value inside closed condition
' run this and click a few times into the GraphicsWindow
'no field has to be drawn outside that pattern and pointer has just to
'point at those lightblue squares - nowhere else is told to point than on
'current Field (FD). But how comes it points to Fields that do not exist?



' create simple sudoku-square (9*9)
Fieldsize = 32
Fieldspace = 8

Left = 0
Top = 1

BorderWidth = 40 ' extra big border to see what happens
gwSize = 2 * BorderWidth + 9 * Fieldsize + 8 * Fieldspace

GraphicsWindow.Width = gwSize
GraphicsWindow.Height = gwSize

' add some shape as a pointer
GraphicsWindow.PenWidth = Fieldspace / 2
GraphicsWindow.PenColor ="Red"
GraphicsWindow.BrushColor ="gray"
Pointer = Shapes.AddRectangle(Fieldsize+Fieldspace, Fieldsize+Fieldspace)
Shapes.SetOpacity(Pointer, 50)


For iRow = 1 To 9
For iColumn = 1 To 9
FD[iColumn + 10 * iRow][Left] = BorderWidth + (Fieldsize + Fieldspace) * (iColumn-1)
FD[iColumn + 10 * iRow][Top] = BorderWidth + (Fieldsize + Fieldspace) * (iRow -1)
EndFor
EndFor
GraphicsWindow.Show()

Timer.Interval = 500
Timer.Tick = Draw
GraphicsWindow.MouseDown = Click

currentField = 11

'initial drawing
GraphicsWindow.BrushColor = "lightBlue"
Draw()


Sub Click
For i = 11 To 99
If i/10 <> Math.Floor(i/10) Then
' during this loop i gets changed by Draw() !
If GraphicsWindow.MouseX >= FD[i][Left] And GraphicsWindow.MouseX <= FD[i][Left] + Fieldsize then
If GraphicsWindow.MouseY >= FD[i][Top] and GraphicsWindow.MouseY <= FD[i][Top] + Fieldsize then
currentField = i
Goto exitClick
EndIf
EndIf
EndIf
EndFor
exitClick:

EndSub


Sub Draw
For i = 11 To 99
If i/10 <> Math.Floor(i/10) Then
' inside this condition i gets changed by Click()!
GraphicsWindow.FillRectangle(FD[i][Left], FD[i][Top], Fieldsize, Fieldsize)
EndIf
EndFor
Shapes.Move(Pointer, FD[currentField][Left] - Fieldspace/2, FD[currentField][Top] - Fieldspace/2)
EndSub