Microsoft Small Basic

Program Listing: LFD194
' Array and Stack
' Copyright (c) 2014 Nonki Takahashi. The MIT License.
'
WQ = Text.GetCharacter(34)
gw = 598
gh = 428
GraphicsWindow.Width = gw
GraphicsWindow.Height = gh
GraphicsWindow.Title = "Array and Stack"
GraphicsWindow.BrushColor = "Gray"
GraphicsWindow.FontName = "Courier New"
op = Shapes.AddText("")
x = gw / 2
y = gh / 2
Shapes.Move(op, x, y)
GraphicsWindow.PenWidth = 0
n = 5
AddColors()
InitStack()
While "True"
PushColors()
PopColors()
EndWhile
Sub AddColors
For ia = 1 To n
c[ia] = GraphicsWindow.GetRandomColor()
Shapes.SetText(op, "a[" + ia + "] = " + c[ia])
GraphicsWindow.BrushColor = c[ia]
a[ia] = Shapes.AddRectangle(50, 10)
x = 50 + (ia - 1) * 60
y = 50
Shapes.Animate(a[ia], x, y, 1000)
GraphicsWindow.BrushColor = "Gray"
GraphicsWindow.DrawText(x + 10, y - 20, "a[" + ia + "]")
Program.Delay(1000)
GraphicsWindow.BrushColor = c[ia]
GraphicsWindow.FillRectangle(x, y, 50, 10)
EndFor
Program.Delay(1000)
EndSub
Sub InitStack
x = 50
y = gh - 50
GraphicsWindow.BrushColor = "Gray"
GraphicsWindow.DrawText(x + 20, y, "s")
EndSub
Sub PushColors
x = 50
For ia = 1 To n
Shapes.SetText(op, "Stack.PushValue(" + WQ + "s" + WQ + ", a[" + ia + "])")
Stack.PushValue("s", a[ia])
Stack.PushValue("c", c[ia])
is = Stack.GetCount("s")
y = gh - 60 - (6 - 1) * 15
Shapes.Animate(a[ia], x, y, 1000)
Program.Delay(1000)
y = gh - 60 - (is - 1) * 15
Shapes.Animate(a[ia], x, y, 1000)
EndFor
Program.Delay(1000)
EndSub
Sub PopColors
For ia = 1 To n
Shapes.SetText(op, "a[" + ia + "] = Stack.PopValue(" + WQ + "s" + WQ + ")")
c[ia] = Stack.PopValue("c")
a[ia] = Stack.PopValue("s")
x = 50
y = gh - 60 - (6 - 1) * 15
Shapes.Animate(a[ia], x, y, 500)
Program.Delay(500)
x = 50 + (ia - 1) * 60
y = 50
Shapes.Animate(a[ia], x, y, 1000)
Program.Delay(1000)
GraphicsWindow.BrushColor = c[ia]
GraphicsWindow.FillRectangle(x, y, 50, 10)
EndFor
EndSub