2 N | NE | E | SE | S | SW | W | NW
6 [N; NE; E; SE; S; SW; W; NW]
10 (* direction -> x, y *)
21 (* Hack to sleep less than 1 sec *)
22 let minisleep subsec =
23 ignore (Unix.select [] [] [] subsec)
27 Array.map (Array.map (fun _ -> Random.int 2)) (Array.make_matrix x y 0)
30 let print_board board =
31 Array.iter (fun row -> Array.iter (print_int) row; print_newline ()) board
34 let new_state = function
35 | 1, live_neighbors when live_neighbors < 2 -> 0
36 | 1, live_neighbors when live_neighbors < 4 -> 1
37 | 1, live_neighbors when live_neighbors > 3 -> 0
38 | 0, live_neighbors when live_neighbors = 3 -> 1
42 let filter_offsides width height neighbors =
44 (fun (x, y) -> x >= 0 && y >= 0 && x < width && y < height)
48 let new_generation board =
49 let height = Array.length board
50 and width = Array.length board.(0) in
59 let off_x, off_y = offset d in
60 (i_x + off_x), (i_y + off_y)
64 let neighbors = filter_offsides width height neighbors in
65 let states = List.map (fun (x, y) -> board.(y).(x)) neighbors in
66 let live_neighbors = List.fold_left (+) 0 states in
67 let state = new_state (state, live_neighbors) in
75 let rec life_loop board =
79 life_loop (new_generation board)
83 Random.init (int_of_float (Unix.time ()));
85 life_loop (init_board x y)