view 5-Erlang/infinirun.erl @ 101:1fae0cca1ef8

Reduce large maze to single width corridors This reduces the permutations for a x x x b x To one (two steps north) from four (two steps north; one east, two north, one west; one east, one north, one west, one north; and one north, one east, one north, one west). Longer corridors were worse! We would filter this in the "been here before via another path" but that's still a lot of lookups in lists, which is inefficient.
author IBBoard <dev@ibboard.co.uk>
date Sun, 14 Jul 2019 13:42:24 +0100
parents 7bab4843aec6
children
line wrap: on
line source

-module(infinirun).
-export([loop/0]).

% To run, call: Restarter = spawn(fun infinirun:loop/0).
% Then call Restarter ! new to initiate
% This does black magic and registers a public atom called "revolver", which is a process running roulette:loop/0 that can receive messages
loop() ->
    % Indicate that we want exist signals, which are a 3-tuple of { 'EXIT', From, Reason }
    process_flag(trap_exit, true),
    receive
        new ->
            % Start a new process
            io:format("Creating and monitoring process.~n"),
            register(revolver, spawn_link(fun roulette:loop/0)),
            loop();
        {'EXIT', From, Reason} ->
            io:format("The shooter ~p died with reason ~p. Restarting…~n", [From, Reason]),
            % Send ourselves a message to register a new process
            self() ! new,
            loop()
end.