home
/
code
/
cellular-automata.git
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (parent:
debaffd
)
Set probability in CLI options.
author
Siraaj Khandkar
<siraaj@khandkar.net>
Mon, 3 Sep 2012 17:32:16 +0000
(13:32 -0400)
committer
Siraaj Khandkar
<siraaj@khandkar.net>
Mon, 3 Sep 2012 17:32:16 +0000
(13:32 -0400)
forest-fire/001/src/forest_fire.ml
patch
|
blob
|
blame
|
history
diff --git
a/forest-fire/001/src/forest_fire.ml
b/forest-fire/001/src/forest_fire.ml
index
98dad01
..
fc91c4a
100644
(file)
--- a/
forest-fire/001/src/forest_fire.ml
+++ b/
forest-fire/001/src/forest_fire.ml
@@
-4,8
+4,8
@@
open Printf
(* ------------------------------------------------------------------------- *
* Constants
* ------------------------------------------------------------------------- *)
(* ------------------------------------------------------------------------- *
* Constants
* ------------------------------------------------------------------------- *)
-let f = 0.01 (* Probability of spontaneous ignition *)
-let p = 1.0 (* Probability of spontaneous growth *)
+let
default_
f = 0.01 (* Probability of spontaneous ignition *)
+let
default_
p = 1.0 (* Probability of spontaneous growth *)
let default_x = 80
let default_y = 25
let default_x = 80
let default_y = 25
@@
-35,6
+35,7
@@
type direction =
type options =
{ size : int * int
type options =
{ size : int * int
+ ; prob : float * float
}
}
@@
-58,17
+59,24
@@
let term_reset () =
let get_opts argv =
let usage = ""
let get_opts argv =
let usage = ""
+ and f = ref default_f
+ and p = ref default_p
and x = ref default_x
and y = ref default_y in
and x = ref default_x
and y = ref default_y in
- let speclist = Arg.align [
- ("-x", Arg.Set_int x, " X.");
- ("-y", Arg.Set_int y, " Y.");
- ] in
+ let speclist =
+ Arg.align
+ [ ("-f", Arg.Set_float f, " Probability of spontaneous ignition.")
+ ; ("-p", Arg.Set_float p, " Probability of spontaneous growth.")
+ ; ("-x", Arg.Set_int x, " Forest width.")
+ ; ("-y", Arg.Set_int y, " Forest height.")
+ ]
+ in
Arg.parse speclist (fun _ -> ()) usage;
{ size = !x, !y
Arg.parse speclist (fun _ -> ()) usage;
{ size = !x, !y
+ ; prob = !f, !p
}
}
@@
-100,13
+108,13
@@
let is_probable = function
| _ -> false
| _ -> false
-let init_cell_state = function
+let init_cell_state
(_, p)
= function
| () when is_probable p -> Tree
| () -> Empty
| () when is_probable p -> Tree
| () -> Empty
-let init_forest (x, y) =
- Array.map (Array.map (init_cell_state)) (Array.make_matrix y x ())
+let init_forest (x, y)
prob
=
+ Array.map (Array.map (init_cell_state
prob
)) (Array.make_matrix y x ())
let string_of_state = function
let string_of_state = function
@@
-116,11
+124,11
@@
let string_of_state = function
let new_state = function
let new_state = function
- | Burning, _
-> Empty
- | Tree, 0
when is_probable f
-> Burning
- | Tree, n
eighbors_burning when neighbors
_burning > 0 -> Burning
- | Empty, _
when is_probable p
-> Tree
- | state, _
-> state
+ | Burning, _
, _
-> Empty
+ | Tree, 0
, (f, _) when is_probable f
-> Burning
+ | Tree, n
_burning, _ when n
_burning > 0 -> Burning
+ | Empty, _
, (_, p) when is_probable p
-> Tree
+ | state, _
, _
-> state
let print_forest forest =
let print_forest forest =
@@
-142,7
+150,7
@@
let is_onside width height (x, y) =
x >= 0 && y >= 0 && x < width && y < height
x >= 0 && y >= 0 && x < width && y < height
-let next_generation forest (width, height) =
+let next_generation forest (width, height)
prob
=
Array.mapi
(
fun iy row ->
Array.mapi
(
fun iy row ->
@@
-153,28
+161,28
@@
let next_generation forest (width, height) =
let neighbors = List.filter (is_onside width height) neighbors in
let neighbor_states = List.map (fun (x, y) -> forest.(y).(x)) neighbors in
let burning_states = List.filter (fun s -> s == Burning) neighbor_states in
let neighbors = List.filter (is_onside width height) neighbors in
let neighbor_states = List.map (fun (x, y) -> forest.(y).(x)) neighbors in
let burning_states = List.filter (fun s -> s == Burning) neighbor_states in
- new_state (state, (List.length burning_states))
+ new_state (state, (List.length burning_states)
, prob
)
)
row
)
forest
)
row
)
forest
-let rec burn forest size =
+let rec burn forest size
prob
=
term_reset ();
print_forest forest;
minisleep 0.1;
term_reset ();
print_forest forest;
minisleep 0.1;
- burn (next_generation forest size
) size
+ burn (next_generation forest size
prob) size prob
let main argv =
Random.self_init ();
let opts = get_opts argv in
let main argv =
Random.self_init ();
let opts = get_opts argv in
- let forest = init_forest opts.size in
+ let forest = init_forest opts.size
opts.prob
in
term_clear ();
term_clear ();
- burn forest opts.size
+ burn forest opts.size
opts.prob
let () = main Sys.argv
let () = main Sys.argv
This page took
0.037568 seconds
and
4
git commands to generate.