Microsoft Small Basic

Program Listing: LJH960
GraphicsWindow.Hide()
Title = "crackers"
PATH = "http://www.crew99.com/crackers/"
true = " - You are a WINNER"
false = ""
win = false
duration = 250
move = 0
button = true
btnup = true
gfxwidth = 500
gfxheight = 500
gfxwidth2 = gfxwidth / 2
gfxheight2 = gfxheight / 2
'------------------------------------------------------------------------------ LOAD GFX
For a = 1 to 4
For b = 1 To 4
If (b <> 4 or a <> 4) then
puzzle[a-1][b-1] = Shapes.AddImage(PATH + "puzzle_" + a + "_" + b + ".png")
field[a-1][b-1] = (a-1)*4 + b
Shapes.Move(puzzle[a-1][b-1], (b-1)*125, (a-1)*125)
Else
field[a-1][b-1] = 0
EndIf
EndFor
EndFor
shape_frame[0] = Shapes.AddLine(0,0,125,0)
shape_frame[1] = Shapes.AddLine(0,125,125,125)
shape_frame[2] = Shapes.AddLine(125,0,125,125)
shape_frame[3] = Shapes.AddLine(0,0,0,125)
For i = 0 To 3
Shapes.HideShape(shape_frame[i])
EndFor
btnImage = ImageList.LoadImage(PATH + "btnGo1.png")
btnWidth = ImageList.GetWidthOfImage(btnImage)
btnHeight = ImageList.GetHeightOfImage(btnImage)
btn1 = Shapes.AddImage(btnImage)
Shapes.HideShape(btn1)
btn2 = Shapes.AddImage(PATH + "btnGo2.png")
Shapes.HideShape(btn2)

'------------------------------------------------------------------------------ INIT WINDOW
GraphicsWindow.CanResize = "false"
GraphicsWindow.Title = Title
GraphicsWindow.Width = gfxwidth
GraphicsWindow.Height = gfxheight
GraphicsWindow.Left = (Desktop.Width - 500) / 2
GraphicsWindow.Top = (Desktop.Height - 500) / 2
GraphicsWindow.BackgroundColor = "Black"
GraphicsWindow.MouseMove = MouseMove
GraphicsWindow.MouseUp = MouseClick
GraphicsWindow.Show()

Shapes.Move(btn1, GraphicsWindow.Width / 2 - btnWidth / 2, GraphicsWindow.Height / 2 - btnHeight / 2)
Shapes.Move(btn2, GraphicsWindow.Width / 2 - btnWidth / 2, GraphicsWindow.Height / 2 - btnHeight / 2)
Shapes.ShowShape(btn1)
pic = false
'------------------------------------------------------------------------------ UPDATE ON MOUSE MOVE
'Highlight the picture below the cursor and highlight the button.
Sub MouseMove
If button = true then
mx = GraphicsWindow.MouseX
my = GraphicsWindow.MouseY
bb = Math.SquareRoot((gfxwidth2 - mx)*(gfxwidth2 - mx) + (gfxheight2 - my)*(gfxheight2 - my))
if bb < 74 and btnup = true then
Shapes.ShowShape(btn2)
Shapes.HideShape(btn1)
btnup = false
elseif bb > 74 and btnup = false then
Shapes.ShowShape(btn1)
Shapes.HideShape(btn2)
btnup = true
endif
else
x = math.Floor(GraphicsWindow.MouseX / 125) '* 125
y = math.Floor(GraphicsWindow.MouseY / 125) '* 125
For i = 0 To 3
Shapes.Move(shape_frame[i], x*125, y*125)
EndFor
EndIf
EndSub
'------------------------------------------------------------------------------ UPDATE ON MOUSE BUTTON UP
Sub MouseClick
If (x-1 >= 0) And (field[y][x-1] = 0) then
move = move + 1
field[y][x-1] = field[y][x]
field[y][x] = 0
puzzle[y][x-1] = puzzle[y][x]
Shapes.Animate(puzzle[y][x], (x-1)*125, y*125, duration)
ElseIf (x+1 <= 3) And (field[y][x+1] = 0) then
move = move + 1
field[y][x+1] = field[y][x]
field[y][x] = 0
puzzle[y][x+1] = puzzle[y][x]
Shapes.Animate(puzzle[y][x], (x+1)*125, y*125, duration)
ElseIf (y-1 >= 0) And (field[y-1][x] = 0) then
move = move + 1
field[y-1][x] = field[y][x]
field[y][x] = 0
puzzle[y-1][x] = puzzle[y][x]
Shapes.Animate(puzzle[y][x], x*125, (y-1)*125, duration)
ElseIf (y+1 <= 3) And (field[y+1][x] = 0) then
move = move + 1
field[y+1][x] = field[y][x]
field[y][x] = 0
puzzle[y+1][x] = puzzle[y][x]
Shapes.Animate(puzzle[y][x], x*125, (y+1)*125, duration)
EndIf
CheckWin() 'after moving , check if the picture is already complete

If button = true and btnup = false then
button = false
Shapes.HideShape(btn2)
For f = 1 To 500
Shuffle() 'shuffle the picture
EndFor
For i = 0 To 3 'draw frame
Shapes.ShowShape(shape_frame[i])
EndFor
EndIf
EndSub
'------------------------------------------------------------------------------ CHECK IF THE PICTURE IS COMPLETED
Sub CheckWin
if move > 0 then
win = true
For a = 1 to 4
For b = 1 To 4
If (b <> 4 or a <> 4) then
If field[a-1][b-1] <> (a-1)*4 + b then
win = false
EndIf
EndIf
EndFor
EndFor
EndIf
GraphicsWindow.Title = Title + " " + win
EndSub
'------------------------------------------------------------------------------ SHUFFLE THE PICTURE
Sub Shuffle
px = 0
py = 0

GetNull()

if xn = 0 Then
px = (Math.GetRandomNumber(3-1) + 1) - 2
elseif xn = 3 Then
px = (Math.GetRandomNumber(3-1)) - 2
else
px = (Math.GetRandomNumber(3)) - 2
EndIf

If px = 0 then
While py = 0
if yn = 0 Then
py = (Math.GetRandomNumber(3-1) + 1) - 2
elseif yn = 3 Then
py = (Math.GetRandomNumber(3-1)) - 2
else
py = (Math.GetRandomNumber(3)) - 2
EndIf
EndWhile
EndIf

x = xn + px
y = yn + py

For i = 0 To 3
Shapes.Move(shape_frame[i], x*125, y*125)
EndFor

MouseClick() ' simulate a click to move the part
EndSub
'------------------------------------------------------------------------------ Get the empty field
Sub GetNull
For a = 1 to 4
For b = 1 To 4
If field[a-1][b-1] = 0 then
xn = b-1
yn = a-1
EndIf
EndFor
EndFor
EndSub