-
Notifications
You must be signed in to change notification settings - Fork 0
/
player-bograts.sml
55 lines (47 loc) · 1.68 KB
/
player-bograts.sml
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
structure BogRats :> LAYER =
struct
open LTG;
structure GS = GameState
fun init gs = ()
(* Returns a sequence of turns that loads the value of integer n
into slot j *)
fun loadn n j =
let
fun load 0 acc = (RightApply (j, Zero)) :: acc
| load 1 acc = (LeftApply (Succ, j)) :: load 0 acc
| load n acc = if n mod 2 = 0 then
(LeftApply (Dbl, j)) :: (load (n div 2) acc)
else (LeftApply (Succ, j)) :: (load (n - 1) acc)
in
rev (load n [LeftApply (Put, j)])
end
val target = ref 0
val targethealth = ref 10000
val instructions = ref [
LTG.RightApply (1, LTG.Zero)
]
fun taketurn gs =
let
val ins = case (!instructions)
of nil =>
(targethealth := (!targethealth) - 1;
if (!targethealth) = 0
then (target := (!target) + 1;
targethealth := 10000;
instructions := (loadn (!target) 0) @ [RightApply(1,Zero),
LeftApply (Get,1)];
LTG.LeftApply (LTG.Dec, 1))
else (instructions :=
(if(!target) = 0
then [RightApply (1, Zero)]
else [ RightApply (1, Zero) ,LeftApply (Get,1)]);
LTG.LeftApply (LTG.Dec, 1))
)
| ins::inses =>
(instructions := inses;
ins)
in
ins
end
end
structure Player = LayerFn(BogRats)