Galton box animation: Difference between revisions

No edit summary
Line 1,697:
| |o|o|o|o|o|o|o| |
| |o|o|o|o|o|o|o| |</pre>
 
=={{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 (like 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.
{{works with|Julia|1.0}}
 
<lang julia>using Random
function drawball(timer)
global r, c, d
print("\e[$r;$(c)H ") # clear last ball position
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
end
end
 
print("\e[2J") # clear screen
for r = 3:2:13, c = 17-r:4:11+r # 6 pins in 6 lines
print("\e[$r;$(c)H^") # draw pins
end
print("\e[15;1H--------------------------")
 
bin = fill(15,7) # positions of top of bins
while "x" != readline() >= "" # x-Enter: exit, ..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
end</lang>
{{out}}
<pre>
^
 
^ ^
 
^ ^ ^
 
^ ^ ^ ^
 
^ ^ ^ ^ ^
 
^ ^ ^ ^ ^ ^
 
--------------------------
o o o o o o o
o o o o o
o o o o
o o o o
o o o o
o o o
o o
o o
o o
o o
o
o
o
o
o</pre>
 
=={{header|Kotlin}}==