aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGuillaume Horel <guillaume.horel@gmail.com>2011-06-18 16:04:13 -0400
committerGuillaume Horel <guillaume.horel@gmail.com>2011-06-18 16:04:13 -0400
commit26361f58aed4800794bd44e4131b557a7748fdd0 (patch)
tree98619003fb7683f280f458fa9f615a6395e20729
parenta45a24f022c77e3a67ef2abb11da1808cce912bd (diff)
downloadicfp2011-26361f58aed4800794bd44e4131b557a7748fdd0.tar.gz
Complete implementation of the cards
seems to be buggy, hopefully it's an easy fix
-rw-r--r--cards.ml182
1 files changed, 116 insertions, 66 deletions
diff --git a/cards.ml b/cards.ml
index 63b7c41..a1d07ed 100644
--- a/cards.ml
+++ b/cards.ml
@@ -1,3 +1,80 @@
+type lambda =
+ |Const of int
+ |Lambda of (lambda -> lambda);;
+let unfold (Lambda x) = x
+let (@) a1 a2 = unfold a1 a2;;
+let out (Lambda x) = x
+let (!) x = Lambda x
+
+let id = !(fun x -> x)
+type slot = {
+ mutable vitality: int;
+ mutable field: lambda
+}
+;;
+let proponent = Array.init 256 (fun _ -> {vitality = 10000; field = id})
+let opponent = Array.init 256 (fun _ -> {vitality = 10000; field = id})
+
+let zero = Const 0
+let succ = !(fun (Const n) -> if n < 65535 then Const (n+1) else Const n)
+let dbl = !(fun (Const n) -> if n<32768 then Const(2*n) else Const n)
+let get = !(fun (Const i) -> if proponent.(i).vitality > 0 then proponent.(i).field else failwith "dead")
+let put = !(fun x -> id)
+let s = !(fun f -> !(fun g -> !(fun x -> (f @ x) @ (g @ x))))
+let k = !(fun x -> !(fun y -> x))
+let inc = !(fun (Const i) ->
+ let v = proponent.(i).vitality in
+ if ( v < 65535 ) & ( v > 0 ) then
+ proponent.(i).vitality <- proponent.(i).vitality + 1;
+ id)
+
+let dec = !(fun (Const i) ->
+ let v = opponent.(255-i).vitality in
+ if v>0 then
+ opponent.(255-1).vitality <-opponent.(255-1).vitality-1;
+ id)
+
+let attack = !(fun (Const i) -> !(fun (Const j) -> !(fun (Const n) ->
+ let v = proponent.(i).vitality in
+ if v - n > 0 then
+ proponent.(i).vitality <- proponent.(i).vitality - n
+ else
+ failwith "not enough life";
+ let w = opponent.(j).vitality in
+ if w > 0 then
+ proponent.(j).vitality <- max 0 (w - 9/10 *n);
+ id)))
+
+let help = !(fun (Const i) -> !(fun (Const j) -> !(fun (Const n) ->
+ let v = proponent.(i).vitality in
+ if v - n > 0 then
+ proponent.(i).vitality <- proponent.(i).vitality - n
+ else
+ failwith "not enough life";
+ let w = proponent.(255-j).vitality in
+ if w > 0 then
+ proponent.(255 - j).vitality <- min (w + n*11/10) 65535;
+ id)))
+
+let copy = !(fun (Const i) -> opponent.(i).field)
+
+let revive = !(fun (Const i) ->
+ let v = proponent.(i).vitality in
+ if v <= 0 then
+ proponent.(i).vitality <- 1;
+ id)
+
+let zombie = !(fun (Const i) -> !(fun x ->
+ let v = opponent.(255-i).vitality in
+ if v <= 0 then
+ begin
+ opponent.(255-i).field <- x;
+ opponent.(255-i).vitality <- -1
+ end
+ else
+ failwith "slot is alive!";
+ id))
+
type card =
| I
| Zero
@@ -33,74 +110,47 @@ let card_of_string s = match s with
| "revive" -> Revive
| "zombie" -> Zombie
;;
-
-type slot = {
- mutable vitality: int;
- mutable field: lambda
-}
-;;
-
-let left_apply card slot = match card with
- | S -> slot.field <- s @ slot.field
-
-let proponent = Array.init 256 (fun _ -> {vitality = 10000; field = id})
-let opponent = Array.init 256 (fun _ -> {vitality = 10000; field = id})
-
-type 'a lambda = Lambda of ('a -> 'a)
-let unfold (Lambda x) = x
-let (@) a1 a2 = unfold a1 a2;;
-let s = Lambda(fun f -> Lambda(fun g -> Lambda(fun Lambda(x) -> (f @ x) @ (g @ x))))
-type lambda =
- |Const of int
- |Lambda of (lambda -> lambda);;
-let out (Lambda x) = x
-let (!) x = Lambda x
-let (@) a1 a2 = out a1 a2
-let id = !(fun x -> x)
-let zero = Const 0
-let succ (Const n) = if n < 65535 then Const (n+1) else Const n
-let dbl (Const n) = if n<32768 then Const(2*n) else Const n
-let get (Const i) = if player1.(i).vitality > 0 then player1.(i).field else failwith "dead"
-let put = !(fun x -> id)
-let s = !(fun f -> !(fun g -> !(fun x -> (f @ x) @ (g @ x))))
-let k = !(fun x -> Lambda(fun y -> y))
-let inc (Const i) = let v = proponent.(i).vitality in
- if ( v < 65535 ) & ( v > 0 ) then
- proponent.(i).vitality <- proponent.(i).vitality + 1;
- id
-let dec (Const i) = let v = opponent.(255-i).vitality in
- if v>0 then
- opponent.(255-1).vitality <-opponent.(255-1).vitality-1;
- id
+let cardfun_of_card c = match c with
+ | I -> id
+ | Zero -> zero
+ | Succ -> succ
+ | Dbl -> dbl
+ | Get -> get
+ | Put -> put
+ | S -> s
+ | K -> k
+ | Inc -> inc
+ | Dec -> dec
+ | Attack -> attack
+ | Help -> help
+ | Copy -> copy
+ | Revive -> revive
+ | Zombie -> zombie
-let attack (Const i) (Const j) (Const n) =
- let v = proponent.(i).vitality in
- if v - n > 0 then
- proponent.(i).vitality <- proponent.(i).vitality - n
- else
- failwith "not enough life";
- let w = opponent.(j).vitality in
- if w>0 then
- proponent.(j).vitality <- max(0, ,
-let help (Const i) (Const j) (Const n) =
- let v = proponent.(i).vitality in
- if v - n > 0 then
- proponent.(i).vitality <- proponent.(i).vitality - n
- else
- failwith "not enough life";
- let w = proponent.(j).vitality in
- if w>0 then
- proponent.(j).vitality <- min (w + n*11/10) 65535;
- id
+let left_apply card slot =
+ slot.field <- (cardfun_of_card card) @ slot.field
-let copy (Const i) =
- opponent.(i).field
+let right_apply slot card =
+ slot.field <- slot.field @ (cardfun_of_card card)
-let revive (Const i) =
- let v = proponent.(i).vitality in
- if v <= 0 then
- proponent.(i).vitality <- 1;
- id
+right_apply proponent.(0) Help;;
+right_apply proponent.(0) Zero;;
+left_apply K proponent.(0);;
+left_apply S proponent.(0);;
+right_apply proponent.(0) Succ;;
+right_apply proponent.(0) Zero;;
+right_apply proponent.(1) Zero;;
+left_apply Succ proponent.(1);;
+left_apply Dbl proponent.(1);;
+left_apply Dbl proponent.(1);;
+left_apply Dbl proponent.(1);;
+left_apply Dbl proponent.(1);;
+left_apply K proponent.(0);;
+left_apply S proponent.(0);;
+right_apply proponent.(0) Get;;
+left_apply K proponent.(0);;
+left_apply S proponent.(0);;
+right_apply proponent.(0) Succ;;
+right_apply proponent.(0) Zero;;
-let iva = "je travaille sur matlab!"