 Example of a Galton Box at the end of animation.


A   Galton device   Sir Francis Galton's device   is also known as a   bean machine,   a   Galton Board,   or a   quincunx.



Description of operation

In a Galton box, there are a set of pins arranged in a triangular pattern.   A number of balls are dropped so that they fall in line with the top pin, deflecting to the left or the right of the pin.   The ball continues to fall to the left or right of lower pins before arriving at one of the collection points between and to the sides of the bottom row of pins.

Eventually the balls are collected into bins at the bottom   (as shown in the image),   the ball column heights in the bins approximate a   bell curve.   Overlaying   Pascal's triangle   onto the pins shows the number of different paths that can be taken to get to each bin.



Task

Generate an animated simulation of a Galton device.



Task requirements

   The box should have at least 5 pins on the bottom row. 
   A solution can use graphics or ASCII animation. 
   Provide a sample of the output/display such as a screenshot.
   There can be one or more balls in flight at the same time. 
   If multiple balls are in flight, ensure they don't interfere with each other.
   A solution should allow users to specify the number of balls, or it should run until full or a preset limit. 
   Optionally,   display the number of balls.


bola$ = "0000ff"
obst$ = "000000"
 
maxBalls = 10
cx = 1
cy = 2
dim Balls(maxBalls, 2)
 
open window 600,600
window origin "ct"
 
maxh = peek("winheight")
 
REM Draw the pins:
 
FOR row = 1 TO 7
  FOR col = 1 TO row
    FILL circle 40*col - 20*row, 40*row+80, 10
  NEXT col
NEXT row
 
REM Animate
tick = 0
bolas = 0
color 0,0,255
 
do
	if (bolas < maxBalls) then
		if tick = 3 then
			tick = 0
			bolas = bolas + 1
			Balls(bolas, cx) = 20
			Balls(bolas, cy) = 10
		end if
		tick = tick + 1
	end if
	for n = 1 to bolas
		if Balls(n, cy) then
			color$ = right$(getbit$(Balls(n,cx),Balls(n,cy) + 10,Balls(n,cx),Balls(n,cy) + 10),6)
			if (color$ = bola$) or (Balls(n,cy) >= maxh - 15) then
				Balls(n,cy) = 0
				break
			end if
			clear fill circle Balls(n,cx),Balls(n,cy),10
			if color$ = obst$ then
				if int(ran(2)) then
					Balls(n,cx) = Balls(n,cx) - 20
				else
					Balls(n,cx) = Balls(n,cx) + 20
				end if
			end if
			Balls(n,cy) = Balls(n,cy)+10
			fill circle Balls(n,cx),Balls(n,cy),10
			wait .001
		else
			wait .001
		end if
	next n
loop
 