How to resolve the algorithm Wireworld step by step in the OCaml programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Wireworld step by step in the OCaml programming language

Table of Contents

Problem Statement

Wireworld is a cellular automaton with some similarities to Conway's Game of Life. It is capable of doing sophisticated computations with appropriate programs (it is actually Turing complete), and is much simpler to program for. A Wireworld arena consists of a Cartesian grid of cells, each of which can be in one of four states. All cell transitions happen simultaneously. The cell transition rules are this:

Create a program that reads a Wireworld program from a file and displays an animation of the processing. Here is a sample description file (using "H" for an electron head, "t" for a tail, "." for a conductor and a space for empty) you may wish to test with, which demonstrates two cycle-3 generators and an inhibit gate: While text-only implementations of this task are possible, mapping cells to pixels is advisable if you wish to be able to display large designs. The logic is not significantly more complex.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Wireworld step by step in the OCaml programming language

Source code in the ocaml programming language

let w = [|
    "  ......tH              ";
    " .        ......        ";
    "  ...Ht...      .       ";
    "               ....     ";
    "               .  ..... ";
    "               ....     ";
    "  tH......      .       ";
    " .        ......        ";
    "  ...Ht...              ";
  |]

let is_head w x y =
  try if w.(x).[y] = 'H' then 1 else 0
  with _ -> 0

let neighborhood_heads w x y =
  let n = ref 0 in
  for _x = pred x to succ x do
    for _y = pred y to succ y do
      n := !n + (is_head w _x _y)
    done;
  done;
  (!n)

let step w =
  let n = Array.init (Array.length w) (fun i -> String.copy w.(i)) in
  let width = Array.length w
  and height = String.length w.(0)
  in
  for x = 0 to pred width do
    for y = 0 to pred height do
      n.(x).[y] <- (
        match w.(x).[y] with
        | ' ' -> ' '
        | 'H' -> 't'
        | 't' -> '.'
        | '.' ->
            (match neighborhood_heads w x y with
            | 1 | 2 -> 'H'
            | _ -> '.')
        | _ -> assert false)
    done;
  done;
  (n)

let print = (Array.iter print_endline)

let () =
  let rec aux w =
    Unix.sleep 1;
    let n = step w in
    print n;
    aux n
  in
  aux w


  

You may also check:How to resolve the algorithm Idiomatically determine all the lowercase and uppercase letters step by step in the XPL0 programming language
You may also check:How to resolve the algorithm HTTPS/Authenticated step by step in the LiveCode programming language
You may also check:How to resolve the algorithm Include a file step by step in the Mathematica / Wolfram Language programming language
You may also check:How to resolve the algorithm Truth table step by step in the PARI/GP programming language
You may also check:How to resolve the algorithm Priority queue step by step in the Axiom programming language