-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcc.lua
292 lines (259 loc) · 7.08 KB
/
cc.lua
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
-- cc
-- local log = require 'log'
local CC = {}
local proneError = 'Cannot move a face down card'
local ordStr2English = {
A = 'an Ace',
['2'] = 'a Two',
['3'] = 'a Three',
['4'] = 'a Four',
['5'] = 'a Five',
['6'] = 'a Six',
['7'] = 'a Seven',
['8'] = 'an Eight',
['9'] = 'a Nine',
['10'] = 'a Ten',
J = 'a Jack',
Q = 'a Queen',
K = 'a King',
}
function CC.EitherProne(cpair)
return cpair[1].prone or cpair[2].prone
end
function CC.Empty(pile, card)
if card.prone then return proneError end
if pile.label then
if pile.label == 'X' then
return 'Cannot move cards there'
end
local ordStr = _G.ORD2STRING[card.ord]
if pile.label ~= ordStr then
return string.format('Can only accept %s, not %s', ordStr2English[pile.label], ordStr2English[ordStr])
end
end
return nil
end
function CC.None(cpair)
if CC.EitherProne(cpair) then return proneError end
return 'No'
end
function CC.Any(cpair)
if CC.EitherProne(cpair) then return proneError end
return nil
end
function CC.Up(cpair)
if CC.EitherProne(cpair) then return proneError end
if cpair[1].ord + 1 ~= cpair[2].ord then
return 'Cards must be in ascending sequence'
end
return nil
end
function CC.Down(cpair)
if CC.EitherProne(cpair) then return proneError end
if cpair[1].ord ~= cpair[2].ord + 1 then
return 'Cards must be in descending sequence'
end
return nil
end
function CC.UpOrDownWrap(cpair)
if CC.EitherProne(cpair) then return proneError end
if cpair[1].ord == 13 and cpair[2].ord == 1 then
return nil
elseif cpair[1].ord == 1 and cpair[2].ord == 13 then
return nil
elseif cpair[1].ord == cpair[2].ord + 1 then
return nil -- eg 4 on 3
elseif cpair[1].ord + 1 == cpair[2].ord then
return nil -- eg 3 on 4
else
return 'Cards must be up or down (Aces on Kings allowed)'
end
end
function CC.UpOrDownSuitWrap(cpair)
if CC.EitherProne(cpair) then return proneError end
if cpair[1].suit ~= cpair[2].suit then
return 'Must be the same suit'
end
return CC.UpOrDownWrap(cpair)
end
function CC.UpOrDown(cpair)
if CC.EitherProne(cpair) then return proneError end
if cpair[1].ord == cpair[2].ord + 1 then
return nil -- eg 4 on 3
elseif cpair[1].ord + 1 == cpair[2].ord then
return nil -- eg 3 on 4
else
return 'Cards must be up or down'
end
end
function CC.UpOrDownSuit(cpair)
if CC.EitherProne(cpair) then return proneError end
if cpair[1].suit ~= cpair[2].suit then
return 'Cards must be the same suit'
end
return CC.UpOrDown(cpair)
end
function CC.UpSuit(cpair)
if CC.EitherProne(cpair) then return proneError end
if cpair[1].suit ~= cpair[2].suit then
return 'Cards must be the same suit'
end
return CC.Up(cpair)
end
function CC.UpSuitTwo(cpair)
if CC.EitherProne(cpair) then return proneError end
if cpair[1].suit ~= cpair[2].suit then
return 'Cards must be the same suit'
end
if cpair[1].ord + 2 ~= cpair[2].ord then
return 'Cards must be up by two'
end
return nil
end
function CC.UpSuitTwoWrap(cpair)
if CC.EitherProne(cpair) then return proneError end
if cpair[1].suit ~= cpair[2].suit then
return 'Cards must be the same suit'
end
if cpair[1].ord == 12 and cpair[2].ord == 1 then
elseif cpair[1].ord == 13 and cpair[2].ord == 2 then
elseif cpair[1].ord + 2 ~= cpair[2].ord then
return 'Cards must be up by two'
end
return nil
end
function CC.UpSuitWrap(cpair)
if CC.EitherProne(cpair) then return proneError end
if cpair[1].suit ~= cpair[2].suit then
return 'Cards must all be the same suit'
end
if cpair[1].ord == 13 and cpair[2].ord == 1 then
-- Ace on King
elseif cpair[1].ord == cpair[2].ord - 1 then
-- up, eg 3 on a 2
else
return 'Cards must go up in rank (Aces on Kings allowed)'
end
return nil
end
function CC.DownSuit(cpair)
if CC.EitherProne(cpair) then return proneError end
if cpair[1].suit ~= cpair[2].suit then
return 'Cards must be the same suit'
end
return CC.Down(cpair)
end
function CC.DownSuitTwo(cpair)
if CC.EitherProne(cpair) then return proneError end
if cpair[1].suit ~= cpair[2].suit then
return 'Cards must be the same suit'
end
if cpair[1].ord ~= cpair[2].ord + 2 then
return 'Cards must be down by two'
end
return nil
end
function CC.DownSuitWrap(cpair)
if CC.EitherProne(cpair) then return proneError end
if cpair[1].suit ~= cpair[2].suit then
return 'Cards must be the same suit'
end
if cpair[1].ord == 1 and cpair[2].ord == 13 then
-- King on Ace
elseif cpair[1].ord - 1 == cpair[2].ord then
-- down, eg 2 on a 3
else
return 'Cards must go down in rank (Kings on Aces allowed)'
end
return nil
end
function CC.DownColor(cpair)
if CC.EitherProne(cpair) then return proneError end
if cpair[1].twoColor ~= cpair[2].twoColor then
return 'Cards must be the same color'
end
return CC.Down(cpair)
end
function CC.DownColorWrap(cpair)
if CC.EitherProne(cpair) then return proneError end
if cpair[1].twoColor ~= cpair[2].twoColor then
return 'Cards must be the same color'
end
if cpair[1].ord == 1 and cpair[2].ord == 13 then
-- King on Ace
elseif cpair[1].ord ~= cpair[2].ord + 1 then
return 'Cards must go down in rank (Kings on Aces allowed)'
end
return nil
end
function CC.UpAltColor(cpair)
if CC.EitherProne(cpair) then return proneError end
if cpair[1].twoColor == cpair[2].twoColor then
return 'Cards must be in alternating colors'
end
return CC.Up(cpair)
end
function CC.DownAltColor(cpair)
if CC.EitherProne(cpair) then return proneError end
if cpair[1].twoColor == cpair[2].twoColor then
return 'Cards must be in alternating colors'
end
return CC.Down(cpair)
end
function CC.DownAltSuit(cpair)
if CC.EitherProne(cpair) then return proneError end
if cpair[1].suit == cpair[2].suit then
return 'Cards must have different suits'
end
return CC.Down(cpair)
end
function CC.DownWrap(cpair)
if CC.EitherProne(cpair) then return proneError end
if cpair[1].ord == 1 and cpair[2].ord == 13 then
-- King on Ace
elseif cpair[1].ord ~= cpair[2].ord + 1 then
return 'Cards must go down in rank (Kings on Aces allowed)'
end
return nil
end
function CC.DownAltColorWrap(cpair)
if CC.EitherProne(cpair) then return proneError end
if cpair[1].twoColor == cpair[2].twoColor then
return 'Cards must be in alternating colors'
end
if cpair[1].ord == 1 and cpair[2].ord == 13 then
-- King on Ace
elseif cpair[1].ord ~= cpair[2].ord + 1 then
return 'Cards must go down in rank (Kings on Aces allowed)'
end
return nil
end
function CC.Accordian(cpair)
local function getPileIdx(pile)
for i, p in ipairs(_G.BAIZE.piles) do
if p == pile then
return i
end
end
return -1
end
if not ((cpair[1].ord == cpair[2].ord) or (cpair[1].suit == cpair[2].suit)) then
return 'Cards must be the same rank or suit'
end
local p1x = getPileIdx(cpair[1].parent)
local p2x = getPileIdx(cpair[2].parent)
-- log.trace(p1x, p2x)
if (p1x + 1 == p2x) or (p1x + 3 == p2x) then
return nil
end
return 'A card can be moved on top of another card immediately to its left or three cards to its left'
end
function CC.Thirteen(cpair)
if CC.EitherProne(cpair) then return proneError end
local sum = cpair[1].ord + cpair[2].ord
if sum ~= 13 then
return string.format('The cards must add up to 13, not %d', sum)
end
return nil
end
return CC