How to resolve the algorithm Galton box animation step by step in the XPL0 programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Galton box animation step by step in the XPL0 programming language

Table of Contents

Problem Statement

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

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.

Generate an animated simulation of a Galton device.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Galton box animation step by step in the XPL0 programming language

Source code in the xpl0 programming language

include c:\cxpl\codes;          \intrinsic code declarations
define  Balls = 80;             \maximum number of balls
int     Bx(Balls), By(Balls),   \character cell coordinates of each ball
        W, I, J, Peg, Dir;
[W:= Peek($40, $4A);            \get screen width in characters
Clear;  CrLf(6);  CrLf(6);
for Peg:= 1 to 10 do                            \draw pegs
        [for I:= 1 to 12-Peg do ChOut(6,^ );    \space over to first peg
         for I:= 1 to Peg do [ChOut(6,^.);  ChOut(6,^ )];
        CrLf(6);
        ];
for J:= 0 to 12-1 do                            \draw slots
        [for I:= 0 to 12-1 do [ChOut(6,^:);  ChOut(6,^ )];
        CrLf(6);
        ];
for I:= 0 to 23-1 do ChOut(6,^.);               \draw bottom
for I:= 0 to Balls-1 do                         \make source of balls at top
        [Bx(I):= 11;  By(I):= 1];
Attrib($C);                                     \make balls bright red
repeat                                          \balls away! ...
    for I:= 0 to Balls-1 do                     \for all the balls ...
        [Cursor(Bx(I), By(I));  ChOut(6, ^ );   \erase ball's initial position
        if Peek($B800, (Bx(I)+(By(I)+1)*W)*2) = ^ \is ball above empty location?
        then    By(I):= By(I)+1                 \yes: fall straight down
        else    [Dir:= Ran(3)-1;                \no: randomly fall right or left
                if Peek($B800, (Bx(I)+Dir+(By(I)+1)*W)*2) = ^  then
                        [Bx(I):= Bx(I)+Dir;  By(I):= By(I)+1];
                ];
        Cursor(Bx(I), By(I));  ChOut(6, ^o);    \draw ball at its new position
        ];
    Sound(0, 3, 1);                             \delay about 1/6 second
until KeyHit;                                   \continue until a key is struck
]

  

You may also check:How to resolve the algorithm Guess the number/With feedback (player) step by step in the UNIX Shell programming language
You may also check:How to resolve the algorithm Anagrams/Deranged anagrams step by step in the Clojure programming language
You may also check:How to resolve the algorithm Bitmap/Bresenham's line algorithm step by step in the D programming language
You may also check:How to resolve the algorithm Comments step by step in the Nemerle programming language
You may also check:How to resolve the algorithm MAC vendor lookup step by step in the zkl programming language