Microsoft Small Basic

Program Listing: LVP196
' Dice Probability
' Versioin 0.1a
' Copyright © 2018 Nonki Takahashi. The MIT License.

title = "Dice Probability"
GraphicsWindow.Title = title
Form()
Controls.ButtonClicked = OnButtonClicked
Timer.Tick = OnTick
Timer.Interval = 1000
Timer.Pause()
While "True"
If buttonClicked Then
nDice = Controls.GetTextBoxText(tboxDice)
Timer.Resume()
t = Clock.ElapsedMilliseconds
DrawGraph()
t = Math.Floor((Clock.ElapsedMilliseconds - t) / 1000)
Timer.Pause()
GraphicsWindow.Title = title + " | " + t + "s"
buttonClicked = "False"
Else
Program.Delay(200)
EndIf
EndWhile

Sub Dec2Sen
' Decimal to senary conversion
' param dec - decimal
' param sen - senary
Stack.PushValue("local", dec)
sen = ""
While 0 < dec
rem = Math.Remainder(dec, 6)
sen = Text.Append(rem, sen)
dec = Math.Floor(dec / 6)
EndWhile
dec = Stack.PopValue("local")
EndSub

Sub DrawGraph
' param nDice
score = ""
cmax = Math.Power(6, nDice) - 1
For case = 0 To cmax
dec = case
Dec2Sen()
s = 0
For d = 1 To nDice
len = Text.GetLength(sen)
If len < d Then
s = s + 1
Else
s = s + Text.GetSubText(sen, d, 1) + 1
EndIf
EndFor
score[s] = score[s] + 1
EndFor
n = Array.GetItemCount(score)
index = Array.GetAllIndices(score)
max = 0
For i = 1 To n
s = index[i]
If max < score[s] Then
max = score[s]
EndIf
EndFor
w = Math.Floor(gw / (n + 1))
x1 = Math.Floor((gw - (n * (w + 1))) / 2)
x2 = x1 + (n * (w + 1))
y1 = gh
y2 = 40
i = 1
GraphicsWindow.BrushColor = "White"
GraphicsWindow.FillRectangle(0, 36, gw , gh - 36)
GraphicsWindow.BrushColor = "Blue"
For x = x1 To x2 Step w + 1
s = index[i]
h = Math.Floor(score[s] * (y1 - y2) / max)
y = y1 - h
GraphicsWindow.FillRectangle(x, y, w, h)
i = i + 1
EndFor
EndSub

Sub Form
gw = 598
gh = 428
GraphicsWindow.Width = gw
GraphicsWindow.Height = gh
GraphicsWindow.BrushColor = "LightGray"
GraphicsWindow.FillRectangle(0, 0, gw, 36)
GraphicsWindow.BrushColor = "Black"
GraphicsWindow.DrawText(10, 10, "Number of dice")
tboxDice = Controls.AddTextBox(110, 7)
Controls.SetSize(tboxDice, 50, 22)
Controls.AddButton("OK", 170, 5)
EndSub

Sub OnButtonClicked
buttonClicked = "True"
EndSub

Sub OnTick
p = Math.Floor(case / cmax * 1000) / 10
l = Math.Floor((Clock.ElapsedMilliseconds - t) / 1000)
GraphicsWindow.Title = title + " | " + l + "s (" + p + "%)"
EndSub