aboutsummaryrefslogtreecommitdiff
path: root/calculette_aoo/lib/carac.ml
diff options
context:
space:
mode:
Diffstat (limited to 'calculette_aoo/lib/carac.ml')
-rw-r--r--calculette_aoo/lib/carac.ml40
1 files changed, 40 insertions, 0 deletions
diff --git a/calculette_aoo/lib/carac.ml b/calculette_aoo/lib/carac.ml
new file mode 100644
index 0000000..a48c734
--- /dev/null
+++ b/calculette_aoo/lib/carac.ml
@@ -0,0 +1,40 @@
+type cout_carac = int * int * int
+
+type t = {
+ value : int
+ ; couts : cout_carac
+ ; bonus : int
+}
+
+let create : ?bonus:int -> int -> cout_carac -> t =
+ fun ?(bonus = 0) value couts -> { value; couts; bonus }
+
+let incr ?(step = 1) t = { t with bonus = t.bonus + step }
+
+(*
+Evaluate the cost for the successives upgrades.
+
+I’m pretty sure this can be transformed into a linear function, but I do not see how…
+
+ c0 * t.bonus
++ max 0 ((((t.bonus - 1) * 2) - 1) * c1)
++ ?
+
+ *)
+let cout : t -> int =
+ fun t ->
+ let c0, c1, c2 = t.couts in
+ let rec c acc t =
+ match t with
+ | 0 -> acc
+ | 1 -> acc + c0
+ | 2 -> c (acc + c0 + c1) (t - 1)
+ | 3 -> c (acc + c0 + (c1 * 2)) (t - 1)
+ | n -> c (acc + c0 + (c1 * 2) + ((n - 3) * c2)) (t - 1)
+ in
+ c 0 t.bonus
+
+let value t = t.value + t.bonus
+
+let repr : Format.formatter -> t -> unit =
+ fun out t -> Format.fprintf out "%d (+%d)" (value t) t.bonus