Abstract PhenoType.
[cellular-automata.git] / polymorphism / 001 / src / polymorphism.ml
index af46bc5..f94d5ad 100644 (file)
@@ -1,6 +1,39 @@
 open Core.Std
 
 
+module Terminal : sig
+  type color = [ `green
+               | `red
+               ]
+
+  val string_with_color : string -> color -> string
+
+  val clear : unit -> unit
+
+  val reset : unit -> unit
+end = struct
+  type color = [ `green
+               | `red
+               ]
+
+  let ansi_code_clear = "\027[2J"    (* Clear screen *)
+  let ansi_code_reset = "\027[1;1H"  (* Reset cursor position *)
+
+  let string_of_color = function
+    | `green -> "\027[0;32m"
+    | `red   -> "\027[1;31m"
+
+  let string_with_color s c =
+    sprintf "%s%s\027[0m" (string_of_color c) s
+
+  let clear () =
+    print_string ansi_code_clear
+
+  let reset () =
+    print_string ansi_code_reset
+end
+
+
 module type MATRIX = sig
   module Point : sig
     type t = {r : int; k : int}
@@ -117,8 +150,25 @@ module State = struct
 end
 
 
-module PhenoType = struct
-  type t = string
+module PhenoType : sig
+  type t
+
+  val create : char -> Terminal.color option -> t
+
+  val to_string : t -> string
+end = struct
+  type t = { color     : Terminal.color option
+           ; character : char
+           }
+
+  let create character color =
+    {color; character}
+
+  let to_string = function
+    | {color=None; character} ->
+      String.of_char character
+    | {color=Some c; character} ->
+      Terminal.string_with_color (String.of_char character) c
 end
 
 
@@ -162,8 +212,8 @@ module Conway : RULE = struct
     string_of_state
 
   let pheno_of_state : (state -> PhenoType.t) = function
-    | D -> " "
-    | A -> "o"
+    | D -> PhenoType.create ' ' None
+    | A -> PhenoType.create 'o' None
 
   let int_of_msg msg =
     msg |> state_of_string |> int_of_state
@@ -197,21 +247,6 @@ module Conway : RULE = struct
 end
 
 
-module Terminal : sig
-  val clear : unit -> unit
-  val reset : unit -> unit
-end = struct
-  let ansi_code_clear = "\027[2J"    (* Clear screen *)
-  let ansi_code_reset = "\027[1;1H"  (* Reset cursor position *)
-
-  let clear () =
-    print_string ansi_code_clear
-
-  let reset () =
-    print_string ansi_code_reset
-end
-
-
 module Automaton : sig
   type t
 
@@ -249,7 +284,7 @@ end = struct
     }
 
   let cell_to_string cell =
-    cell.data.Cell.pheno
+    PhenoType.to_string cell.data.Cell.pheno
 
   let print t =
     Terminal.reset ();
This page took 0.027315 seconds and 4 git commands to generate.