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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
|
exception Rule_error of string
type lambda =
|Const of int
|Lambda of (lambda -> lambda);;
let unfold x = match x with
| Lambda y -> y
| _ -> raise( Rule_error "wrong unfolding")
let id = Lambda(fun x -> x)
let (@) a1 a2 = unfold a1 a2
type slot = {
mutable vitality: int;
mutable field: lambda
}
;;
let player0 = Array.init 256 (fun _ -> {vitality = 10000; field = id})
let player1 = Array.init 256 (fun _ -> {vitality = 10000; field = id})
let card_count = ref(0)
let proponent = ref player0
let opponent = ref player1
let increase card_count =
if !card_count>=1000 then
raise( Rule_error "apply limit exceeded")
else
card_count:=!card_count+1
let zero = increase card_count;Const 0
let succ = increase card_count;
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 = increase card_count;
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 = increase card_count;
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 = increase card_count;Lambda(fun x -> id)
let s = increase card_count;Lambda(fun f -> Lambda(fun g -> Lambda(fun x -> (f @ x) @ (g @ x))))
let k = increase card_count;Lambda(fun x -> Lambda(fun y -> x))
let inc = increase card_count;
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 = increase card_count;
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 dec = increase card_count;
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 = increase card_count;
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 = increase card_count;
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 = !opponent.(255-j).vitality in
if w > 0 then
!opponent.(255-j).vitality <- max 0 (w - 9/10 *n);
id
| _ -> raise( Rule_error "not an integer"))))
let attack_bis = increase card_count;
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 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
| _ -> raise( Rule_error "not an integer"))))
let help = increase card_count;
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 = increase card_count;
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 = increase card_count;
Lambda(fun a -> match a with
| Const i -> !opponent.(i).field
| _ -> raise( Rule_error "not an integer"))
let revive = increase card_count;
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 = increase card_count;
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
| Zero
| Succ
| Dbl
| Get
| Put
| S
| K
| Inc
| Dec
| Attack
| Help
| Copy
| Revive
| Zombie
;;
let card_of_string s = match s with
| "I" -> I
| "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
| _ -> raise( Rule_error "not a valid card name")
;;
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 cardfun_of_card_bis c = match c with
| I -> id
| Zero -> zero
| Succ -> succ
| Dbl -> dbl
| Get -> get
| Put -> put
| S -> s
| K -> k
| Inc -> inc_bis
| Dec -> dec_bis
| Attack -> attack_bis
| Help -> help_bis
| Copy -> copy
| Revive -> revive
| Zombie -> zombie
let left_apply player card number =
if player = 0 then begin
proponent := player0;
opponent := player1
end
else
begin
proponent := player1;
opponent := player0
end;
let slot = !proponent.(number) in
slot.field <-
try
(cardfun_of_card card) @ slot.field
with
| Rule_error _ | Invalid_argument _ -> card_count:=0; id
let right_apply player card number =
if player = 0 then begin
proponent := player0;
opponent := player1
end
else
begin
proponent := player1;
opponent := player0
end;
let slot = !proponent.(number) in
slot.field <-
try
slot.field @ (cardfun_of_card card)
with
| Rule_error _ | Invalid_argument _ -> card_count:=0; id
let right_apply_bis player card number =
if player = 0 then begin
proponent := player0;
opponent := player1
end
else
begin
proponent := player1;
opponent := player0
end;
let slot = !proponent.(number) in
slot.field <-
try
slot.field @ (cardfun_of_card_bis card)
with
| Rule_error _ | Invalid_argument _ -> card_count:=0; 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;;
*)
|