How to resolve the algorithm Galton box animation step by step in the Zig programming language
How to resolve the algorithm Galton box animation step by step in the Zig 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 Zig programming language
Source code in the zig programming language
const std = @import("std");
const rand = std.rand;
const time = std.time;
const PEG_LINES = 20;
const BALLS = 10;
fn boardSize(comptime peg_lines: u16) u16 {
var i: u16 = 0;
var size: u16 = 0;
inline while (i <= peg_lines) : (i += 1) {
size += i + 1;
}
return size;
}
const BOARD_SIZE = boardSize(PEG_LINES);
fn stepBoard(board: *[BOARD_SIZE]u1, count: *[PEG_LINES + 1]u8) void {
var prng = rand.DefaultPrng.init(@bitCast(time.timestamp()));
var p: u8 = 0;
var sum: u16 = 0;
while (p <= PEG_LINES) : (p += 1) {
const pegs = PEG_LINES - p;
var i: u16 = 0;
while (i < pegs + 1) : (i += 1) {
if (pegs != PEG_LINES and board[BOARD_SIZE - 1 - sum - i] == 1) {
if (prng.random().boolean()) {
board.*[BOARD_SIZE - 1 - sum - i + pegs + 1] = 1;
} else {
board.*[BOARD_SIZE - 1 - sum - i + pegs + 2] = 1;
}
} else if (pegs == PEG_LINES and board[BOARD_SIZE - 1 - sum - i] == 1) {
count.*[pegs - i] += 1;
}
board.*[BOARD_SIZE - 1 - sum - i] = 0;
}
sum += pegs + 1;
}
}
fn printBoard(board: *[BOARD_SIZE]u1, count: *[PEG_LINES + 1]u8) !void {
const stdout = std.io.getStdOut();
_ = try stdout.write("\x1B[2J\x1B[1;1H");
var pegs: u16 = 0;
var sum: u16 = 0;
while (pegs <= PEG_LINES) : (pegs += 1) {
var i: u16 = 0;
while (i < (PEG_LINES - pegs)) : (i += 1) _ = try stdout.write(" ");
i = 0;
while (i < pegs + 1) : (i += 1) {
const spot = if (board[i + sum] == 1) "o" else " ";
_ = try stdout.write(spot);
if (i != pegs) _ = try stdout.write("*");
}
sum += pegs + 1;
_ = try stdout.write("\n");
}
for (count) |n| {
const num_char = [2]u8{'0' + n, ' '};
_ = try stdout.write(&num_char);
}
_ = try stdout.write("\n");
}
pub fn main() !void {
var board: [BOARD_SIZE]u1 = [_]u1{0} ** BOARD_SIZE;
var bottom_count: [PEG_LINES+1]u8 = [_]u8{0} ** (PEG_LINES + 1);
var i: u16 = 0;
while (i < PEG_LINES + BALLS + 1) : (i += 1) {
if (i < BALLS) board[0] = 1;
try printBoard(&board, &bottom_count);
stepBoard(&board, &bottom_count);
time.sleep(150000000);
}
}
You may also check:How to resolve the algorithm File input/output step by step in the Logo programming language
You may also check:How to resolve the algorithm Guess the number/With feedback (player) step by step in the R programming language
You may also check:How to resolve the algorithm Greatest element of a list step by step in the ooRexx programming language
You may also check:How to resolve the algorithm Sorting algorithms/Heapsort step by step in the jq programming language
You may also check:How to resolve the algorithm Attractive numbers step by step in the Wren programming language