From: Amélia Coutard-Sander <git@ameliathe1st.gay>
Date: Sat, 4 Jan 2025 02:42:19 +0000 (+0100)
Subject: Ajout d'une sérialisation/dé-sérialisation des plateaux de cellules
X-Git-Url: https://git.ameliathe1st.gay/?a=commitdiff_plain;h=ce2033423a38d00a098f0b9a1ec875e0d43eded6;p=cells.git

Ajout d'une sérialisation/dé-sérialisation des plateaux de cellules
---

diff --git a/automata/automata.ml b/automata/automata.ml
index 9592444..eaa8467 100644
--- a/automata/automata.ml
+++ b/automata/automata.ml
@@ -37,6 +37,10 @@ module type Automaton = sig
   val next : t -> t
 
   val color : t -> char * char * char
+
+  val of_string : string -> t
+
+  val to_string : t -> string
 end
 
 module Coord = struct
@@ -121,6 +125,36 @@ let update (type t) (m : (module Automaton with type t = t)) (d, board) =
             CoordMap.empty ccoords
           |> CoordMap.filter (fun _ -> Array.exists (Array.exists (fun c -> c <> d))) )
 
+let deserialise (type t) (m : (module Automaton with type t = t)) channel =
+        let module M = (val m) in
+        let d = Option.get (In_channel.input_line channel) |> M.of_string in
+        let board =
+                let rec aux board =
+                        match In_channel.input_line channel with
+                        | None -> board
+                        | Some x ->
+                                let x = int_of_string x
+                                and y = In_channel.input_line channel |> Option.get |> int_of_string in
+                                let chunk =
+                                        Array.init chunk_size (fun _ ->
+                                            Array.init chunk_size (fun _ ->
+                                                In_channel.input_line channel |> Option.get |> M.of_string))
+                                in
+                                aux (CoordMap.add (x, y) chunk board)
+                in
+                aux CoordMap.empty
+        in
+        (d, board)
+
+let serialise (type t) (m : (module Automaton with type t = t)) (d, board) channel =
+        let module M = (val m) in
+        Printf.fprintf channel "%s\n" (M.to_string d);
+        CoordMap.iter
+          (fun (x, y) chunk ->
+            Printf.fprintf channel "%d\n%d\n" x y;
+            Array.iter (Array.iter (fun c -> Printf.fprintf channel "%s\n" (M.to_string c))) chunk)
+          board
+
 let automata =
         [
           (module Life : Automaton);
diff --git a/automata/automata.mli b/automata/automata.mli
index 69aeab8..3f871eb 100644
--- a/automata/automata.mli
+++ b/automata/automata.mli
@@ -37,6 +37,10 @@ module type Automaton = sig
   val next : t -> t
 
   val color : t -> char * char * char
+
+  val of_string : string -> t
+
+  val to_string : t -> string
 end
 
 type 't board
@@ -50,3 +54,7 @@ val set : int -> int -> 't -> 't board -> 't board
 val update : (module Automaton with type t = 't) -> 't board -> 't board
 
 val automata : (module Automaton) list
+
+val deserialise : (module Automaton with type t = 't) -> in_channel -> 't board
+
+val serialise : (module Automaton with type t = 't) -> 't board -> out_channel -> unit
diff --git a/automata/automata/brain.ml b/automata/automata/brain.ml
index 15c6e20..5de9e9d 100644
--- a/automata/automata/brain.ml
+++ b/automata/automata/brain.ml
@@ -53,3 +53,14 @@ let color = function
         | Off -> ('\x00', '\x00', '\x1F')
         | On -> ('\xFF', '\xFF', '\xFF')
         | Dying -> ('\x00', '\x00', '\xFF')
+
+let of_string = function
+        | "D" -> Off
+        | "A" -> On
+        | "Y" -> Dying
+        | _ -> failwith "Unparseable"
+
+let to_string = function
+        | Off -> "D"
+        | On -> "A"
+        | Dying -> "Y"
diff --git a/automata/automata/cont.ml b/automata/automata/cont.ml
index 12ec040..0d0ec9e 100644
--- a/automata/automata/cont.ml
+++ b/automata/automata/cont.ml
@@ -42,3 +42,7 @@ let color c =
                 |> char_of_int
         in
         if c > 0. then (v, '\x00', '\x00') else ('\x00', '\x00', v)
+
+let of_string = float_of_string
+
+let to_string = string_of_float
diff --git a/automata/automata/life.ml b/automata/automata/life.ml
index 6876cae..16f9c1b 100644
--- a/automata/automata/life.ml
+++ b/automata/automata/life.ml
@@ -46,3 +46,12 @@ let next = function
 let color = function
         | Dead -> ('\x00', '\x00', '\x1F')
         | Alive -> ('\xFF', '\xFF', '\xFF')
+
+let of_string = function
+        | "D" -> Dead
+        | "A" -> Alive
+        | _ -> failwith "Unparseable"
+
+let to_string = function
+        | Dead -> "D"
+        | Alive -> "A"
diff --git a/automata/automata/maze.ml b/automata/automata/maze.ml
index 4e9adda..1181c4c 100644
--- a/automata/automata/maze.ml
+++ b/automata/automata/maze.ml
@@ -46,3 +46,12 @@ let next = function
 let color = function
         | Sol -> ('\x00', '\x00', '\x1F')
         | Mur -> ('\xFF', '\xFF', '\xFF')
+
+let of_string = function
+        | "S" -> Sol
+        | "M" -> Mur
+        | _ -> failwith "Unparseable"
+
+let to_string = function
+        | Sol -> "S"
+        | Mur -> "M"
diff --git a/automata/automata/wireworld.ml b/automata/automata/wireworld.ml
index 895944d..a040a03 100644
--- a/automata/automata/wireworld.ml
+++ b/automata/automata/wireworld.ml
@@ -57,3 +57,16 @@ let color = function
         | Conductor -> ('\xBF', '\xBF', '\x00')
         | Head -> ('\xBF', '\x00', '\x00')
         | Tail -> ('\x00', '\x00', '\xBF')
+
+let of_string = function
+        | "0" -> Empty
+        | "C" -> Conductor
+        | "H" -> Head
+        | "T" -> Tail
+        | _ -> failwith "Unparseable"
+
+let to_string = function
+        | Empty -> "0"
+        | Conductor -> "C"
+        | Head -> "H"
+        | Tail -> "T"
diff --git a/bin/modes.ml b/bin/modes.ml
index d7414f9..80419ea 100644
--- a/bin/modes.ml
+++ b/bin/modes.ml
@@ -134,6 +134,12 @@ functor
               | 'l' -> Either.left (ntimes (chpos 1 0) st)
               | 'i' -> Either.left (ntimes (chsize 1) st)
               | 'o' -> Either.left (ntimes (chsize (-1)) st)
+              | 'w' ->
+                      Out_channel.with_open_text "test.auto" (Automata.serialise (module M) st.board);
+                      Either.left st
+              | 'r' ->
+                      Either.left
+                        { st with board = In_channel.with_open_text "test.auto" (Automata.deserialise (module M)) }
               | _ -> Either.left st
     end