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
|
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;
getcard: import cardlib;
dTOP, dRIGHT, dLEFT, oRIGHT, oDOWN,
aCENTRERIGHT, aCENTRELEFT, aUPPERRIGHT,
EXPAND, FILLX, FILLY, Stackspec: import Cardlib;
include "../gather.m";
clique: ref Clique;
open: array of ref Object; # [8]
cells: array of ref Object; # [4]
acepiles: array of ref Object; # [4]
txpiles: array of ref Object; # [len open + len cells]
deck: ref Object;
fnames := array[] of {
"qua",
"quack",
"quackery",
"quad",
"quadrangle",
"quadrangular",
"quadrant",
"quadratic",
"quadrature",
"quadrennial",
};
dir(name: string, perm: int, owner: string): Sys->Dir
{
d := Sys->zerodir;
d.name = name;
d.uid = owner;
d.gid = owner;
d.qid.qtype = (perm >> 24) & 16rff;
d.mode = perm;
# d.atime = now;
# d.mtime = now;
return d;
}
suitsout := array[4] of {* => -1};
mainmember: ref Cmember;
CLICK: con iota;
Openspec := Stackspec(
"display", # style
19, # maxcards
0, # conceal
"" # title
);
Pilespec := Stackspec(
"pile", # style
19, # maxcards
0, # conceal
"pile" # title
);
Untitledpilespec := Stackspec(
"pile", # style
13, # maxcards
0, # conceal
"" # title
);
clienttype(): string
{
return "cards";
}
maxmembers(): int
{
return 1;
}
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";
}
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);
g.fcreate(0, -1, dir("data", 8r555|Sys->DMDIR, "spree"));
for(i := 0; i < len fnames; i++)
g.fcreate(i + 1, 0, dir(fnames[i], 8r444, "arble"));
return nil;
}
propose(members: array of string): string
{
if (len members != 1)
return "one member only";
return nil;
}
start(members: array of ref Member, archived: int)
{
sys->print("freecell: starting\n");
if (archived) {
archiveobj := cardlib->unarchive();
open = cardlib->getarchivearray("open");
cells = cardlib->getarchivearray("cells");
acepiles = cardlib->getarchivearray("acepiles");
txpiles = cardlib->getarchivearray("txpiles");
deck = cardlib->getarchiveobj("deck");
for (i := 0; i < len suitsout; i++)
suitsout[i] = int archiveobj.getattr("suitsout" + string i);
mainmember = Cmember.findid(int archiveobj.getattr("mainmember"));
allow->unarchive(archiveobj);
archiveobj.delete();
} else {
sys->print("freecell: starting afresh\n");
mainmember = Cmember.join(members[0], -1);
mainmember.layout.lay.setvisibility(All);
startclique();
movefree();
allow->add(CLICK, members[0], "click %o %d");
}
}
readfile(f: int, boffset: big, n: int): array of byte
{
offset := int boffset;
f--;
if (f < 0 || f >= len fnames)
return nil;
data := array of byte fnames[f];
if (offset >= len data)
return nil;
if (offset + n > len data)
n = len data - offset;
return data[offset:offset + n];
}
archive()
{
sys->print("freecell: archiving\n");
archiveobj := cardlib->archive();
cardlib->archivearray(open, "open");
cardlib->archivearray(cells, "cells");
cardlib->archivearray(acepiles, "acepiles");
cardlib->archivearray(txpiles, "txpiles");
cardlib->setarchivename(deck, "deck");
for (i := 0; i < len suitsout; i++)
archiveobj.setattr("suitsout" + string i, string suitsout[i], None);
archiveobj.setattr("mainmember", string mainmember.id, None);
allow->archive(archiveobj);
}
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 "you are not playing";
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");
if (sel.isempty() || sel.stack == stack) {
if (idx < 0 || idx >= len stack.children)
return "invalid index";
case stype {
"cell" or
"open" =>
select(cp, stack, (idx, nc));
* =>
return "you can't move cards from there";
}
} else {
from := sel.stack;
case stype {
"acepile" =>
if (sel.r.end != sel.r.start + 1)
return "only one card at a time!";
addtoacepile(sel.stack);
sel.set(nil);
movefree();
"open" =>
c := getcard(sel.stack.children[sel.r.start]);
col := !isred(c.suit);
n := c.number + 1;
for (i := sel.r.start; i < sel.r.end; i++) {
c2 := getcard(sel.stack.children[i]);
if (isred(c2.suit) == col)
return "bad colour sequence";
if (c2.number != n - 1)
return "bad number sequence";
n = c2.number;
col = isred(c2.suit);
}
if (nc != 0) {
c2 := getcard(stack.children[nc - 1]);
if (isred(c2.suit) == isred(c.suit) || c2.number != c.number + 1)
return "opposite colours, descending, only";
}
r := sel.r;
selstack := sel.stack;
sel.set(nil);
fc := freecells(stack);
if (r.end - r.start - 1 > len fc)
return "not enough free cells";
n = 0;
for (i = r.end - 1; i >= r.start + 1; i--)
selstack.transfer((i, i + 1), fc[n++], -1);
selstack.transfer((i, i + 1), stack, -1);
while (--n >= 0)
fc[n].transfer((0, 1), stack, -1);
movefree();
"cell" =>
if (sel.r.end - sel.r.start > 1 || nc > 0)
return "only one card allowed there";
sel.transfer(stack, -1);
movefree();
* =>
return "can't move there";
}
}
}
return nil;
}
freecells(dest: ref Object): array of ref Object
{
fc := array[len txpiles] of ref Object;
n := 0;
for (i := 0; i < len txpiles; i++)
if (len txpiles[i].children == 0 && txpiles[i] != dest)
fc[n++] = txpiles[i];
return fc[0:n];
}
# move any cards that can be moved.
movefree()
{
nmoved := 1;
while (nmoved > 0) {
nmoved = 0;
for (i := 0; i < len txpiles; i++) {
pile := txpiles[i];
nc := len pile.children;
if (nc == 0)
continue;
card := getcard(pile.children[nc - 1]);
if (suitsout[card.suit] != card.number - 1)
continue;
# card can be moved; now make sure there's no card out
# that might be moved onto this card
for (j := 0; j < len suitsout; j++)
if (isred(j) != isred(card.suit) && card.number > 1 && suitsout[j] < card.number - 1)
break;
if (j == len suitsout) {
addtoacepile(pile);
nmoved++;
}
}
}
}
addtoacepile(pile: ref Object)
{
nc := len pile.children;
if (nc == 0)
return;
card := getcard(pile.children[nc - 1]);
for (i := 0; i < len acepiles; i++) {
anc := len acepiles[i].children;
if (anc == 0) {
if (card.number == 0)
break;
continue;
}
acard := getcard(acepiles[i].children[anc - 1]);
if (acard.suit == card.suit && acard.number == card.number - 1)
break;
}
if (i < len acepiles) {
pile.transfer((nc - 1, nc), acepiles[i], -1);
suitsout[card.suit] = card.number;
}
}
startclique()
{
addlayobj, addlayframe: import cardlib;
open = array[8] of {* => newstack(nil, Openspec, "open", nil)};
acepiles = array[4] of {* => newstack(nil, Untitledpilespec, "acepile", nil)};
cells = array[4] of {* => newstack(nil, Untitledpilespec, "cell", "cell")};
for (i := 0; i < len cells; i++)
cells[i].setattr("showsize", "0", All);
txpiles = array[12] of ref Object;
txpiles[0:] = open;
txpiles[len open:] = cells;
deck = clique.newobject(nil, All, "stack");
cardlib->makecards(deck, (0, 13), nil);
addlayframe("arena", nil, nil, dTOP|EXPAND|FILLX|FILLY, dTOP);
addlayframe("top", "arena", nil, dTOP|EXPAND, dTOP);
addlayframe("bot", "arena", nil, dTOP|EXPAND, dTOP);
for (i = 0; i < 4; i++)
addlayobj(nil, "top", nil, dRIGHT, acepiles[i]);
for (i = 0; i < 4; i++)
addlayobj(nil, "top", nil, dLEFT, cells[i]);
for (i = 0; i < len open; i++)
addlayobj(nil, "bot", nil, dLEFT|oDOWN|EXPAND, open[i]);
deal();
}
deal()
{
cardlib->shuffle(deck);
cardlib->deal(deck, 7, open, 0);
}
newstack(parent: ref Object, spec: Stackspec, stype, title: string): ref Object
{
stack := cardlib->newstack(parent, nil, spec);
stack.setattr("type", stype, None);
stack.setattr("actions", "click", All);
stack.setattr("title", title, All);
return stack;
}
isred(suit: int): int
{
return suit == Cardlib->DIAMONDS || 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);
}
}
#randstate := 1;
#srand(seed: int)
#{
# randstate = seed;
#}
#
#rand(): int
#{
# randstate = randstate * 214013 + 2531011;
# return (randstate >> 16) & 0x7fff;
#}
##From: jimh@MICROSOFT.com (Jim Horne)
##
##I'm happy to share the card shuffle algorithm, but I warn you,
##it does depend on the rand() and srand() function built into MS
##compilers. The good news is that I believe these work the same
##for all our compilers.
##
##I use cards.dll which has it's own mapping of numbers (0-51) to
##cards. The following will give you the idea. Play around with
##this and you'll be able to generate all the cliques.
##
##Go ahead and post the code. People might as well have fun with it.
##Please keep me posted on anything interesting that comes of it.
##Thanks.
#
#msdeal(cliquenumber: int): array of array of Card
#{
# deck := array[52] of Card;
# for (i := 0; i < len deck; i++) # put unique card in each deck loc.
# deck[i] = Card(i % 4, i / 4, 0);
# wleft := 52; # cards left to be chosen in shuffle
# cards := array[8] of {* => array[7] of Card};
# max := array[8] of {* => 0};
# srand(cliquenumber);
# for (i = 0; i < 52; i++) {
# j := rand() % wleft;
# card[i % 8][i / 8] = deck[j];
# max[i % 8] = i / 8;
# deck[j] = deck[--wleft];
# }
# for (i = 0; i < len cards; i++)
# cards[i] = cards[i][0:max[i]];
# return cards;
#}
|