-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmissions.py
440 lines (354 loc) · 13.4 KB
/
missions.py
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
import random
from colors import Color
def is_even(v):
return v % 2 == 0
def sum_color(color, cards):
sum = 0
for card in cards:
if card.color == color:
sum += card.number
return sum
def sum_all(cards):
sum = 0
for card in cards:
sum += card.number
return sum
def get_positions(color, cards):
arr = []
for index, card in enumerate(cards):
if card.color == color:
arr.append(index)
return arr
def get_values(cards):
values = []
for card in cards:
values.append(card.number)
return list(set(values))
def get_colors(cards):
colors = []
for card in cards:
colors.append(card.color)
return list(set(colors))
class MissionDeck:
def __init__(self):
self.allmissions = create_missions()
id = 0
for m in self.allmissions:
m.id = id
id += 1
random.shuffle(self.allmissions)
def get_missions(self, n):
return self.allmissions[0:n]
def total_mission_count(self):
return len(self.allmissions)
class MissionFactory:
@classmethod
def create_sum_mission(cls):
class Mission:
def __init__(self, color, amount):
self.color = color
self.amount = amount
def test(self, cards):
return sum_color(self.color, cards) == self.amount
def __repr__(self):
return f"Sum of {self.color} is {self.amount}"
return Mission
@classmethod
def create_total_mission(cls):
class Mission:
def __init__(self, amount):
self.amount = amount
def test(self, cards):
return sum_all(cards) == self.amount
def __repr__(self):
return f"Sum of all is {self.amount}"
return Mission
@classmethod
def create_halfsum_mission(cls):
class Mission:
def __init__(self, color1, color2):
self.color1 = color1
self.color2 = color2
def test(self, cards):
sum1 = sum_color(self.color1, cards)
sum2 = sum_color(self.color2, cards)
if sum1 * sum2 == 0:
return False
return sum1 == sum2 / 2
def __repr__(self):
return f"Sum of {self.color1} is half the sum of {self.color2}"
return Mission
@classmethod
def create_equalsum_mission(cls):
class Mission:
def __init__(self, color1, color2):
self.color1 = color1
self.color2 = color2
def test(self, cards):
sum1 = sum_color(self.color1, cards)
sum2 = sum_color(self.color2, cards)
if sum1 * sum2 == 0:
return False
return sum1 == sum2
def __repr__(self):
return f"Sum of {self.color1} is the sum of {self.color2}"
return Mission
@classmethod
def create_two_adjacent_mission(cls):
class Mission:
def __init__(self, color):
self.color = color
def test(self, cards):
positions = get_positions(self.color, cards)
if len(positions) != 2:
return False
pos1 = positions[0]
pos2 = positions[1]
if abs(pos1 - pos2) == 1:
return True
return False
def __repr__(self):
return f"Two {self.color} next to eachother"
return Mission
@classmethod
def create_two_non_adjacent_mission(cls):
class Mission:
def __init__(self, color):
self.color = color
def test(self, cards):
positions = get_positions(self.color, cards)
if len(positions) != 2:
return False
pos1 = positions[0]
pos2 = positions[1]
if abs(pos1 - pos2) > 1:
return True
return False
def __repr__(self):
return f"Two {self.color} not next to eachother"
return Mission
@classmethod
def create_two_colors_mission(cls):
class Mission:
def __init__(self, color1, color2):
self.color1 = color1
self.color2 = color2
def test(self, cards):
positions1 = get_positions(self.color1, cards)
positions2 = get_positions(self.color2, cards)
return len(positions1) + len(positions2) == 4
def __repr__(self):
return f"All cards are {self.color1} or {self.color2}"
return Mission
@classmethod
def create_one_and_three_or_two_and_four_mission(cls):
class Mission:
def __init__(self, color):
self.color = color
def test(self, cards):
positions = get_positions(self.color, cards)
if len(positions) != 2:
return False
if positions == [1, 3] or positions == [2, 4]:
return True
return False
def __repr__(self):
return f"Only 1 & 3 or 2 & 4 are {self.color}"
return Mission
@classmethod
def create_three_of_color_mission(cls):
class Mission:
def __init__(self, color):
self.color = color
def test(self, cards):
positions = get_positions(self.color, cards)
if len(positions) == 3:
return True
return False
def __repr__(self):
return f"Three {self.color} cards"
return Mission
@classmethod
def create_more_or_less_than_four_mission(cls):
class Mission:
def __init__(self, sign):
self.sign = sign # 0 = <, 1 = >
def test(self, cards):
for card in cards:
if self.sign == 0:
if not card.number < 4:
return False
if self.sign == 1:
if not card.number > 4:
return False
return True
def __repr__(self):
if self.sign == 0:
return "All numbers are less than 4"
return "All numbers are greater than 4"
return Mission
@classmethod
def create_sameness_mission(cls):
class Mission:
def __init__(self, mode):
self.mode = mode
def test(self, cards):
if self.mode == 0:
values = get_values(cards)
return len(values) == 1
if self.mode == 1:
values = get_values(cards)
return len(values) == 4
if self.mode == 2:
values = get_values(cards)
colors = get_colors(cards)
return len(values) == 4 and len(colors) == 4
if self.mode == 3:
colors = get_colors(cards)
return len(colors) == 4
def __repr__(self):
if self.mode == 0:
return "All cards have same number"
if self.mode == 1:
return "All cards have different numbers"
if self.mode == 2:
return "All cards have different numbers and colors"
if self.mode == 3:
return "All cards have different colors"
return Mission
@classmethod
def create_oddness_mission(cls):
class Mission:
def __init__(self, mode):
self.mode = mode
def test(self, cards):
if self.mode == 0:
if is_even(cards[0].number) and is_even(cards[2].number):
if not is_even(cards[1].number) and not is_even(
cards[3].number
):
return True
if not is_even(cards[0].number) and not is_even(cards[2].number):
if is_even(cards[1].number) and is_even(cards[3].number):
return True
return False
else:
for card in cards:
if self.mode == 1:
if not is_even(card.number):
return False
if self.mode == 2:
if is_even(card.number):
return False
return True
def __repr__(self):
if self.mode == 0:
return "Alternating even and uneven or vice versa"
if self.mode == 1:
return "All numbers are even"
if self.mode == 2:
return "All numbers are odd"
return Mission
@classmethod
def create_sequence_mission(cls):
class Mission:
def __init__(self, mode):
self.mode = mode
def test(self, cards):
if self.mode == 0:
if (
cards[0].number == cards[1].number - 1
and cards[1].number == cards[2].number - 1
):
return True
if (
cards[1].number == cards[2].number - 1
and cards[2].number == cards[3].number - 1
):
return True
return False
if self.mode == 1:
values = get_values(cards)
values.sort()
if len(values) != 4:
return False
if values[0] == values[3] - 3:
return True
return False
def __repr__(self):
if self.mode == 0:
return "Three ascending numbers"
if self.mode == 1:
return "Four ascending numbers in random order"
return Mission
def create_missions():
missions = []
factory = MissionFactory.create_sum_mission()
missions.append(factory(Color.Pink, 10))
missions.append(factory(Color.Green, 6))
missions.append(factory(Color.Orange, 11))
missions.append(factory(Color.Orange, 9))
missions.append(factory(Color.Orange, 2))
missions.append(factory(Color.Purple, 3))
missions.append(factory(Color.Pink, 6))
missions.append(factory(Color.Pink, 4))
missions.append(factory(Color.Purple, 9))
missions.append(factory(Color.Green, 7))
missions.append(factory(Color.Green, 2))
factory = MissionFactory.create_total_mission()
missions.append(factory(10))
missions.append(factory(15))
missions.append(factory(18))
missions.append(factory(20))
factory = MissionFactory.create_halfsum_mission()
missions.append(factory(Color.Pink, Color.Purple))
missions.append(factory(Color.Green, Color.Orange))
missions.append(factory(Color.Purple, Color.Green))
missions.append(factory(Color.Orange, Color.Pink))
factory = MissionFactory.create_equalsum_mission()
missions.append(factory(Color.Pink, Color.Orange))
missions.append(factory(Color.Orange, Color.Green))
missions.append(factory(Color.Purple, Color.Pink))
missions.append(factory(Color.Green, Color.Purple))
factory = MissionFactory.create_two_adjacent_mission()
missions.append(factory(Color.Pink))
missions.append(factory(Color.Orange))
missions.append(factory(Color.Purple))
missions.append(factory(Color.Green))
factory = MissionFactory.create_two_non_adjacent_mission()
missions.append(factory(Color.Pink))
missions.append(factory(Color.Orange))
missions.append(factory(Color.Purple))
missions.append(factory(Color.Green))
factory = MissionFactory.create_two_colors_mission()
missions.append(factory(Color.Green, Color.Pink))
missions.append(factory(Color.Pink, Color.Purple))
missions.append(factory(Color.Purple, Color.Orange))
missions.append(factory(Color.Green, Color.Orange))
factory = MissionFactory.create_one_and_three_or_two_and_four_mission()
missions.append(factory(Color.Pink))
missions.append(factory(Color.Green))
missions.append(factory(Color.Orange))
missions.append(factory(Color.Purple))
factory = MissionFactory.create_three_of_color_mission()
missions.append(factory(Color.Pink))
missions.append(factory(Color.Green))
missions.append(factory(Color.Orange))
missions.append(factory(Color.Purple))
factory = MissionFactory.create_more_or_less_than_four_mission()
missions.append(factory(0))
missions.append(factory(1))
factory = MissionFactory.create_sameness_mission()
missions.append(factory(0))
missions.append(factory(1))
missions.append(factory(2))
missions.append(factory(3))
factory = MissionFactory.create_oddness_mission()
missions.append(factory(0))
missions.append(factory(1))
missions.append(factory(2))
factory = MissionFactory.create_sequence_mission()
missions.append(factory(0))
missions.append(factory(1))
assert len(missions) == 54
return missions