I have made a start on the following RosettaCode problem:
http://rosettacode.org/wiki/Galton_box_animation

Does anyone have ideas on how to improve the current ASCII "animation"?
Other suggestioned improvements are welcome.
Feel free to come up with totally different solutions too.

NB.*dropBalls v Simulate Galton Box
NB. y is: number of balls to drop
NB. x is: number of pins in last row of Galton Box (default 5)
dropBalls=: 3 : 0
 5 dropBalls y
:
 maxpins=. x
 nballs=. y
 Pins=: 2 * (,~ 0 , 2 = 3 +/\ {.)^:(<:maxpins) ,:(0 1 $~ >:@+:) maxpins
 GBox=: 0, (2+maxpins){.Pins  NB. pad Pins
 ballidx=. balls=. 0 #~ #GBox
 final=. 0$0
 for_ball. i. nballs+#GBox do.
   newball=. 0 1 {~ ball < nballs
   final=. final , balls #&{: ballidx
   balls=. newball , }: balls
   ballidx=. maxpins , }: ballidx
   deflection=. 0, 0 0 ,~ _1 1{~ maxpins ?@$ 2
   ballidx=. ballidx + deflection
   echo ' o*' {~ balls (([: <"1 i.@# ,.]) ballidx)}GBox
   echo /:~ ({.,#)/.~ final
   echo ' '
 end.
 final
)

Here was an earlier idea using rotate.

NB.*dropBalls2 v Simulate Galton Box
NB. y is: number of balls to drop
NB. x is: number of pins in last row of Galton Box (default 5)
dropBalls2=: 3 : 0
 5 dropBalls2 y
:
 maxpins=. x
 nballs=. y
 GBox=. (0 $~ 3&+ , >:@+:) maxpins
 Pins=. 2* 0,~ 0,~(,~ 0 , 2 = 3 +/\ {.)^:(maxpins) ,:(0 1 $~ >:@+:) maxpins
 newball=. 1 maxpins}{:GBox
 sums=. {: GBox
 for_ball. i. nballs+#GBox do.
   if. ball >: y do. newball=. (0 #~ {:@$) GBox end.
   sums=. sums + {: GBox
   deflection=. 0, 0 0 ,~ _1 1{~ maxpins ?@$ 2
   GBox=. newball , }: GBox
   echo ' .*' {~Pins+GBox=. deflection |."0 1 GBox
   echo sums
   echo ' '
 end.
)
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to