Galton box animation: Difference between revisions

m
(→‎{{header|REXX}}: added a prompt so the user knows what to do when the simulation is frozen on a specific step.)
Line 1,699:
 
=={{header|Julia}}==
This is a proof of concept code. It does not use any external packages. The ASCII animation is running in the stdout terminal, which has to be at least 28 character wide, and 40 character high, and accept ANSI control sequences for positioning the cursor, and clearing the screen (likee.g. the Windows console, or ConEmu).
 
6 pins in 6 rows are hard coded. The next ball is released at the press of the Enter key. Keep it depressed for continuous running.
The balls are randomly deflected left or right at hitting the pins, and they fall to the bins at the bottom, which extend downwards.
The timer function sets the speed of the animation. Change the "interval" parameter to larger values for slower movement.
Pressing x then Enter exits, other keys are ignored.
{{works with|Julia|1.0}}
 
Line 1,710:
function drawball(timer)
global r, c, d
print("\e[$r;$(c)H ") # clear last ball position (r,c)
if (r +=1) > 14
close(timer)
b = (bin[(c+2)>>2] += 1)# update count in bin
print("\e[$b;$(c)Ho") # lengthen bar of balls in bin
else
r in 3:2:13 && c in 17-r:4:11+r && (d = 2bitrand(1)[1]-1)
print("\e[$r;$(c+=d)Ho")# show ball moving in random direction d
end
end
Line 1,728:
 
bin = fill(15,7) # positions of top of bins
while "x" != readline() >= "" # x-Enter: exit, {keys..}Enter: next ball
global r,c,d = 0,14,0
t = Timer(drawball, 0.0, interval = 0.1)
while r < 15 sleep(0.01) end
print("\e[40;1H") # move cursor far down