-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgen_double_dummy.py
44 lines (31 loc) · 1.2 KB
/
gen_double_dummy.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
import sys
from redeal import Deal
def write_deal(x):
print(str(deal).strip().replace(' ', '\t'))
nt_tot_tricks, nt_lead_tricks = get_double_dummy_tricks(deal, strain='N', lead='W')
print(format_dd_tricks(nt_tot_tricks, nt_lead_tricks))
spade_tot_tricks, spade_lead_tricks = get_double_dummy_tricks(deal, strain='S', lead='W')
print(format_dd_tricks(spade_tot_tricks, spade_lead_tricks))
def get_double_dummy_tricks(deal, strain, lead):
lead_tricks = sorted(list(deal.dd_all_tricks(strain, lead).items()), reverse=True)
result = []
min_tricks = 13
for card, tricks in lead_tricks:
declarer_tricks = 13 - tricks
if declarer_tricks < min_tricks:
min_tricks = declarer_tricks
result.append((str(card).strip(), declarer_tricks))
return min_tricks, result
def format_dd_tricks(tot_tricks, lead_tricks):
return ' '.join([str(tot_tricks)] + ['%s %d' % (card, tricks) for card, tricks in lead_tricks])
def generate(n):
dealer = Deal.prepare({})
i = 0
while i < n:
deal = dealer()
yield deal
i += 1
if __name__ == '__main__':
n = int(sys.argv[1])
for deal in generate(n):
write_deal(deal)