Colorize alive Life cells with white.
[cellular-automata.git] / polymorphism / 001 / src / polymorphism.ml
index aae480b..f132fd7 100644 (file)
@@ -4,6 +4,7 @@ open Core.Std
 module Terminal : sig
   type color = [ `green
                | `red
+               | `white
                ]
 
   val string_with_color : string -> color -> string
@@ -14,6 +15,7 @@ module Terminal : sig
 end = struct
   type color = [ `green
                | `red
+               | `white
                ]
 
   let ansi_code_clear = "\027[2J"    (* Clear screen *)
@@ -22,6 +24,7 @@ end = struct
   let string_of_color = function
     | `green -> "\027[0;32m"
     | `red   -> "\027[1;31m"
+    | `white -> "\027[1;37m"
 
   let string_with_color s c =
     sprintf "%s%s\027[0m" (string_of_color c) s
@@ -187,7 +190,7 @@ module type RULE = sig
 end
 
 
-module Conway : RULE = struct
+module Life : RULE = struct
   type state = D | A
 
   let state_of_string : (string -> state) = function
@@ -213,7 +216,7 @@ module Conway : RULE = struct
 
   let pheno_of_state : (state -> PhenoType.t) = function
     | D -> PhenoType.create ' ' None
-    | A -> PhenoType.create 'o' None
+    | A -> PhenoType.create 'o' (Some `white)
 
   let int_of_msg msg =
     msg |> state_of_string |> int_of_state
@@ -290,9 +293,8 @@ module ForestFire : RULE = struct
   let f = 0.000001  (* Probability of spontaneous ignition *)
   let p = 0.1       (* Probability of spontaneous growth *)
 
-  let is_probable = function
-    | probability when (Random.float 1.0) <= probability -> true
-    | _ -> false
+  let is_probable p =
+    (Random.float 1.0) <= p
 
   let next state ~burning_neighbors =
     match state, burning_neighbors with
@@ -388,7 +390,7 @@ let main () =
   let rows, columns = Or_error.ok_exn Linux_ext.get_terminal_size () in
   let interval = 0.1 in
   let rules =
-    [ (module Conway : RULE)
+    [ (module Life : RULE)
     ; (module ForestFire : RULE)
     ]
   in
This page took 0.027926 seconds and 4 git commands to generate.