 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.


package require Tcl 8.6
 
oo::class create GaltonBox {
    variable b w h n x y cnt step dropping
 
    constructor {BALLS {NUMPEGS 5} {HEIGHT 24}} {
	set n $NUMPEGS
	set w [expr {$n*2 + 1}]
	set h $HEIGHT
	puts -nonewline "\033\[H\033\[J"
	set x [set y [lrepeat $BALLS 0]]
	set cnt 0
	set step 0
	set dropping 1
 
	set b [lrepeat $h [lrepeat $w " "]]
	for {set i 0} {$i < $n} {incr i} {
	    for {set j [expr {-$i}]} {$j <= $i} {incr j 2} {
		lset b [expr {2*$i+2}] [expr {$j+$w/2}] "*"
	    }
	}
    }
 
    method show {} {
	puts -nonewline "\033\[H"
	set oldrow {}
	foreach row $b {
	    foreach char $row oldchar $oldrow {
		if {$char ne "*"} {
		    puts -nonewline "$char "
		} elseif {$oldchar eq " "} {
		    puts -nonewline "\033\[32m*\033\[m "
		} else {
		    puts -nonewline "\033\[31m*\033\[m "
		}
	    }
	    set oldrow $row
	    puts ""
	}
    }
 
    method Move idx {
	set xx [lindex $x $idx]
	set yy [lindex $y $idx]
	set kill 0
 
	if {$yy < 0} {return 0}
	if {$yy == $h-1} {
	    lset y $idx -1
	    return 0
	}
 
	switch [lindex $b [incr yy] $xx] {
	    "*" {
		incr xx [expr {2*int(2 * rand()) - 1}]
		if {[lindex $b [incr yy -1] $xx] ne " "} {
		    set dropping 0
		}
	    }
	    "o" {
		incr yy -1
		set kill 1
	    }
	}
 
	set c [lindex $b [lindex $y $idx] [lindex $x $idx]]
	lset b [lindex $y $idx] [lindex $x $idx] " "
	lset b $yy $xx $c
	if {$kill} {
	    lset y $idx -1
	} else {
	    lset y $idx $yy
	}
	lset x $idx $xx
	return [expr {!$kill}]
    }
 
    method step {} {
	set moving 0
	for {set i 0} {$i < $cnt} {incr i} {
	    set moving [expr {[my Move $i] || $moving}]
	}
	if {2 == [incr step] && $cnt < [llength $x] && $dropping} {
	    set step 0
	    lset x $cnt [expr {$w / 2}]
	    lset y $cnt 0
	    if {[lindex $b [lindex $y $cnt] [lindex $x $cnt]] ne " "} {
		return 0
	    }
	    lset b [lindex $y $cnt] [lindex $x $cnt] "o"
	    incr cnt
	}
	return [expr {($moving || $dropping)}]
    }
}
 
GaltonBox create board 1024 {*}$argv
while true {
    board show
    if {[board step]} {after 60} break
}