| 1 | open Core.Std |
| 2 | |
| 3 | |
| 4 | module type MATRIX = sig |
| 5 | type 'a t |
| 6 | |
| 7 | val create : rows:int -> cols:int -> data:'a -> 'a t |
| 8 | |
| 9 | val get : 'a t -> row:int -> col:int -> 'a |
| 10 | |
| 11 | val set : 'a t -> row:int -> col:int -> data:'a -> unit |
| 12 | |
| 13 | val map : 'a t -> f:(row:int -> col:int -> data:'a -> 'b) -> 'b t |
| 14 | |
| 15 | val iter : 'a t -> f:(row:int -> col:int -> data:'a -> unit) -> unit |
| 16 | |
| 17 | val print : 'a t -> to_string:('a -> string) -> unit |
| 18 | end |
| 19 | |
| 20 | module Matrix : MATRIX = struct |
| 21 | type 'a t = 'a array array |
| 22 | |
| 23 | let create ~rows ~cols ~data = |
| 24 | Array.make_matrix ~dimx:rows ~dimy:cols data |
| 25 | |
| 26 | let iter t ~f = |
| 27 | Array.iteri t ~f:( |
| 28 | fun row cols -> |
| 29 | Array.iteri cols ~f:( |
| 30 | fun col data -> |
| 31 | f ~row ~col ~data |
| 32 | ) |
| 33 | ) |
| 34 | |
| 35 | let print t ~to_string = |
| 36 | Array.iter t ~f:( |
| 37 | fun row -> |
| 38 | Array.iter row ~f:(fun x -> printf "%s" (to_string x)); |
| 39 | print_newline () |
| 40 | ) |
| 41 | |
| 42 | let map t ~f = |
| 43 | Array.mapi t ~f:( |
| 44 | fun row cols -> |
| 45 | Array.mapi cols ~f:( |
| 46 | fun col data -> |
| 47 | f ~row ~col ~data |
| 48 | ) |
| 49 | ) |
| 50 | |
| 51 | let get t ~row ~col = |
| 52 | t.(row).(col) |
| 53 | |
| 54 | let set t ~row ~col ~data = |
| 55 | t.(row).(col) <- data |
| 56 | end |
| 57 | |
| 58 | |
| 59 | module type CELL = sig |
| 60 | type t |
| 61 | |
| 62 | val init : unit -> t |
| 63 | |
| 64 | val to_string : t -> string |
| 65 | |
| 66 | val state : t -> int |
| 67 | |
| 68 | val react : t -> states:int list -> t |
| 69 | end |
| 70 | |
| 71 | |
| 72 | module Conway : CELL = struct |
| 73 | type t = D | A |
| 74 | |
| 75 | let of_int = function |
| 76 | | 0 -> D |
| 77 | | 1 -> A |
| 78 | | _ -> assert false |
| 79 | |
| 80 | let to_int = function |
| 81 | | D -> 0 |
| 82 | | A -> 1 |
| 83 | |
| 84 | let to_string = function |
| 85 | | D -> " " |
| 86 | | A -> "o" |
| 87 | |
| 88 | let init () = Random.int 2 |> of_int |
| 89 | |
| 90 | let state = to_int |
| 91 | |
| 92 | let react t ~states = |
| 93 | let live_neighbors = List.fold_left states ~init:0 ~f:(+) in |
| 94 | match t with |
| 95 | | A when live_neighbors < 2 -> D |
| 96 | | A when live_neighbors < 4 -> A |
| 97 | | A when live_neighbors > 3 -> D |
| 98 | | D when live_neighbors = 3 -> A |
| 99 | | t -> t |
| 100 | end |
| 101 | |
| 102 | |
| 103 | let main rows cols () = |
| 104 | Random.self_init (); |
| 105 | let init ~row:_ ~col:_ ~data = Conway.init data in |
| 106 | let grid = Matrix.create ~rows ~cols ~data:() |> Matrix.map ~f:init in |
| 107 | Matrix.print grid ~to_string:Conway.to_string |
| 108 | |
| 109 | |
| 110 | let spec = |
| 111 | let summary = "Polymorphic Cellular Automata" in |
| 112 | let spec = |
| 113 | let open Command.Spec in |
| 114 | empty |
| 115 | +> flag "-rows" (optional_with_default 5 int) ~doc:"Height" |
| 116 | +> flag "-cols" (optional_with_default 5 int) ~doc:"Width" |
| 117 | in |
| 118 | Command.basic ~summary spec main |
| 119 | |
| 120 | |
| 121 | let () = Command.run spec |