From 0b480dbf710cf182d0be87f4dfd8870d9ef78813 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Am=C3=A9lia=20Coutard-Sander?= Date: Mon, 16 Dec 2024 23:23:49 +0100 Subject: [PATCH] Le voisinage est maintenant un type abstrait --- automata/automata.ml | 12 ++++++++---- automata/automata.mli | 8 ++++++-- automata/automata/brain.ml | 4 ++++ automata/automata/cont.ml | 4 ++++ automata/automata/life.ml | 4 ++++ automata/automata/maze.ml | 4 ++++ automata/automata/wireworld.ml | 4 ++++ 7 files changed, 34 insertions(+), 6 deletions(-) diff --git a/automata/automata.ml b/automata/automata.ml index 47189dd..8ad1fc4 100644 --- a/automata/automata.ml +++ b/automata/automata.ml @@ -18,9 +18,13 @@ module type Automaton = sig type t - val neighbours : (int * int) list + type 'a neighbours - val transition : t list -> t -> t + val neighbours : (int * int) neighbours + + val map : ('a -> 'b) -> 'a neighbours -> 'b neighbours + + val transition : t neighbours -> t -> t val default : t @@ -50,10 +54,10 @@ let set x y c ((d, cells) as board) = 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, + ( M.transition (M.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))) ) + M.transition (M.map (fun (dx, dy) -> get (x + dx) (y + dy) board) M.neighbours) cells.(x).(y))) ) let automata = [ diff --git a/automata/automata.mli b/automata/automata.mli index 7d3aa7b..073e67c 100644 --- a/automata/automata.mli +++ b/automata/automata.mli @@ -18,9 +18,13 @@ module type Automaton = sig type t - val neighbours : (int * int) list + type 'a neighbours - val transition : t list -> t -> t + val neighbours : (int * int) neighbours + + val map : ('a -> 'b) -> 'a neighbours -> 'b neighbours + + val transition : t neighbours -> t -> t val default : t diff --git a/automata/automata/brain.ml b/automata/automata/brain.ml index 0429a70..1e46cb1 100644 --- a/automata/automata/brain.ml +++ b/automata/automata/brain.ml @@ -17,8 +17,12 @@ let name = "Le Cerveau de Brian" type t = Off | On | Dying +type 'a neighbours = 'a list + let neighbours = [(-1, -1); (-1, 0); (-1, 1); (0, -1); (0, 1); (1, -1); (1, 0); (1, 1)] +let map = List.map + let transition l = function | Off -> let n = List.length (List.filter (( = ) On) l) in diff --git a/automata/automata/cont.ml b/automata/automata/cont.ml index 64c3308..58ec814 100644 --- a/automata/automata/cont.ml +++ b/automata/automata/cont.ml @@ -17,8 +17,12 @@ let name = "Automate continu random" type t = float +type 'a neighbours = 'a list + let neighbours = [(-1, 0); (0, -1); (0, 1); (1, 0)] +let map = List.map + let transition l c = List.fold_left ( +. ) c l /. 5. let default = 0. diff --git a/automata/automata/life.ml b/automata/automata/life.ml index 0324885..ba0b3c1 100644 --- a/automata/automata/life.ml +++ b/automata/automata/life.ml @@ -17,8 +17,12 @@ let name = "Conway's Game of Life" type t = Dead | Alive +type 'a neighbours = 'a list + let neighbours = [(-1, -1); (-1, 0); (-1, 1); (0, -1); (0, 1); (1, -1); (1, 0); (1, 1)] +let map = List.map + let transition l c = let n = List.length (List.filter (( = ) Alive) l) in if n < 2 || n > 3 then Dead else if n == 3 then Alive else c diff --git a/automata/automata/maze.ml b/automata/automata/maze.ml index 402d982..f7bd101 100644 --- a/automata/automata/maze.ml +++ b/automata/automata/maze.ml @@ -17,8 +17,12 @@ let name = "Labyrinthe B3S12345" type t = Sol | Mur +type 'a neighbours = 'a list + let neighbours = [(-1, -1); (-1, 0); (-1, 1); (0, -1); (0, 1); (1, -1); (1, 0); (1, 1)] +let map = List.map + let transition l c = let n = List.length (List.filter (( = ) Mur) l) in if n < 1 || n > 5 then Sol else if n == 3 then Mur else c diff --git a/automata/automata/wireworld.ml b/automata/automata/wireworld.ml index 14ab5d7..f8e5e01 100644 --- a/automata/automata/wireworld.ml +++ b/automata/automata/wireworld.ml @@ -17,8 +17,12 @@ let name = "Wireworld" type t = Empty | Conductor | Head | Tail +type 'a neighbours = 'a list + let neighbours = [(-1, -1); (-1, 0); (-1, 1); (0, -1); (0, 1); (1, -1); (1, 0); (1, 1)] +let map = List.map + let transition l = function | Empty -> Empty | Conductor -> -- 2.46.0