Game Of Life

post image

Game Of Life Rules :

A board with equal size square boxes called cells. Each cell is alive when its bright and dead when its not. For a cell to be alive it must have 2 or more neighbouring cells that are alive too else it dies. If it has one alive neighbour it dies, no neighbour, it dies. A cell is born when a cell has exactly three live neighbours.

So usually, this is played in a infinite universe and an infinite environment space with unlimited memory and computing power that none of us has access to so building it on a periodic-repeating single plane universe when the edges are connected to its parallel ones.

Langauges:

Rust, WebAssembly and Javascript

the only god tier languages I know in existence
the only god tier languages I know in existence

Why Javascript?

  • can directly read and write to WebAssembly limear memory space

  • Garbage-collector heap

  • considers and returns scalar

What is wasm_bindgen

  • used to box Rust structures

  • wrapping pointers in javascript class for usability

  • basically clears boundries

Game Plan

Well, here comes the binary! the best part. The whole universe is mapped cell to byte into a flat one dimensional array where the live cell is represented by 0 and a dead cell is represented by 1.

Consider a 4 x 4 universe : 16 cells in total mapped into an flat array looks like this →

count index 0
count index 0

How is it mapped? good question! An index function is used to allocate the cell environment into a byte memory reserved in the universe like this:

index(row, column, universe) = row * width(universe)

WebAssembly LIner Memoy ?→ Rust String → Javascripts Garbage collector Heap → HTML textContent

code source

As you notice, a macro #[repr(u8)] is used so as to represent each cell with a single byte.