From 8a9f7ffbfd573d29e76dede90cba8fc6dd55b401 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Am=C3=A9lia=20Coutard-Sander?= Date: Thu, 12 Dec 2024 16:18:42 +0100 Subject: [PATCH] Ajout de deux automates exemples pour l'API Le Jeu de la Vie et Wireworld. --- automata/automata.ml | 2 +- automata/automata/life.ml | 38 +++++++++++++++++++++++++++ automata/automata/wireworld.ml | 48 ++++++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 automata/automata/life.ml create mode 100644 automata/automata/wireworld.ml diff --git a/automata/automata.ml b/automata/automata.ml index 63de763..847918c 100644 --- a/automata/automata.ml +++ b/automata/automata.ml @@ -31,4 +31,4 @@ module type Automaton = sig val color : t -> Graphics.color end -let automata = [] +let automata = [(module Life : Automaton); (module Wireworld : Automaton)] diff --git a/automata/automata/life.ml b/automata/automata/life.ml new file mode 100644 index 0000000..0ee7472 --- /dev/null +++ b/automata/automata/life.ml @@ -0,0 +1,38 @@ +(* Copyright 2024 Amélia COUTARD . + * + * This file from the program cells is free software: you can redistribute it and/or + * modify it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License along + * with this program. If not, see . + *) + +let name = "life" + +type t = Dead | Alive + +let neighbours = [(-1, -1); (-1, 0); (-1, 1); (0, -1); (0, 1); (1, -1); (1, 0); (1, 1)] + +let transition l c = + let n = List.length (List.filter (( = ) Alive) l) in + if n <= 2 || n >= 4 then Dead else if n == 3 then Alive else c + +let default = Dead + +let prev = function + | Dead -> Alive + | Alive -> Dead + +let next = function + | Dead -> Alive + | Alive -> Dead + +let color = function + | Dead -> Graphics.rgb 0 0 31 + | Alive -> Graphics.rgb 255 255 255 diff --git a/automata/automata/wireworld.ml b/automata/automata/wireworld.ml new file mode 100644 index 0000000..023751c --- /dev/null +++ b/automata/automata/wireworld.ml @@ -0,0 +1,48 @@ +(* Copyright 2024 Amélia COUTARD . + * + * This file from the program cells is free software: you can redistribute it and/or + * modify it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License along + * with this program. If not, see . + *) + +let name = "wireworld" + +type t = Empty | Conductor | Head | Tail + +let neighbours = [(-1, -1); (-1, 0); (-1, 1); (0, -1); (0, 1); (1, -1); (1, 0); (1, 1)] + +let transition l = function + | Empty -> Empty + | Conductor -> + let n = List.length (List.filter (( = ) Head) l) in + if n = 1 || n = 2 then Head else Conductor + | Head -> Tail + | Tail -> Conductor + +let default = Empty + +let prev = function + | Empty -> Tail + | Conductor -> Empty + | Head -> Conductor + | Tail -> Head + +let next = function + | Empty -> Conductor + | Conductor -> Head + | Head -> Tail + | Tail -> Empty + +let color = function + | Empty -> Graphics.rgb 0 0 31 + | Conductor -> Graphics.rgb 191 191 0 + | Head -> Graphics.rgb 191 0 0 + | Tail -> Graphics.rgb 0 0 191 -- 2.46.0