-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.rsh
202 lines (177 loc) · 4.7 KB
/
index.rsh
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
'reach 0.1';
const ROWS = 3;
const COLS = 3;
const CELLS = ROWS * COLS;
const cells_type = Array(Bool, 9);
const Board_type = Object({
turn:Bool,
O:cells_type,
X:cells_type,
win:Bool
})
const cells = Array.replicate(CELLS,false);
const newBoard = (turn) => ({
turn: turn,
O:cells,
X:cells,
win: false
})
const cellBoth = (board, i) =>
(board.X[i] || board.O[i]);
const validMove = (board, m) => (! cellBoth(board, m));
const validStep = (step) => (0<=step && step<CELLS);
function getValidPlay(interact, board) {
const step = interact.getStep(board);
assume(validStep(step));
assume(validMove(board, step));
return declassify(step);
}
function step(board,pos){
require(validStep(pos));
require(validMove(board,pos));
return {
turn:!board.turn,
X:board.turn? board.X.set(pos,true) : board.X ,
O:board.turn? board.O : board.O.set(pos,true) ,
win: false
};
}
function getCell(singleBoard,i){
if(0<=i && i<CELLS){
return singleBoard[i];
}else{
return false;
}
}
function getLine(singleBoard,i,len){
return (
getCell(singleBoard,i) &&
getCell(singleBoard,add(i,len)) &&
getCell(singleBoard,i+len+len));
}
function isWin(singleBoard) {
return (
getLine(singleBoard,0,1) ||
getLine(singleBoard,3,1) ||
getLine(singleBoard,6,1) ||
getLine(singleBoard,0,3) ||
getLine(singleBoard,1,3) ||
getLine(singleBoard,2,3) ||
getLine(singleBoard,0,4) ||
getLine(singleBoard,2,2)
);
}
function allPlaced(board){
return (
cellBoth(board,0) &&
cellBoth(board,1) &&
cellBoth(board,2) &&
cellBoth(board,3) &&
cellBoth(board,4) &&
cellBoth(board,5) &&
cellBoth(board,6) &&
cellBoth(board,7) &&
cellBoth(board,8)
)
}
function isDone(board){
return (isWin(board.O) || isWin(board.X) || allPlaced(board));
}
const finalBoardX = (board) => ({
...board,
win: isWin(board.X)
})
const finalBoardO = (board) => ({
...board,
win: isWin(board.O)
})
const Player = {
...hasRandom,
getStep: Fun([Board_type], UInt),
informTimeout: Fun([], Null),
getId: Fun([], UInt),
getUrl: Fun([], Array(UInt,32)),
preview: Fun([UInt, Array(UInt,32)], Null),
showEnd: Fun([Board_type, UInt, Address, Array(UInt,32)], Null)
};
const Alice = {
...Player,
wager: UInt,
deadline: UInt,
}
const Bob = {
...Player,
acceptWager: Fun([UInt], Null)
}
const Nft =
{ owner: Address,
url: Array(UInt,32) };
export const main = Reach.App(
{}, [Participant('Alice', Alice), Participant('Bob', Bob), View('NFT', Nft)],
(A, B, vNFT) => {
const informTimeout = () => {
each([A, B], () => {
interact.informTimeout(); }); };
A.only(() => {
const wager = declassify(interact.wager);
const deadline = declassify(interact.deadline);
});
A.publish(wager, deadline)
.pay(wager);
commit();
B.only(() => {
interact.acceptWager(wager); });
B.pay(wager)
.timeout(relativeTime(deadline), () => closeTo(A, informTimeout));
commit();
A.only(() => {
const id = declassify(interact.getId());
const url = declassify(interact.getUrl());
});
A.publish(id)
.timeout(relativeTime(deadline), () => closeTo(A, informTimeout));
commit();
A.publish(url)
.timeout(relativeTime(deadline), () => closeTo(A, informTimeout));
each([A, B], () => {
interact.preview(id, url);
});
var board = newBoard(true);
invariant(balance() == 2 * wager);
while(!isDone(board)){
if(board.turn){
commit();
A.only(() => {
const Aplay = getValidPlay(interact,board);});
A.publish(Aplay)
.timeout(relativeTime(deadline), () => closeTo(B, informTimeout));
board = step(board, Aplay);
continue;
}else {
commit();
B.only(() => {
const Bplay = getValidPlay(interact,board);});
B.publish(Bplay)
.timeout(relativeTime(deadline), () => closeTo(A, informTimeout));
board = step(board, Bplay);
continue;
}
}
const [ toA, toB ] =
(isWin( board.X ) ? [ 2, 0 ]
: (isWin( board.O ) ? [ 0, 2 ]
: [ 1, 1 ]));
const owner = isWin( board.X ) ? A : B;
vNFT.owner.set(owner);
vNFT.url.set(url);
transfer(toA * wager).to(A);
transfer(toB * wager).to(B);
commit();
A.only(()=>{
interact.showEnd(finalBoardX(board), id, owner, url);
});
B.only(()=>{
interact.showEnd(finalBoardO(board), id, owner, url);
});
}
);