Microsoft Small Basic

Program Listing: MDB491-1
' Dice
' Copyright © 2014-2018 Nonki Takahashi. The MIT License.
' Version 0.4
' Last update 2018-02-02
' Program ID MDB491-1

title = "Dice 0.4"
GraphicsWindow.Title = title
Form()
Dice_Init()
GraphicsWindow.BrushColor = "Green"
GraphicsWindow.FillEllipse(gw - size * 2 - 44 - 10, 10, size * 2 + 44, size * 2 + 44)
x0 = gw - size - 22 - 10
y0 = 10 + size + 22
r = size * 0.75
clicked = "False"
While total < 2000
Dice_RemoveAll()
a = Math.GetRandomNumber(360)
_a[1] = Math.GetRadians(a)
_a[2] = _a[1] - Math.Pi
sum = 0
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()
sum = sum + n
EndFor
CountSum()
Program.Delay(0)
EndWhile

Sub CountSum
total = total + 1
GraphicsWindow.Title = title + " | " + total
count[sum] = count[sum] + 1
x = xl + (sum - 2) * (width + gap)
y = yb - count[sum]
GraphicsWindow.BrushColor = "#CC0000FF"
GraphicsWindow.FillRectangle(x, y, width, 1)
For s = 2 To 12
x = xl + (s - 2) * (width + gap)
y = yb - count[s]
probability = Math.Round(count[s] / total * 100) + "%"
Shapes.SetText(prob[s], probability)
If Text.GetLength(probability) = 2 Then
k = 0.3
Else
k = 0.2
EndIf
Shapes.Move(prob[s], x + width * k, y - 20)
EndFor
EndSub

Sub Form
gw = 598
gh = 428
GraphicsWindow.Width = gw
GraphicsWindow.Height = gh
GraphicsWindow.BrushColor = "White"
GraphicsWindow.FontName = "MS Trebuchet"
GraphicsWindow.BrushColor = "DimGray"
width = 40
gap = 1
sum = 2
xl = (gw - (width * 11 + gap * 10)) / 2
k = 0.4
For sum = 2 To 12
x = xl + (sum - 2) * (width + gap)
If 9 < sum Then
k = 0.3
EndIf
GraphicsWindow.DrawText(x + width * k, gh - 20, sum)
prob[sum] = Shapes.AddText("0%")
Shapes.Move(prob[sum], x + width * 0.3, gh - 44)
EndFor
count = 0
GraphicsWindow.PenColor = "Gray"
yb = gh - 24
For y = yb To 0 Step -50
GraphicsWindow.DrawLine(0, y, gw, y)
GraphicsWindow.DrawText(0, y, count)
count = count + 50
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 = 30
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