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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Galton box animation step by step in the Wren 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 Wren programming language

Source code in the wren programming language

import "random" for Random
import "./iterate" for Reversed

var boxW = 41       // Galton box width.
var boxH = 37       // Galton box height.
var pinsBaseW = 19  // Pins triangle base.
var nMaxBalls = 55  // Number of balls.

var centerH = pinsBaseW + (boxW - pinsBaseW * 2 + 1) / 2 - 1
var Rand = Random.new()

class Cell {
    static EMPTY  { " " }
    static BALL   { "o" }
    static WALL   { "|" }
    static CORNER { "+" }
    static FLOOR  { "-" }
    static PIN    { "." }
}

/* Galton box. Will be printed upside down. */
var Box = List.filled(boxH, null)
for (i in 0...boxH) Box[i] = List.filled(boxW, Cell.EMPTY)

class Ball {
    construct new(x, y) {
        if (Box[x][y] != Cell.EMPTY) Fiber.abort("The cell at (x, y) is not empty.")
        Box[y][x] = Cell.BALL
        _x = x
        _y = y
    }

    doStep() {
        if (_y <= 0) return  // Reached the bottom of the box.
        var cell = Box[_y - 1][_x]
        if (cell == Cell.EMPTY) {
            Box[_y][_x] = Cell.EMPTY
            _y = _y - 1
            Box[_y][_x] = Cell.BALL
        } else if (cell == Cell.PIN) {
            Box[_y][_x] = Cell.EMPTY
            _y = _y - 1
            if (Box[_y][_x - 1] == Cell.EMPTY && Box[_y][_x + 1] == Cell.EMPTY) {
                _x = _x + Rand.int(2) * 2 - 1
                Box[_y][_x] = Cell.BALL
                return
            } else if (Box[_y][_x - 1] == Cell.EMPTY){
                _x = _x + 1
            } else _x = _x - 1
            Box[_y][_x] = Cell.BALL
        } else {
            // It's frozen - it always piles on other balls.
        }
    }
}

var initializeBox = Fn.new {
    // Set ceiling and floor:
    Box[0][0] = Cell.CORNER
    Box[0][boxW - 1] = Cell.CORNER
    for (i in 1...boxW - 1) Box[0][i] = Cell.FLOOR
    for (i in 0...boxW) Box[boxH - 1][i] = Box[0][i]

    // Set walls:
    for (r in 1...boxH - 1) {
        Box[r][0] = Cell.WALL
        Box[r][boxW - 1] = Cell.WALL
    }
 
    // Set pins:
    for (nPins in 1..pinsBaseW) {
        for (pin in 0...nPins) {
            Box[boxH - 2 - nPins][centerH + 1 - nPins + pin * 2] = Cell.PIN
        }
    }
}

var drawBox = Fn.new() {
    for (row in Reversed.new(Box, 1)) {
        for (c in row) System.write(c)
        System.print()
    }
}

initializeBox.call()
var balls = []
for (i in 0...nMaxBalls + boxH) {
    System.print("\nStep %(i):")
    if (i < nMaxBalls) balls.add(Ball.new(centerH, boxH - 2))  // Add ball.
    drawBox.call()

    // Next step for the simulation.
    // Frozen balls are kept in balls list for simplicity
    for (b in balls) b.doStep()
}


  

You may also check:How to resolve the algorithm Sleep step by step in the Forth programming language
You may also check:How to resolve the algorithm LZW compression step by step in the Ada programming language
You may also check:How to resolve the algorithm Date format step by step in the PL/I programming language
You may also check:How to resolve the algorithm Hamming numbers step by step in the Forth programming language
You may also check:How to resolve the algorithm Linear congruential generator step by step in the Picat programming language