2 N | NE | E | SE | S | SW | W | NW
6 [N; NE; E; SE; S; SW; W; NW]
13 let char_of_state = function
20 (* direction -> x, y *)
31 (* Hack to sleep less than 1 sec *)
32 let minisleep subsec =
33 ignore (Unix.select [] [] [] subsec)
37 Array.map (Array.map (fun _ -> Random.int 2)) (Array.make_matrix y x 0)
40 let print_board board =
44 (fun state -> print_char (char_of_state state))
51 let new_state = function
52 | 1, live_neighbors when live_neighbors < 2 -> 0
53 | 1, live_neighbors when live_neighbors < 4 -> 1
54 | 1, live_neighbors when live_neighbors > 3 -> 0
55 | 0, live_neighbors when live_neighbors = 3 -> 1
59 let filter_offsides width height neighbors =
61 (fun (x, y) -> x >= 0 && y >= 0 && x < width && y < height)
65 let new_generation board =
66 let height = Array.length board
67 and width = Array.length board.(0) in
76 let off_x, off_y = offset d in
77 (i_x + off_x), (i_y + off_y)
81 let neighbors = filter_offsides width height neighbors in
82 let states = List.map (fun (x, y) -> board.(y).(x)) neighbors in
83 let live_neighbors = List.fold_left (+) 0 states in
84 let state = new_state (state, live_neighbors) in
92 let rec life_loop board =
96 life_loop (new_generation board)
100 let x = int_of_string argv.(1)
101 and y = int_of_string argv.(2)
104 Random.init (int_of_float (Unix.time ()));
106 life_loop (init_board x y)
109 let () = main Sys.argv