gol.ml
· 1.6 KiB · OCaml
Raw
Playground
Random.self_init ()
type cell = Dead | Alive
let cell_to_string = function
| Alive -> "#"
| Dead -> "_"
let cell_to_int = function
| Alive -> 1
| Dead -> 0
let random_cell = fun _ -> if Random.bool () then Dead else Alive ;;
let random_row = fun _ -> Array.init 10 random_cell ;;
let random_grid () = Array.init 10 random_row ;;
let empty_grid () = Array.init 10 (fun _ -> Array.make 10 Dead) ;;
let grid = ref (random_grid ()) ;;
let show_row row =
for x = 0 to 9 do
cell_to_string row.(x) |> print_string
done;
print_newline () ;;
let show_grid () =
for y = 0 to 9 do
show_row !grid.(y)
done
let read_cell x y =
!grid.(y mod 10).(x mod 10) ;;
let neighbors x y =
(read_cell (x + 9) (y + 9) |> cell_to_int)
+ (read_cell x (y + 9) |> cell_to_int)
+ (read_cell (x + 1) (y + 9) |> cell_to_int)
+ (read_cell (x + 9) y |> cell_to_int)
+ (read_cell (x + 1) y |> cell_to_int)
+ (read_cell (x + 9) (y + 1) |> cell_to_int)
+ (read_cell x (y + 1) |> cell_to_int)
+ (read_cell (x + 1) (y + 1) |> cell_to_int)
let next_state x y =
match read_cell x y, neighbors x y with
| (Dead, 3) -> Alive
| (Alive, 2) | (Alive, 3) -> Alive
| _ -> Dead
let update_grid () =
let next_grid = empty_grid () in
for y = 0 to 9 do
for x = 0 to 9 do
next_grid.(y).(x) <- next_state x y
done
done;
grid := next_grid ;;
while true do
show_grid () ;
print_newline () ;
update_grid () ;
let _ = read_line () in () ;
done
| 1 | Random.self_init () |
| 2 | |
| 3 | type cell = Dead | Alive |
| 4 | let cell_to_string = function |
| 5 | | Alive -> "#" |
| 6 | | Dead -> "_" |
| 7 | |
| 8 | let cell_to_int = function |
| 9 | | Alive -> 1 |
| 10 | | Dead -> 0 |
| 11 | |
| 12 | let random_cell = fun _ -> if Random.bool () then Dead else Alive ;; |
| 13 | let random_row = fun _ -> Array.init 10 random_cell ;; |
| 14 | |
| 15 | let random_grid () = Array.init 10 random_row ;; |
| 16 | let empty_grid () = Array.init 10 (fun _ -> Array.make 10 Dead) ;; |
| 17 | |
| 18 | let grid = ref (random_grid ()) ;; |
| 19 | |
| 20 | let show_row row = |
| 21 | for x = 0 to 9 do |
| 22 | cell_to_string row.(x) |> print_string |
| 23 | done; |
| 24 | print_newline () ;; |
| 25 | |
| 26 | let show_grid () = |
| 27 | for y = 0 to 9 do |
| 28 | show_row !grid.(y) |
| 29 | done |
| 30 | |
| 31 | let read_cell x y = |
| 32 | !grid.(y mod 10).(x mod 10) ;; |
| 33 | |
| 34 | let neighbors x y = |
| 35 | (read_cell (x + 9) (y + 9) |> cell_to_int) |
| 36 | + (read_cell x (y + 9) |> cell_to_int) |
| 37 | + (read_cell (x + 1) (y + 9) |> cell_to_int) |
| 38 | + (read_cell (x + 9) y |> cell_to_int) |
| 39 | + (read_cell (x + 1) y |> cell_to_int) |
| 40 | + (read_cell (x + 9) (y + 1) |> cell_to_int) |
| 41 | + (read_cell x (y + 1) |> cell_to_int) |
| 42 | + (read_cell (x + 1) (y + 1) |> cell_to_int) |
| 43 | |
| 44 | let next_state x y = |
| 45 | match read_cell x y, neighbors x y with |
| 46 | | (Dead, 3) -> Alive |
| 47 | | (Alive, 2) | (Alive, 3) -> Alive |
| 48 | | _ -> Dead |
| 49 | |
| 50 | let update_grid () = |
| 51 | let next_grid = empty_grid () in |
| 52 | for y = 0 to 9 do |
| 53 | for x = 0 to 9 do |
| 54 | next_grid.(y).(x) <- next_state x y |
| 55 | done |
| 56 | done; |
| 57 | grid := next_grid ;; |
| 58 | |
| 59 | while true do |
| 60 | show_grid () ; |
| 61 | print_newline () ; |
| 62 | update_grid () ; |
| 63 | let _ = read_line () in () ; |
| 64 | done |
| 65 | |
| 66 |