summaryrefslogtreecommitdiff
path: root/appl/spree/engines/afghan.b
blob: 0ca2ca49aac8b6b5241b8f3463feeebc1a649356 (plain)
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
implement Gatherengine;

include "sys.m";
	sys: Sys;
include "draw.m";
include "sets.m";
	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: import cardlib;
	dTOP, dLEFT, oLEFT, oRIGHT, EXPAND, FILLX, FILLY, aUPPERCENTRE,
	Stackspec: import Cardlib;
include "../gather.m";

CLICK, REDEAL: con iota;

clique: ref Clique;
rows: array of ref Object;		# [10]
central: array of ref Object;	# [4]
chokey, deck: ref Object;
direction := 0;
nredeals := 0;

Rowpilespec := Stackspec(
	"display",		# style
	10,			# maxcards
	0,			# conceal
	nil			# title
);

Centralpilespec := Stackspec(
	"pile",
	13,
	0,
	nil
);

clienttype(): string
{
	return "cards";
}

maxmembers(): int
{
	return 1;
}

readfile(nil: int, nil: big, nil: int): array of byte
{
	return nil;
}

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);
	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;
}

propose(members: array of string): string
{
	if (len members != 1)
		return "one member only";
	return nil;
}

archive()
{
	archiveobj := cardlib->archive();
	allow->archive(archiveobj);
	cardlib->archivearray(rows, "rows");
	cardlib->archivearray(central, "central");
	cardlib->setarchivename(chokey, "chokey");
	cardlib->setarchivename(deck, "deck");
	archiveobj.setattr("direction", string direction, None);
	archiveobj.setattr("nredeals", string nredeals, None);
}

start(members: array of ref Member, archived: int)
{
	if (archived) {
		archiveobj := cardlib->unarchive();
		allow->unarchive(archiveobj);
		rows = cardlib->getarchivearray("rows");
		central = cardlib->getarchivearray("central");
		chokey = cardlib->getarchiveobj("chokey");
		deck = cardlib->getarchiveobj("deck");
		direction = int archiveobj.getattr("direction");
		nredeals = int archiveobj.getattr("nredeals");
	} else {
		p := members[0];
		Cmember.join(p, -1).layout.lay.setvisibility(All);
		startclique();
		allow->add(CLICK, p, "click %o %d");
	}
}

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 {
	REDEAL =>
		if (nredeals >= 3)
			return "no more redeals";
		redeal();
		nredeals++;
	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) {
			# selecting a card to move
			if (idx < 0 || idx >= len stack.children)
				return "invalid index";
			case stype {
			"row" or
			"chokey" =>
				select(cp, stack, (nc - 1, nc));
			* =>
				return "you can't move cards from there";
			}
		} else {
			# selecting a stack to move to.
			card := cardlib->getcard(sel.stack.children[sel.r.start]);
			case stype {
			"central" =>
				top := cardlib->getcard(stack.children[nc - 1]);
				if (direction == 0) {
					if (card.number != (top.number + 1) % 13 &&
							card.number != (top.number + 12) % 13)
						return "out of sequence";
					if (card.suit != top.suit)
						return "wrong suit";
					direction = card.number - top.number;
				} else {
					if (card.number != (top.number + direction + 13) % 13)
						return "out of sequence";
					if (card.suit != top.suit)
						return "wrong suit";
				}
			"row" =>
				if (nc == 0 || sel.stack.getattr("type") == "chokey")
					return "you wish!";
				top := cardlib->getcard(stack.children[nc - 1]);
				if (card.suit != top.suit)
					return "wrong suit";
				if (card.number != (top.number + 1) % 13 &&
						card.number != (top.number + 12) % 13)
					return "out of sequence";
			"chokey" =>
				if (nc != 0)
					return "only one card allowed there";
			* =>
				return "can't move there";
			}
			sel.transfer(stack, -1);
		}
	}
	return nil;
}

startclique()
{
	addlayobj, addlayframe: import cardlib;

	entry := clique.newobject(nil, All, "widget entry");
	entry.setattr("command", "say", All);

	but := clique.newobject(nil, All, "widget button");
	but.setattr("text", "Redeal", All);
	but.setattr("command", "redeal", All);
	allow->add(REDEAL, Cmember.index(0).p, "redeal");

	addlayframe("topf", nil, nil, dTOP|EXPAND|FILLX|aUPPERCENTRE, dTOP);
	addlayobj(nil, "topf", nil, dLEFT, but);
	addlayobj(nil, "topf", nil, dLEFT|EXPAND|FILLX, entry);

	addlayframe("arena", nil, nil, dTOP|EXPAND|FILLX|FILLY, dTOP);

	addlayframe("left", "arena", nil, dLEFT|EXPAND, dTOP);
	addlayframe("central", "arena", nil, dLEFT|EXPAND, dTOP);
	addlayframe("right", "arena", nil, dLEFT|EXPAND, dTOP);

	rows = array[10] of {* => newstack(nil, Rowpilespec, "row")};
	central = array[4] of {* => newstack(nil, Centralpilespec, "central")};
	chokey = newstack(nil, Centralpilespec, "chokey");

	deck = clique.newobject(nil, All, "stack");
	cardlib->makecards(deck, (0, 13), nil);
	cardlib->shuffle(deck);

	for (i := 0; i < 5; i++)
		addlayobj(nil, "left", nil, dTOP|oRIGHT, rows[i]);
	for (i = 5; i < 10; i++)
		addlayobj(nil, "right", nil, dTOP|oRIGHT, rows[i]);
	for (i = 0; i < 4; i++)
		addlayobj(nil, "central", nil, dTOP, central[i]);
	addlayobj(nil, "central", nil, dTOP, chokey);

	for (i = 0; i < 52; i++)
		cardlib->setface(deck.children[i], 1);
	# get top card from deck for central piles.
	c := deck.children[len deck.children - 1];
	v := cardlib->getcard(c);
	j := 0;
	for (i = len deck.children - 1; i >= 0; i--) {
		w := cardlib->getcard(deck.children[i]);
		if (w.number == v.number)
			deck.transfer((i, i + 1), central[j++], -1);
	}
	for (i = 0; i < 10; i += 5) {
		for (j = i; j < i + 4; j++)
			deck.transfer((0, 5), rows[j], -1);
		deck.transfer((0, 4), rows[j], -1);
	}
}

redeal()
{
	for (i := 0; i < len rows; i++)
		cardlib->discard(rows[i], deck, 0);
	cardlib->shuffle(deck);

	i = 0;
	while ((n := len deck.children) > 0) {
		l, r: int;
		if (n >= 10)
			l = r = 5;
		else {
			l = n / 2;
			r = n - l;
		}
		deck.transfer((0, l), rows[i], 0);
		deck.transfer((0, r), rows[i + 5], 0);
		i++;
	}

	n = cardlib->nmembers();
	for (i = 0; i < n; i++)
		Cmember.index(i).sel.set(nil);
}

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;
}

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);
	}
}

archivearray(a: array of ref Object, name: string)
{
	for (i := 0; i < len a; i++)
		cardlib->setarchivename(a[i], name + string i);
}

unarchivearray(a: array of ref Object, name: string)
{
	for (i := 0; i < len a; i++)
		a[i] = cardlib->getarchiveobj(name + string i);
}