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