Microsoft Small Basic

Program Listing: MDB491-0
' Dice
' Copyright © 2014 Nonki Takahashi. The MIT License.
' Version 0.3
' Last update 2014-12-19
' History: Created from Backgammon Board 0.1a.
' Program ID MDB491-0
'
GraphicsWindow.Title = "Dice 0.3"
Form()
Dice_Init()
x0 = gw / 2
y0 = gh / 2
r = size
clicked = "False"
Controls.ButtonClicked = OnButtonClicked
While "True"
If clicked Then
Dice_RemoveAll()
If Array.ContainsValue(btn, btnClicked) Then
param["array"] = btn
param["value"] = btnClicked
Array_GetIndexOf()
n = index
i = 1
x = x0 - size / 2
y = y0 - size / 2
Dice_Add()
ElseIf btnClicked = btnRandom Then
a = Math.GetRandomNumber(360)
_a[1] = Math.GetRadians(a)
_a[2] = _a[1] - Math.Pi
For i = 1 To 2
n = Math.GetRandomNumber(6)
x = x0 + r * Math.Cos(_a[i]) - size / 2
y = y0 + r * Math.Sin(_a[i]) - size / 2
Dice_Add()
EndFor
EndIf
clicked = "False"
Else
Program.Delay(200)
EndIf
EndWhile
Sub Form
gw = 598
gh = 428
GraphicsWindow.Width = gw
GraphicsWindow.Height = gh
GraphicsWindow.BackgroundColor = "Green"
GraphicsWindow.BrushColor = "White"
GraphicsWindow.FontName = "MS Trebuchet"
GraphicsWindow.BrushColor = "Black"
For n = 1 To 6
btn[n] = Controls.AddButton(n, 10 + (n - 1) * 24, 8)
EndFor
btnRandom = Controls.AddButton("Random", 10, 48)
EndSub
Sub OnButtonClicked
clicked = "True"
btnClicked = Controls.LastClickedButton
EndSub
Sub Array_GetIndexOf
' param["array"] - the array to check
' param["value"] - the value to check
_n = Array.GetItemCount(param["array"])
_index = Array.GetAllIndices(param["array"])
For _i = 1 To _n
If param["array"][_index[_i]] = param["value"] Then
index = _index[_i]
_i = _n ' break
EndIf
EndFor
EndSub
Sub Dice_Add
GraphicsWindow.PenWidth = 0
GraphicsWindow.BrushColor = "White"
dice[i][0] = Shapes.AddRectangle(size, size)
Shapes.Move(dice[i][0], x, y)
k = 0
For j = 1 To 9
If Text.GetSubText(pattern[n], j, 1) = "." Then
k = k + 1
If n = 1 Then
GraphicsWindow.BrushColor = "Red"
d = size / 3
p = 0.35
Else
GraphicsWindow.BrushColor = "Black"
d = size / 5
p = 0.6
EndIf
x1 = x + (Math.Remainder(j - 1, 3) + p) * size / 4
y1 = y + (Math.Floor((j - 1) / 3) + p) * size / 4
dice[i][k] = Shapes.AddEllipse(d, d)
Shapes.Move(dice[i][k], x1, y1)
EndIf
EndFor
EndSub
Sub Dice_Init
pattern[1] = " . "
pattern[2] = ". ."
pattern[3] = " . . . "
pattern[4] = ". . . ."
pattern[5] = ". . . . ."
pattern[6] = ". .. .. ."
size = 40
EndSub
Sub Dice_RemoveAll
nDice = Array.GetItemCount(dice)
For i = 1 To nDice
nSpot = Array.GetItemCount(dice[i])
For k = 1 To nSpot - 1
Shapes.Remove(dice[i][k])
EndFor
Shapes.Remove(dice[i][0])
EndFor
EndSub