Microsoft Small Basic

Program Listing: ZZL953-0
' How many different ways to go down 12 steps with 1 or 2 steps?
' Copyright (c) 2013 Nonki Takahshi. All right reserved.
'
CRLF = Text.GetCharacter(13) + Text.GetCharacter(10)
GraphicsWindow.Title = "How many different ways to go down 12 steps?"
GraphicsWindow.BackgroundColor = "Ivory"
gw = 640
gh = 480
GraphicsWindow.Width = gw
GraphicsWindow.Height = gh
x0 = 10
y0 = gh - 140
GraphicsWindow.BrushColor = "Gray"
GraphicsWindow.FillRectangle(0, y0, gw, 200)
h = 14
For i = 1 To 12
y = y0 - i * h
w = (13 - i) * (2 * h)
GraphicsWindow.FillRectangle(x0, y, w, h)
EndFor
GraphicsWindow.PenWidth = 0
GraphicsWindow.BrushColor = "Green"
ball["obj"] = Shapes.AddEllipse(2 * h, 2 * h)
ball["x"] = 0
ball["y"] = 0
GraphicsWindow.BrushColor = "Black"
GraphicsWindow.FontSize = 30
textobj = Shapes.AddText("1:1-1-1-1-1-1-1-1-1-1-1-1")
Shapes.Move(textobj, x0, y0 + 40)
GraphicsWindow.FontSize = 10
list = Controls.AddMultiLineTextBox(gw - 210, 10)
Controls.SetSize(list, 200, gh - 20)
buf = ""
num = 0 ' number of patterns
wstep = 2 ' with steps
For n = 2047 To 0 Step -1
DecToBin()
BinToSteps()
If match Then
buf = buf + t + CRLF
If num <= 40 THen
Controls.SetTextBoxText(list, buf)
EndIf
Shapes.SetText(textobj, t)
ns = Array.GetItemCount(steps)
is = Array.GetAllIndices(steps)
For i = 1 To ns
s = steps[is[i]]
MoveBall()
EndFor
EndIf
EndFor
Controls.SetTextBoxText(list, buf)
Sub MoveBall
' param s - step number of stairs
x2 = x0 + (12 - s) * 2 * h
y2 = y0 - (s + 2) * h
x1 = (ball["x"] + x2) / 2
y1 = Math.Min(ball["y"], y2) - h
Shapes.Move(ball["obj"], x1, y1)
Program.Delay(msec)
Shapes.Move(ball["obj"], x2, y2)
Program.Delay(msec)
ball["x"] = x2
ball["y"] = y2
EndSub
Sub DecToBin
' param n - decimal number
' return b - binary number string
Stack.PushValue("local", n)
b = ""
While 0 < n
b = Text.Append(Math.Remainder(n, 2), b)
n = Math.Floor(n / 2)
EndWhile
If b = "" Then
b = "0"
EndIf
n = Stack.PopValue("local")
EndSub
Sub BinToSteps
' param b - binary number string
' return match - if having only 1 or 2 steps
' return t - text of steps
' return steps[] - array of step number
len = Text.GetLength(b)
If len < 11 Then
b = Text.Append(Text.GetSubText("0000000000", 1, 11 - len), b)
EndIf
b = Text.Append(b, "1")
num = num + 1
If 1 <= num And num <= 10 Then
msec = 100
ElseIf 11 <= num And num <= 40 Then
msec = 10
Else
msec = 0
EndIf
match = "True"
t = num + ": "
steps = "0=12;"
last = 12
For i = 1 To 12
cs = 12 - i
If Text.GetSubText(b, i, 1) = "1" Then
j = last - cs
If wstep < j Then
match = "False"
num = num - 1
Goto break
EndIf
If last = 12 Then
t = t + j
Else
t = t + "-" + j
EndIf
steps[i] = cs
last = cs
EndIf
EndFor
break:
EndSub