-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.rsh v0.3
63 lines (55 loc) · 1.41 KB
/
index.rsh v0.3
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
'reach 0.1';
/*
This version has a single player draw off the deck and a flag to
control the while loop while prompting the user for more cards
*/
// this considers aces high
const cardValue = (x) => (x == 1 ? 11 : (x < 10 ? x : 10))
const Shared = {
...hasRandom,
getCard: Fun([], UInt),
startGame: Fun([], Tuple(UInt, UInt)),
};
export const main = Reach.App(() => {
const A = Participant('Alice', {
...Shared,
wager: UInt,
// Specify Alice's interact interface here
});
const B = Participant('Bob', {
...Shared,
// Specify Bob's interact interface here
});
init();
A.only(() => {
const wager = declassify(interact.wager);
const [cardA, cardB] = declassify(interact.startGame());
});
// The first one to publish deploys the contract
A.publish(wager, cardA, cardB)
.pay(wager);
commit();
// The second one to publish always attaches
B.pay(wager);
// flag controls exiting the loop
var [sumA, flag] = [(cardValue(cardA) + cardValue(cardB)), 0];
invariant(balance() == 2 * wager);
while(sumA < 21 && flag == 0){
commit();
A.only(() => {
const next = declassify(interact.getCard());
});
A.publish(next);
if(next > 0){
[sumA, flag] = [(cardValue(next) + sumA), 0];
continue;
} else {
[sumA, flag] = [sumA, 1];
continue;
}
};
transfer(2 * wager).to(B);
//assert(flag == 1);
commit();
exit();
});