-
Notifications
You must be signed in to change notification settings - Fork 11
/
game_of_life_par.ml
64 lines (56 loc) · 1.59 KB
/
game_of_life_par.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
let _num_domains = try int_of_string Sys.argv.(1) with _ -> 1
let n_times = try int_of_string Sys.argv.(2) with _ -> 50
let board_size = try int_of_string Sys.argv.(3) with _ -> 1024
let rg =
ref (Array.init board_size (fun _ -> Array.init board_size (fun _ -> Random.int 2)))
let rg' =
ref (Array.init board_size (fun _ -> Array.init board_size (fun _ -> Random.int 2)))
let buf = Bytes.create board_size
let get g x y =
try g.(x).(y)
with _ -> 0
let neighbourhood g x y =
(get g (x-1) (y-1)) +
(get g (x-1) (y )) +
(get g (x-1) (y+1)) +
(get g (x ) (y-1)) +
(get g (x ) (y+1)) +
(get g (x+1) (y-1)) +
(get g (x+1) (y )) +
(get g (x+1) (y+1))
let next_cell g x y =
let n = neighbourhood g x y in
match g.(x).(y), n with
| 1, 0 | 1, 1 -> 0 (* lonely *)
| 1, 4 | 1, 5 | 1, 6 | 1, 7 | 1, 8 -> 0 (* overcrowded *)
| 1, 2 | 1, 3 -> 1 (* lives *)
| 0, 3 -> 1 (* get birth *)
| _ (* 0, (0|1|2|4|5|6|7|8) *) -> 0 (* barren *)
let print g =
for x = 0 to board_size - 1 do
for y = 0 to board_size - 1 do
if g.(x).(y) = 0
then Bytes.set buf y '.'
else Bytes.set buf y 'o'
done;
print_endline (Bytes.unsafe_to_string buf)
done;
print_endline ""
let next () =
let g = !rg in
let new_g = !rg' in
for x = 0 to board_size - 1 do
for y = 0 to board_size - 1 do
new_g.(x).(y) <- next_cell g x y
done
done;
rg := new_g;
rg' := g
let rec repeat n =
match n with
| 0 -> ()
| _ -> next (); repeat (n-1)
let ()=
print !rg;
repeat n_times;
print !rg