Microsoft Small Basic

Program Listing: SDZ133
'size of puzzle
maxx=3
maxy=3

GraphicsWindow.BrushColor="Blue"
GraphicsWindow.DrawBoundText(5,5,290,"Press S to shuffle the puzzle")
GraphicsWindow.DrawBoundText(5,20,290,"Press 1 for one single Shuffle move")
GraphicsWindow.DrawBoundText(5,35,290,"Click a piece with the mouse to move it")

GraphicsWindow.width=350
GraphicsWindow.Height=350


'create puzzle
CreatePuzzle()

' shuffle pieces
' For i=1 to 64
' ShufflePuzzle()
' program.delay(200)
' endfor


GraphicsWindow.MouseMove=OnMousemove
GraphicsWindow.MouseDown=OnMouseDown
GraphicsWindow.KeyDown=OnKeyDown

While "true"
If shuffleCount>0 Then
ShufflePuzzle()
program.delay(200)
shuffleCount=ShuffleCount-1
endif

endwhile

Sub createPuzzle
x=1
y=1
For i=1 To maxx*maxy-1

img=ImageList.LoadImage("http://www.li-do.nl/userfiles/apple33"+y+x+".jpg")
square[x][y]=shapes.AddImage(img)
Shapes.Move(square[x][y],50+x*50,50+y*50)
x=x+1
If x>maxx Then
y=y+1
x=1
endif

endfor
freex=maxx
freey=maxy
lastx=0
lasty=0
endsub



sub ShufflePuzzle
valid=0
while valid=0
dx=math.GetRandomNumber(3)-2
dy=math.GetRandomNumber(3)-2

If ((dx=0) Or (dy=0)) And (dx<>dy) And (freex+dx>=1) And (freex+dx<=maxx) And (freey+dy>=1) And (freey+dy<=maxy)Then
valid=1
endif

If (freex+dx=lastx) And (freey+dy=lasty) Then
valid=0
endif

If valid=1 then
lastx=freex
lasty=freey
endif

endwhile
'exchange (freex,freey) and (freex+dx,freey+dy)

t=square[freex+dx][freey+dy]
square[freex][freey]=t
Shapes.animate(square[freex][freey],50+freex*50,50+freey*50,200)
freex=freex+dx
freey=freey+dy
endsub

Sub onMousemove
a=GraphicsWindow.MouseX
b=GraphicsWindow.MouseY
a=a-50
a=a/55
a=math.floor(a)

b=b-50
b=b/55
b=math.floor(b)

'GraphicsWindow.BrushColor="white"
'GraphicsWindow.fillRectangle(5,5,290,20)
'GraphicsWindow.BrushColor="Blue"
'GraphicsWindow.DrawBoundText(5,5,290,a+", "+b)
endsub

Sub onMouseDown
a=GraphicsWindow.MouseX
b=GraphicsWindow.MouseY
a=a-50
a=a/55
a=math.floor(a)

b=b-50
b=b/55
b=math.floor(b)

If (math.abs(freex-a)=1 And (freey=b)) or ((freex=a) And math.abs(freey-b)=1) Then
t=square[a][b]
square[freex][freey]=t
Shapes.animate(square[freex][freey],50+freex*50,50+freey*50,500)
freex=a
freey=b
endif
EndSub

Sub OnKeyDown
' shuffle pieces
key=GraphicsWindow.LastKey
If key="S" then
ShuffleCount= 64
ElseIf key="D1" then
shuffleCount =1
endif
EndSub