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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
|
implement Gatherengine;
include "sys.m";
sys: Sys;
include "draw.m";
include "sets.m";
sets: Sets;
Set, set, A, B, All, None: import sets;
include "../spree.m";
spree: Spree;
Attributes, Range, Object, Clique, Member, rand: import spree;
include "allow.m";
allow: Allow;
include "cardlib.m";
cardlib: Cardlib;
Selection, Cmember, Card: import cardlib;
dTOP, dLEFT, oDOWN, EXPAND, FILLX, FILLY, aCENTRELEFT, Stackspec: import Cardlib;
include "../gather.m";
clique: ref Clique;
CLICK, SAY, SHOW: con iota;
KING: con 12;
NACES: con 7; # number of ace piles to fit across the board.
Dmember: adt {
pile,
spare1,
spare2: ref Object;
open: array of ref Object; # [4]
acepiles: array of ref Object;
};
scores: array of int;
scorelabel: ref Object;
dmembers: array of ref Dmember;
Openspec := Stackspec(
"display", # style
4, # maxcards
0, # conceal
"" # title
);
Pilespec := Stackspec(
"pile", # style
13, # maxcards
0, # conceal
"pile" # title
);
Untitledpilespec := Stackspec(
"pile", # style
13, # maxcards
0, # conceal
"" # title
);
clienttype(): string
{
return "cards";
}
init(srvmod: Spree, g: ref Clique, nil: list of string, nil: int): string
{
sys = load Sys Sys->PATH;
clique = g;
spree = srvmod;
allow = load Allow Allow->PATH;
if (allow == nil) {
sys->print("whist: cannot load %s: %r\n", Allow->PATH);
return "bad module";
}
allow->init(spree, clique);
sets = load Sets Sets->PATH;
if (sets == nil) {
sys->print("whist: cannot load %s: %r\n", Sets->PATH);
return "bad module";
}
sets->init();
cardlib = load Cardlib Cardlib->PATH;
if (cardlib == nil) {
sys->print("whist: cannot load %s: %r\n", Cardlib->PATH);
return "bad module";
}
cardlib->init(spree, clique);
return nil;
}
maxmembers(): int
{
return 100;
}
readfile(nil: int, nil: big, nil: int): array of byte
{
return nil;
}
propose(members: array of string): string
{
if (len members < 3)
return "need at least 3 members";
return nil;
}
archive()
{
archiveobj := cardlib->archive();
allow->archive(archiveobj);
for (i := 0; i < len dmembers; i++) {
dp := dmembers[i];
s := "d" + string i + "_";
cardlib->setarchivename(dp.spare1, s + "spare1");
cardlib->setarchivename(dp.spare2, s + "spare2");
cardlib->setarchivename(dp.pile, s + "pile");
cardlib->archivearray(dp.open, s + "open");
cardlib->archivearray(dp.acepiles, s + "acepiles");
}
cardlib->setarchivename(scorelabel, "scorelabel");
s := "";
for (i = 0; i < len scores; i++)
s += " " + string scores[i];
archiveobj.setattr("scores", s, None);
}
start(members: array of ref Member, archived: int)
{
if (archived) {
archiveobj := cardlib->unarchive();
allow->unarchive(archiveobj);
dmembers = array[len members] of ref Dmember;
for (i := 0; i < len dmembers; i++) {
dp := dmembers[i] = ref Dmember;
s := "d" + string i + "_";
dp.spare1 = cardlib->getarchiveobj(s + "spare1");
dp.spare2 = cardlib->getarchiveobj(s + "spare2");
dp.pile = cardlib->getarchiveobj(s + "pile");
dp.open = cardlib->getarchivearray(s + "open");
dp.acepiles = cardlib->getarchivearray(s + "acepiles");
}
scorelabel = cardlib->getarchiveobj("scorelabel");
s := archiveobj.getattr("scores");
(n, toks) := sys->tokenize(s, " ");
scores = array[len members] of int;
for (i = 0; toks != nil; toks = tl toks)
scores[i++] = int hd toks;
} else {
pset := None;
for (i := 0; i < len members; i++) {
p := members[i];
Cmember.join(p, i);
pset = pset.add(p.id);
allow->add(CLICK, p, "click %o %d");
}
Cmember.index(0).layout.lay.setvisibility(All.X(A&~B, pset).add(members[0].id));
layout();
deal();
allow->add(SAY, nil, "say &");
}
}
command(p: ref Member, cmd: string): string
{
(err, tag, toks) := allow->action(p, cmd);
if (err != nil)
return err;
cp := Cmember.find(p);
if (cp == nil)
return "bad member";
case tag {
CLICK =>
# click stack index
stack := clique.objects[int hd tl toks];
nc := len stack.children;
idx := int hd tl tl toks;
sel := cp.sel;
stype := stack.getattr("type");
d := dmembers[cp.ord];
if (sel.isempty() || sel.stack == stack) {
# selecting a card to move
if (nc == 0 && stype == "spare1") {
cardlib->flip(d.spare2);
d.spare2.transfer((0, len d.spare2.children), d.spare1, 0);
return nil;
}
if (idx < 0 || idx >= len stack.children)
return "invalid index";
if (owner(stack) != cp)
return "not yours, don't touch!";
case stype {
"spare2" or
"pile" =>
select(cp, stack, (nc - 1, nc));
"open" =>
select(cp, stack, (idx, nc));
"spare1" =>
if ((n := nc) > 3)
n = 3;
for (i := 0; i < n; i++) {
cardlib->setface(stack.children[nc - 1], 1);
stack.transfer((nc - 1, nc), d.spare2, -1);
nc--;
}
* =>
return "you can't move cards from there";
}
} else {
# selecting a stack to move to.
frompile := sel.stack.getattr("type") == "pile";
case stype {
"acepile" =>
if (sel.r.end != sel.r.start + 1)
return "only one card at a time!";
card := getcard(sel.stack.children[sel.r.start]);
if (nc == 0) {
if (card.number != 0)
return "aces only";
} else {
top := getcard(stack.children[nc - 1]);
if (card.number != top.number + 1)
return "out of sequence";
if (card.suit != top.suit)
return "wrong suit";
}
sel.transfer(stack, -1);
if (card.number == KING) # kings get flipped
cardlib->setface(stack.children[len stack.children - 1], 0);
"open" =>
if (owner(stack) != cp)
return "not yours, don't touch!";
c := getcard(sel.stack.children[sel.r.start]);
col := !isred(c);
n := c.number + 1;
for (i := sel.r.start; i < sel.r.end; i++) {
c2 := getcard(sel.stack.children[i]);
if (isred(c2) == col)
return "bad colour sequence";
if (c2.number != n - 1)
return "bad number sequence";
n = c2.number;
col = isred(c2);
}
if (nc != 0) {
c2 := getcard(stack.children[nc - 1]);
if (isred(c2) == isred(c) || c2.number != c.number + 1)
return "invalid move";
}
sel.transfer(stack, -1);
* =>
return "can't move there";
}
if (frompile) {
nc = len d.pile.children;
if (nc == 0) {
endround();
deal();
} else {
cardlib->setface(d.pile.children[nc - 1], 1);
d.pile.setattr("title", "pile [" + string nc + "]", All);
}
}
}
SAY =>
clique.action("say member " + string p.id + ": '" + joinwords(tl toks) + "'", nil, nil, All);
SHOW =>
clique.show(nil);
}
return nil;
}
getcard(card: ref Object): Card
{
return cardlib->getcard(card);
}
isred(c: Card): int
{
return c.suit == Cardlib->DIAMONDS || c.suit == Cardlib->HEARTS;
}
select(cp: ref Cmember, stack: ref Object, r: Range)
{
if (cp.sel.isempty()) {
cp.sel.set(stack);
cp.sel.setrange(r);
} else {
if (cp.sel.r.start == r.start && cp.sel.r.end == r.end)
cp.sel.set(nil);
else
cp.sel.setrange(r);
}
}
owner(stack: ref Object): ref Cmember
{
parent := clique.objects[stack.parentid];
n := cardlib->nmembers();
for (i := 0; i < n; i++) {
cp := Cmember.index(i);
if (cp.obj == parent)
return cp;
}
return nil;
}
layout()
{
n := cardlib->nmembers();
dmembers = array[n] of ref Dmember;
for (i := 0; i < n; i++) {
cp := Cmember.index(i);
d := dmembers[i] = ref Dmember;
d.spare1 = newstack(cp.obj, Untitledpilespec, "spare1");
d.spare2 = newstack(cp.obj, Untitledpilespec, "spare2");
d.pile = newstack(cp.obj, Pilespec, "pile");
d.open = array[4] of {* => newstack(cp.obj, Openspec, "open")};
d.acepiles = array[4] of {* => newstack(cp.obj, Untitledpilespec, "acepile")};
cardlib->makecards(d.spare1, (0, 13), string i);
}
entry := clique.newobject(nil, All, "widget entry");
entry.setattr("command", "say", All);
cardlib->addlayobj(nil, nil, nil, dTOP|FILLX, entry);
scores = array[n] of {* => 0};
scorelabel = clique.newobject(nil, All, "widget label");
setscores();
cardlib->addlayobj(nil, nil, nil, dTOP|FILLX, scorelabel);
cardlib->addlayframe("arena", nil, nil, dTOP|EXPAND|FILLX|FILLY, dTOP);
row := 0;
col := 0;
maketable("arena");
for (i = 0; i < n; i++) {
d := dmembers[i];
f := "p" + string i;
cardlib->addlayobj(nil, f, nil, dLEFT, d.spare1);
cardlib->addlayobj(nil, f, nil, dLEFT, d.spare2);
cardlib->addlayobj(nil, f, nil, dLEFT, d.pile);
for (j := 0; j < len d.open; j++)
cardlib->addlayobj(nil, f, nil, dLEFT|EXPAND|oDOWN, d.open[j]);
for (j = 0; j < len d.acepiles; j++) {
cardlib->addlayobj(nil, "a" + string row, nil, dLEFT|EXPAND, d.acepiles[j]);
if (++col >= NACES) {
col = 0;
row++;
}
}
}
}
setscores()
{
s := "Scores: ";
n := cardlib->nmembers();
for (i := 0; i < n; i++) {
s += Cmember.index(i).p.name + ": " + string scores[i];
if (i < n - 1)
s[len s] = ' ';
}
scorelabel.setattr("text", s, All);
}
deal()
{
n := cardlib->nmembers();
for (i := 0; i < n; i++) {
cp := Cmember.index(i);
d := dmembers[i];
deck := d.spare1;
cardlib->shuffle(deck);
deck.transfer((0, 13), d.pile, 0);
cardlib->setface(d.pile.children[12], 1);
d.pile.setattr("title", "pile [13]", All);
for (j := 0; j < len d.open; j++) {
deck.transfer((0, 1), d.open[j], 0);
cardlib->setface(d.open[j].children[0], 1);
}
}
}
endround()
{
# go through all the ace piles, moving cards back to the appropriate deck
# and counting appropriately.
# move all other cards back too.
n := cardlib->nmembers();
for (i := 0; i < n; i++) {
d := dmembers[i];
Cmember.index(i).sel.set(nil);
for (j := 0; j < len d.acepiles; j++) {
acepile := d.acepiles[j];
nc := len acepile.children;
for (k := nc - 1; k >= 0; k--) {
card := acepile.children[k];
back := int card.getattr("rear");
scores[back]++;
if (getcard(card).number == KING)
scores[back] += 5;
cardlib->setface(card, 0);
acepile.transfer((k, k + 1), dmembers[back].spare1, -1);
}
}
if (len d.pile.children == 0)
scores[i] += 10; # bonus for going out
else
scores[i] -= len d.pile.children;
cardlib->discard(d.pile, d.spare1, 1);
cardlib->discard(d.spare2, d.spare1, 1);
for (j = 0; j < len d.open; j++)
cardlib->discard(d.open[j], d.spare1, 1);
}
setscores();
}
maketable(parent: string)
{
addlayframe: import cardlib;
n := cardlib->nmembers();
na := ((n * 4) + (NACES - 1)) / NACES;
for (i := 0; i < n; i++) {
layout := Cmember.index(i).layout;
# one frame for each member other than self;
# then all the ace piles; then self.
for (j := 0; j < n; j++)
if (j != i)
addlayframe("p" + string j, parent, layout, dTOP|EXPAND, dTOP);
for (j = 0; j < na; j++)
addlayframe("a" + string j, parent, layout, dTOP|EXPAND|aCENTRELEFT, dTOP);
addlayframe("p" + string i, parent, layout, dTOP|EXPAND, dTOP);
}
}
newstack(parent: ref Object, spec: Stackspec, stype: string): ref Object
{
stack := cardlib->newstack(parent, nil, spec);
stack.setattr("type", stype, None);
stack.setattr("actions", "click", All);
return stack;
}
joinwords(v: list of string): string
{
if (v == nil)
return nil;
s := hd v;
for (v = tl v; v != nil; v = tl v)
s += " " + hd v;
return s;
}
remark(s: string)
{
clique.action("remark " + s, nil, nil, All);
}
|