-
Notifications
You must be signed in to change notification settings - Fork 0
/
tron.py
579 lines (498 loc) · 19.9 KB
/
tron.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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
import random
import sys
from progress.bar import Bar
class Tron:
'''
Based on the work done by: /u/Azgurath & /u/mpaw975
'''
def __init__(self):
self.draw = True
self.loops = 100000
# report metrics
self.turn_three_tron = 0
self.turn_three_karn = 0
self.total_have_karn = 0
self.total_turns = 0
self.failed_to_tron = 0
self.failed_starting_size = 0
self.success_starting_size = 0
self.turn_four_tron = 0
# experimental settings
# for testing optimal behaviour
self.scry_for_second_piece = False
self.map_for_second_piece = False
def game(self):
'''
main logic for how the games are played out
'''
self.populate_deck()
# Populate field
field = []
# Populate cards on bottom
bottom = []
# Populate starting hand
mulligans = 0
decided_on_hand = False
hand = random.sample(self.deck, 7)
# Mulligan
# Keep if guarunteed tron
# Keep if 6 cards or less and 2 or more tron pieces
# Keep if 4 or less cards and 1 or more tron pieces
while not decided_on_hand and mulligans < 7:
# natural tron
if all(x in hand for x in ['Mine', 'Tower', 'PP']):
decided_on_hand = True
# guarunteed tron
elif (self.troncheck(hand) >= 2 and (
('map' in hand) or
('scry' in hand and 'star' in hand))):
decided_on_hand = True
# 2 pieces
if mulligans >= 2 and not decided_on_hand:
if self.troncheck(hand) >= 2:
decided_on_hand = True
if any(x in hand for x in ('Tower', 'Mine', 'PP')):
if 'map' in hand or 'scry' in hand:
decided_on_hand = True
elif all(x in hand for x in ('stir', 'star')):
decided_on_hand = True
# 1 piece
if mulligans >= 3 and not decided_on_hand:
if any(x in hand for x in ('Tower', 'Mine', 'PP')):
decided_on_hand = True
# Mull to 3
if mulligans >= 4 and not decided_on_hand:
decided_on_hand = True
# Mulligan
if not decided_on_hand:
mulligans += 1
hand = random.sample(self.deck, 7)
# Take the cards in hand from the deck
for card in hand:
self.deck.remove(card)
# Put mulligans back
target_hand_size = 7 - mulligans
saved = []
guaranteed_tron = False
while target_hand_size < 7 and len(saved) < target_hand_size:
if self.troncheck(saved) == 3:
guaranteed_tron = True
if not guaranteed_tron:
if 'Tower' in hand and 'Tower' not in saved:
saved.append('Tower')
hand.remove('Tower')
continue
if 'Mine' in hand and 'Mine' not in saved:
saved.append('Mine')
hand.remove('Mine')
continue
if 'PP' in hand and 'PP' not in saved:
saved.append('PP')
hand.remove('PP')
continue
if self.troncheck(saved) >= 2:
if 'map' in hand and 'map' not in saved:
saved.append('map')
hand.remove('map')
guaranteed_tron = True
continue
if 'scry' in hand and 'star' in hand and len(saved) >= target_hand_size + 2:
saved.append('scry')
hand.remove('scry')
saved.append('star')
hand.remove('star')
guaranteed_tron = True
continue
if 'stir' in hand and 'star' in hand and len(saved) >= target_hand_size + 2:
saved.append('stir')
hand.remove('stir')
saved.append('star')
hand.remove('star')
continue
if 'scry' in hand and 'forest' in hand and len(saved) >= target_hand_size + 2:
saved.append('scry')
hand.remove('scry')
saved.append('forest')
hand.remove('forest')
guaranteed_tron = True
continue
if 'stir' in hand and 'forest' in hand and len(saved) >= target_hand_size + 2:
saved.append('stir')
hand.remove('stir')
saved.append('forest')
hand.remove('forest')
continue
if 'map' in hand:
saved.append('map')
hand.remove('map')
continue
if 'star' in hand:
saved.append('star')
hand.remove('star')
continue
if 'scry' in hand:
saved.append('scry')
hand.remove('scry')
continue
if 'stir' in hand:
saved.append('stir')
hand.remove('stir')
continue
if 'forest' in hand:
saved.append('forest')
hand.remove('forest')
continue
if 'gq' in hand:
saved.append('gq')
hand.remove('gq')
continue
else:
if 'karn' in hand:
saved.append('karn')
hand.remove('karn')
continue
if 'star' in hand:
saved.append('star')
hand.remove('star')
continue
card = hand[0]
hand.remove(card)
saved.append(card)
if mulligans > 0:
bottom += [x for x in hand]
hand = [x for x in saved]
# Main loop for turns!
turn = 0
tron = False
while not tron:
# increment turn counter
turn += 1
# initialize mana to 0
mana = 0
green = 0
lands_played = 0
# otherwise
if self.draw or turn != 1:
new_card = random.choice(self.deck)
hand.append(new_card)
self.deck.remove(new_card)
# tap lands for mana
for card in field:
if card in ('Mine', 'PP', 'Tower', 'gq', 'forest'):
if card == 'forest':
green += 1
mana += 1
# on second turn, crack stars before playing land
# in order to see if we find more pieces
if turn == 2 and 'star' in field and mana > 0:
field.remove('star')
new_card = random.choice(self.deck)
self.deck.remove(new_card)
hand.append(new_card)
green += 1
# play a new tron land
for card in hand:
if card in ('Mine', 'PP', 'Tower') and (
card not in field) and lands_played == 0:
hand.remove(card)
field.append(card)
mana += 1
lands_played = 1
# check if we have tron
if 'PP' in field and 'Tower' in field and 'Mine' in field:
tron = True
# crack stars for green
if 'star' in field:
field.remove('star')
new_card = random.choice(self.deck)
self.deck.remove(new_card)
hand.append(new_card)
green += 1
# if two tron pieces, use map or scrying
if self.troncheck(hand, field) >= 2:
# cast and use map if none is in field and you have enough mana
if 'map' in hand and 'map' not in field and mana > 2:
hand.remove('map')
field.append('map')
mana -= 1
# use map
if 'map' in field and mana > 1:
self.use_map(hand, field)
mana -= 2
# cast scrying
if 'scry' in hand and mana > 1 and green > 0:
self.use_scry(hand, field)
mana -= 2
green -= 1
# cast map
if 'map' in hand and mana > 0:
hand.remove('map')
field.append('map')
mana -= 1
did_something = True
while mana > 0 and did_something:
did_something = False
search_effects = 0
for card in hand:
if card in ('scry', 'map'):
search_effects += 1
# if mana, cast stirrings
if green > 0 and mana > 0 and 'stir' in hand:
# use stirrings
green -= 1
mana -= 1
self.use_stir(hand, field)
did_something = True
continue
# test to see if you should fetch second piece
# w/o a plan for the third
if self.scry_for_second_piece or search_effects > 1:
if green > 0 and mana > 1 and 'scry' in hand:
self.use_scry(hand, field)
mana -= 2
green -= 1
did_something = True
continue
if search_effects > 1:
if mana > 1 and 'map' in field:
self.use_map(hand, field)
mana -= 2
did_something = True
if mana > 0 and 'map' in hand:
hand.remove('map')
field.append('map')
mana -= 1
did_something = True
continue
# cast star
if 'star' in hand and mana > 0:
hand.remove('star')
field.append('star')
mana -= 1
did_something = True
continue
# crack star
if 'star' in field and mana > 0:
field.remove('star')
green += 1
new_card = random.choice(self.deck)
self.deck.remove(new_card)
hand.append(new_card)
did_something = True
continue
# map for second piece as a last resort
# if nothing else yielded results
if self.map_for_second_piece or search_effects > 1:
if mana > 1 and 'map' in field:
self.use_map(hand, field)
mana -= 2
did_something = True
if mana > 0 and 'map' in hand:
hand.remove('map')
field.append('map')
mana -= 1
did_something = True
continue
# play a new tron land
if lands_played == 0:
for card in hand:
if card in ('Mine', 'PP', 'Tower') and card not in field:
hand.remove(card)
field.append(card)
mana += 1
lands_played = 1
if self.troncheck(hand, field) >= 2:
if 'map' in field and mana > 1:
self.use_map(hand, field)
mana -= 2
if 'scry' in hand and green > 0 and mana > 1:
self.use_scry(hand, field)
green -= 1
mana -= 2
if 'map' in hand and mana > 0:
hand.remove('map')
field.append('map')
mana -= 1
if lands_played == 0:
if 'forest' in hand:
hand.remove('forest')
field.append('forest')
mana += 1
green += 1
lands_played = 1
if 'gq' in hand:
hand.remove('gq')
field.append('gq')
mana += 1
lands_played = 1
if 'stir' in hand and green > 0 and mana > 0:
self.use_stir(hand, field)
green -= 1
mana -= 1
if 'star' in hand and mana > 0:
hand.remove('star')
field.append('star')
mana -= 1
# end turn
if turn == 3:
self.turn_three_tron += 1
elif turn == 4:
self.turn_four_tron += 1
if turn == 3 and 'Karn' in hand:
self.turn_three_karn += 1
if 'Karn' in hand:
self.total_have_karn += 1
if turn < 10:
self.total_turns += turn
self.success_starting_size += target_hand_size
else:
self.failed_to_tron += 1
self.failed_starting_size += target_hand_size
def report(self):
avg_turns = self.total_turns / float(self.loops - self.failed_to_tron)
failed_avg_size = self.failed_starting_size / float(self.failed_to_tron)
avg_size = self.success_starting_size / float(self.loops - self.failed_to_tron)
turn_four_tron = self.turn_four_tron / float(self.loops) * 100
tot_t4_tron = (self.turn_three_tron + self.turn_four_tron) / float(self.loops) * 100
t3_karn = self.turn_three_karn / float(self.loops) * 100
have_karn = self.total_have_karn / float(self.loops) * 100
tot_failed = self.failed_to_tron / float(self.loops) * 100
self.output('\nOn the play:' if not self.draw else '\nOn the draw')
self.output('-' * 70)
self.output('Turn three tron: {:.2f}%'.format(
self.turn_three_tron / float(self.loops) * 100))
self.output('Turn four tron: {:.2f}% (tot incl t3: {:.2f}%)'.format(
turn_four_tron, tot_t4_tron))
self.output('Turn three Karn: {:.2f}%'.format(t3_karn))
self.output('Average turn tron: {:.2f}'.format(avg_turns))
self.output('\nHave Karn when tron is done: {:.2f}%'.format(have_karn))
self.output('Failed to get tron by tun 10: {:.2f}%'.format(tot_failed))
self.output('Average starting hand size: {:.2f}'.format(avg_size))
self.output('Average starting hand size when ' +
'failed to get tron: {:.2f}'.format(failed_avg_size))
self.output('\nGames played: {}'.format(self.loops))
self.output('-' * 70)
@staticmethod
def output(*message):
op = ' '.join(message)
sys.stdout.write(f'{op}\n')
def populate_deck(self):
self.deck = [
'Mine', 'Mine', 'Mine', 'Mine',
'PP', 'PP', 'PP', 'PP',
'Tower', 'Tower', 'Tower', 'Tower',
'star', 'star', 'star', 'star',
'star', 'star', 'star', 'star',
'map', 'map', 'map', 'map',
'scry', 'scry', 'scry', 'scry',
'stir', 'stir', 'stir', 'stir',
'forest', 'forest', 'forest',
'forest', 'forest', 'gq', 'gq',
'Karn', 'Karn', 'Karn', 'Karn'
]
for x in range(60 - len(self.deck)):
self.deck.append('dead')
self.bottom = []
def settings(self, args):
if 'test' in args:
self.scry_for_second_piece = True
self.map_for_second_piece = True
if 'otp' in args:
self.draw = False
for arg in args:
try:
loops = int(arg)
self.loops = loops
except Exception:
pass
@staticmethod
def troncheck(listA=[], listB=[]):
'''
used to check hand, field or hand + field for tron pieces
'''
tp = 0
for x in ('Mine', 'Tower', 'PP'):
tp += 1 if x in listA + listB else 0
return tp
def use_map(self, hand, field):
field.remove('map')
self.deck.extend(self.bottom)
self.bottom = []
for card in ('Tower', 'Mine', 'PP'):
if card not in hand and card not in field:
try:
self.deck.remove(card)
except ValueError:
print('Hand: {}'.format(hand))
print('Field: {}'.format(field))
print('Deck: {}'.format(self.deck))
hand.append(card)
def use_scry(self, hand, field):
hand.remove('scry')
self.deck.extend(self.bottom)
self.bottom = []
for card in ('Tower', 'Mine', 'PP'):
if card not in hand and card not in field:
try:
self.deck.remove(card)
except Exception:
print('Oops...')
hand.append(card)
def use_stir(self, hand, field):
hand.remove('stir')
cards = random.sample(self.deck, 5)
card_chosen = ''
# Look for if we have guraunteed tron
if self.troncheck(hand, field) >= 2:
if(('map' in hand or 'map' in field) or
(('star' in hand or 'star' in field) and
('scry' in hand))):
# If so, take Karn or stars to dig.
if 'Karn' in cards and card_chosen == '':
card_chosen = 'Karn'
if 'star' in cards and card_chosen == '':
card_chosen = 'star'
if 'forest' in cards and card_chosen == '':
card_chosen = 'forest'
# If we don't have tron, get things to help us get it
# Get new tron piece
if self.troncheck(hand, field) != 3:
for card in cards:
if card in ('Tower', 'Mine', 'PP'):
if card not in field and (
card not in hand) and card_chosen == '':
card_chosen = card
else:
# Get map if we have two pieces in hand
if self.troncheck(hand, field) >= 2 and (
'map' in cards) and card_chosen == '':
card_chosen = 'map'
# Get a star
if 'star' in cards and card_chosen == '':
card_chosen = 'star'
# Get a forest
if 'forest' in cards and card_chosen == '':
card_chosen = 'forest'
# Get any other land
for card in cards:
if card in {'Tower', 'Mine', 'PP', 'gq'} and (
card_chosen == ''):
card_chosen = card
# Add chosen card to hand, remove other five from the deck.
# Ideally these cards will be stored in a list and re-appended
# to the deck when a map or scrying is used.
if card_chosen != '':
hand.append(card_chosen)
for card in cards:
self.deck.remove(card)
self.bottom.append(card)
def main(args):
T = Tron()
T.settings(args)
with Bar('Assembling Tron', max=T.loops) as bar:
for i in range(T.loops):
T.game()
bar.next()
T.report()
if __name__ == '__main__':
main(sys.argv)