From: Amélia Coutard-Sander Date: Thu, 12 Dec 2024 22:46:51 +0000 (+0100) Subject: Implémentation basique de l'évaluateur d'automates X-Git-Url: https://git.ameliathe1st.gay/?a=commitdiff_plain;h=86662d10ab670d7a2453af4a3d6f7f686067086e;p=cells.git Implémentation basique de l'évaluateur d'automates --- diff --git a/automata/automata.ml b/automata/automata.ml index 639bcee..7b90be5 100644 --- a/automata/automata.ml +++ b/automata/automata.ml @@ -31,4 +31,22 @@ module type Automaton = sig val color : t -> char * char * char end +(* TODO: infinite board *) +type 't board = 't * 't array array + +let initial (type t) (m : (module Automaton with type t = t)) = + let module M = (val m) in + (M.default, Array.make_matrix 64 64 M.default) + +let get x y (d, board) = if x < 0 || 63 < x || y < 0 || 63 < y then d else board.(x).(y) + +let set x y c (_, board) = if x < 0 || 63 < x || y < 0 || 63 < y then () else board.(x).(y) <- c + +let update (type t) (m : (module Automaton with type t = t)) ((d, cells) as board) = + let module M = (val m) in + ( M.transition (List.map (Fun.const d) M.neighbours) d, + Array.init 64 (fun x -> + Array.init 64 (fun y -> + M.transition (List.map (fun (dx, dy) -> get (x + dx) (y + dy) board) M.neighbours) cells.(x).(y))) ) + let automata = [(module Life : Automaton); (module Wireworld : Automaton)] diff --git a/automata/automata.mli b/automata/automata.mli index 40a67b3..2e1d96a 100644 --- a/automata/automata.mli +++ b/automata/automata.mli @@ -31,4 +31,14 @@ module type Automaton = sig val color : t -> char * char * char end +type 't board + +val initial : (module Automaton with type t = 't) -> 't board + +val get : int -> int -> 't board -> 't + +val set : int -> int -> 't -> 't board -> unit + +val update : (module Automaton with type t = 't) -> 't board -> 't board + val automata : (module Automaton) list