]> git.f4mnq.fr Git - cells.git/commitdiff
Le voisinage est maintenant un type abstrait
authorAmélia Coutard-Sander <git@f4mnq.fr>
Wed, 7 Jan 2026 13:38:52 +0000 (14:38 +0100)
committerAmélia Coutard-Sander <git@f4mnq.fr>
Wed, 7 Jan 2026 13:53:53 +0000 (14:53 +0100)
automata/automata.ml
automata/automata.mli
automata/automata/brain.ml
automata/automata/cont.ml
automata/automata/life.ml
automata/automata/maze.ml
automata/automata/wireworld.ml

index 27c49ce811648c8a99cc5016e7fe7e2815b0d7f1..470c878b39f3b3965e789f4ca20c5e42a1e1a482 100644 (file)
@@ -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 =
         [
index 892cec1d7d6c7f5bb4de4bcd5830e0ac631df571..fc983570578caa3d7650b84de1f01012f5475346 100644 (file)
@@ -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
 
index 9f9fd2d401ed1807b385072a5cec033c10bc297d..ad5ab1b362548146eb073a3bd37b49da4729b30c 100644 (file)
@@ -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
index 459ae58d2a81b34dd5fb5123818a21a8e73d36d4..4605dd23531c8904c6929774fbb0615edae39396 100644 (file)
@@ -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.
index 7ad5883225de6d83fe4419a0d3403fd95587c243..b2befcbc9fd91f98a06375d5d51a8d267168032e 100644 (file)
@@ -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
index 4e5e0fa5da9663a6d6cb950e10c6fd5bf0068ca9..8fba68fa601c14e8f55b6e95bf3fef4a2c95cf2a 100644 (file)
@@ -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
index f05b7f203f9f06ee6c9fad2b71f831ea118b788f..d757b1eb78d97f7a856658e0d876f03f36c3c464 100644 (file)
@@ -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 ->