From 86662d10ab670d7a2453af4a3d6f7f686067086e Mon Sep 17 00:00:00 2001 From: =?utf8?q?Am=C3=A9lia=20Coutard-Sander?= Date: Thu, 12 Dec 2024 23:46:51 +0100 Subject: [PATCH] =?utf8?q?Impl=C3=A9mentation=20basique=20de=20l'=C3=A9val?= =?utf8?q?uateur=20d'automates?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- automata/automata.ml | 18 ++++++++++++++++++ automata/automata.mli | 10 ++++++++++ 2 files changed, 28 insertions(+) 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 -- 2.46.0