aboutsummaryrefslogtreecommitdiffstats
path: root/game.ml
diff options
context:
space:
mode:
Diffstat (limited to 'game.ml')
-rw-r--r--game.ml167
1 files changed, 99 insertions, 68 deletions
diff --git a/game.ml b/game.ml
index 9aecc4f..c327890 100644
--- a/game.ml
+++ b/game.ml
@@ -1,13 +1,17 @@
+exception Rule_error of string
+
type lambda =
|Const of int
|Lambda of (lambda -> lambda);;
let unfold (Lambda x) = x
-let (@) a1 a2 = unfold a1 a2;;
-
let id = Lambda(fun x -> x)
+let (@) a1 a2 =
+ try unfold a1 a2 with Rule_error "wrong application"
+ -> id;;
+
type slot = {
mutable vitality: int;
mutable field: lambda
@@ -20,99 +24,125 @@ let proponent = ref player0
let opponent = ref player1
let zero = Const 0
-let succ = Lambda(fun (Const n) -> if n < 65535 then Const (n+1) else Const n)
-let dbl = Lambda(fun (Const n) -> if n<32768 then Const(2*n) else Const n)
-let get = Lambda(fun (Const i) -> if !proponent.(i).vitality > 0 then !proponent.(i).field else failwith "dead")
+let succ = Lambda(fun a -> match a with
+ |Const n -> if n < 65535 then Const (n+1) else Const n
+ |_ -> raise( Rule_error "not an integer"))
+let dbl = Lambda(fun a -> match a with
+ |Const n -> if n<32768 then Const(2*n) else Const n
+ |_ -> raise( Rule_error "not an integer"))
+
+let get = Lambda(fun a -> match a with
+ |(Const i) -> if !proponent.(i).vitality > 0 then !proponent.(i).field else raise( Rule_error "not a valid slot number")
+ |_ -> raise( Rule_error "not an integer" ))
+
let put = Lambda(fun x -> id)
let s = Lambda(fun f -> Lambda(fun g -> Lambda(fun x -> (f @ x) @ (g @ x))))
let k = Lambda(fun x -> Lambda(fun y -> x))
-let inc = Lambda(fun (Const i) ->
- let v = !proponent.(i) .vitality in
- if ( v < 65535 ) & ( v > 0 ) then
- !proponent.(i).vitality <- v + 1;
- id)
+let inc = Lambda(fun a -> match a with
+ |Const i -> let v = !proponent.(i) .vitality in
+ if ( v < 65535 ) & ( v > 0 ) then
+ !proponent.(i).vitality <- v + 1;
+ id
+ |_ -> raise( Rule_error "not an integer"))
+
+let inc_bis = Lambda(fun a -> match a with
+ |Const i -> let v = !proponent.(i) .vitality in
+ if ( v < 65535 ) & ( v > 0 ) then
+ !proponent.(i).vitality <- v - 1;
+ id
+ |_ -> raise( Rule_error "not an integer"))
-let inc_bis = Lambda(fun (Const i) ->
- let v = !proponent.(i) .vitality in
- if ( v < 65535 ) & ( v > 0 ) then
- !proponent.(i).vitality <- v - 1;
- id)
-let dec = Lambda(fun (Const i) ->
- let v = !opponent.(255-i).vitality in
- if v > 0 then
- !opponent.(255-1).vitality <- v - 1;
- id)
+let dec = Lambda(fun a -> match a with
+ | Const i -> let v = !opponent.(255-i).vitality in
+ if v > 0 then
+ !opponent.(255-1).vitality <- v - 1;
+ id
+ | _ -> raise( Rule_error "not an integer"))
-let dec_bis = Lambda(fun (Const i) ->
- let v = !opponent.(255-i).vitality in
- if v > 0 then
- !opponent.(255-1).vitality <- v + 1;
- id)
+let dec_bis = Lambda(fun a -> match a with
+ | (Const i) -> let v = !opponent.(255-i).vitality in
+ if v > 0 then
+ !opponent.(255-1).vitality <- v + 1;
+ id
+ | _ -> raise( Rule_error "not an integer"))
-let attack = Lambda(fun (Const i) -> Lambda(fun (Const j) -> Lambda(fun (Const n) ->
+let attack = Lambda(fun a -> Lambda(fun b -> Lambda(fun c -> match a, b, c with
+ | Const i, Const j, Const n ->
let v = !proponent.(i).vitality in
if v - n > 0 then
!proponent.(i).vitality <- v - n
else
- failwith "not enough life";
+ raise( Rule_error "not enough life left");
let w = !opponent.(255-j).vitality in
if w > 0 then
!opponent.(255-j).vitality <- max 0 (w - 9/10 *n);
- id)))
+ id
+ |_ -> raise( Rule_error "not an integer"))))
-let attack_bis = Lambda(fun (Const i) -> Lambda(fun (Const j) -> Lambda(fun (Const n) ->
+let attack_bis = Lambda(fun a -> Lambda(fun b -> Lambda(fun c -> match a, b, c with
+ | Const i, Const j, Const n ->
let v = !proponent.(i).vitality in
if v - n > 0 then
!proponent.(i).vitality <- v - n
else
- failwith "not enough life";
+ raise( Rule_error "not enought life left");
let w = !opponent.(255-j).vitality in
if w > 0 then
!opponent.(255-j).vitality <- min 65535 (w + 9/10 *n);
- id)))
+ id
+ | _ -> raise( Rule_error "not an integer"))))
-let help = Lambda(fun (Const i) -> Lambda(fun (Const j) -> Lambda(fun (Const n) ->
- let v = !proponent.(i).vitality in
- if v - n > 0 then
- !proponent.(i).vitality <- v - n
- else
- failwith "not enough life";
- let w = !proponent.(j).vitality in
- if w > 0 then
- !proponent.(j).vitality <- max (w - n*11/10) 0;
- id)))
+let help = Lambda(fun a -> Lambda(fun b -> Lambda(fun c -> match a, b, c with
+ | Const i, Const j, Const n ->
+ let v = !proponent.(i).vitality in
+ if v - n > 0 then
+ !proponent.(i).vitality <- v - n
+ else
+ raise( Rule_error "not enough life left");
+ let w = !proponent.(j).vitality in
+ if w > 0 then
+ !proponent.(j).vitality <- max (w - n*11/10) 0;
+ id
+ | _ -> raise( Rule_error "not an integer"))))
-let help_bis = Lambda(fun (Const i) -> Lambda(fun (Const j) -> Lambda(fun (Const n) ->
- let v = !proponent.(i).vitality in
- if v - n > 0 then
- !proponent.(i).vitality <- v - 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 help_bis = Lambda(fun a -> Lambda(fun b -> Lambda(fun c -> match a, b, c with
+ | Const i, Const j, Const n ->
+ let v = !proponent.(i).vitality in
+ if v - n > 0 then
+ !proponent.(i).vitality <- v - n
+ else
+ raise( Rule_error "not enough life left");
+ let w = !proponent.(j).vitality in
+ if w > 0 then
+ !proponent.(j).vitality <- min (w + n*11/10) 65535;
+ id
+ | _ -> raise( Rule_error "not an integer"))))
-let copy = Lambda(fun (Const i) -> !opponent.(i).field)
+let copy = Lambda(fun a -> match a with
+ | Const i -> !opponent.(i).field
+ | _ -> raise( Rule_error "not an integer"))
-let revive = Lambda(fun (Const i) ->
- let v = !proponent.(i).vitality in
- if v <= 0 then
- !proponent.(i).vitality <- 1;
- id)
+let revive = Lambda(fun a -> match a with
+ | Const i -> let v = !proponent.(i).vitality in
+ if v <= 0 then
+ !proponent.(i).vitality <- 1;
+ id
+ | _ -> raise( Rule_error "not an integer"))
-let zombie = Lambda(fun (Const i) -> Lambda(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))
+let zombie = Lambda(fun a -> match a with
+ | Const i -> Lambda(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
+ raise( Rule_error "slot is alive");
+ id)
+ |_ -> raise( Rule_error "not an integer"))
type card =
| I
@@ -148,6 +178,7 @@ let card_of_string s = match s with
| "copy" -> Copy
| "revive" -> Revive
| "zombie" -> Zombie
+ | _ -> raise( Rule_error "not a valid card name")
;;
let cardfun_of_card c = match c with
@@ -223,7 +254,7 @@ let right_apply_bis player card number =
end;
let slot = !proponent.(number) in
slot.field <- slot.field @ (cardfun_of_card_bis card)
-
+
(*
right_apply !proponent.(0) Help;;
right_apply !proponent.(0) Zero;;