-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbridge_hand_generator.py
153 lines (130 loc) · 4.57 KB
/
bridge_hand_generator.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Bridge Hand Generator
Created on Mon Oct 5 00:08:03 2020
@author: edchen
"""
import numpy as np
# create 52 card deck
suits = ['S','H','D','C']
nums = ['A','K','Q','J','T','9','8','7','6','5','4','3','2']
cards = []
for i in range(len(suits)):
for j in range(len(nums)):
cards.append(suits[i]+nums[j])
len(cards)
# deal one hand
deal = np.random.choice(cards, size=52, replace=False)
# sort cards for one hand in suit_rank order
def card_order(a):
shuffled = []
for i in range(len(cards)):
if cards[i] in set(a):
shuffled.append(cards[i])
return shuffled
# example: show one ordered hand
card_order(deal[0:13])
# sort dealt cards for one hand by suit
def display_hand(a):
S, H, D, C = [],[],[],[]
for i in range(len(a)):
if a[i][0] == 'S':
S.append(a[i][1])
elif a[i][0] == 'H':
H.append(a[i][1])
elif a[i][0] == 'D':
D.append(a[i][1])
elif a[i][0] == 'C':
C.append(a[i][1])
return [S,H,D,C]
# example: show suit sorted hand
display_hand(card_order(deal[0:13]))
# deal number of hands passed as n
def deal_hand(n):
# space variable for where to position East
def tab_check(a):
if len(East[a]) < 2:
return '\t\t\t\t\t\t\t\t'
elif len(East[a]) < 5:
return '\t\t\t\t\t\t\t'
elif len(East[a]) < 7:
return '\t\t\t\t\t\t'
elif len(East[a]) < 9:
return '\t\t\t\t\t'
elif len(East[a]) < 11:
return '\t\t\t\t'
else:
return '\t\t\t'
# choose dealer
def dealer():
return np.random.choice(['N','S','E','W'], size=1, replace=True)
# choose vulnerability
def vul():
return np.random.choice(['N/S','E/W','None','Both'], size=1, replace=True)
# if lists instantiated, store direction label as first item in list
try:
All_North.append('North')
All_South.append('South')
All_East.append('East')
All_West.append('West')
except:
pass
# deal hands
for i in range(n):
deal = np.random.choice(cards, size=52, replace=False)
North = display_hand(card_order(deal[0:13]))
South = display_hand(card_order(deal[13:26]))
East = display_hand(card_order(deal[26:39]))
West = display_hand(card_order(deal[39:52]))
# if lists instantiated, store dealt hands for each direction
try:
All_North.append(North)
All_South.append(South)
All_East.append(East)
All_West.append(West)
except:
pass
# format and print out each hand for all directions
dealer_choice = dealer()[0]
vul_choice = vul()[0]
try:
Hand_Dealer.append(dealer_choice)
Hand_Vul.append(vul_choice)
except:
pass
print(i+1,'. Dealer:',dealer_choice,' Vulnerable:',vul_choice,'\r\n')
print('\t\t\t\t','North','\r\n'
'\t\t\t\t','S: ',', '.join(North[0]),'\r\n'
'\t\t\t\t','H: ',', '.join(North[1]),'\r\n'
'\t\t\t\t','D: ',', '.join(North[2]),'\r\n'
'\t\t\t\t','C: ',', '.join(North[3]),'\r\n'
'East','\t\t\t\t\t\t\t\t','West','\r\n'
'S: ',', '.join(East[0]),tab_check(0),'S: ',', '.join(West[0]),'\r\n'
'H: ',', '.join(East[1]),tab_check(1),'H: ',', '.join(West[1]),'\r\n'
'D: ',', '.join(East[2]),tab_check(2),'D: ',', '.join(West[2]),'\r\n'
'C: ',', '.join(East[3]),tab_check(3),'C: ',', '.join(West[3]),'\r\n'
'\t\t\t\t','South','\r\n'
'\t\t\t\t','S: ',', '.join(South[0]),'\r\n'
'\t\t\t\t','H: ',', '.join(South[1]),'\r\n'
'\t\t\t\t','D: ',', '.join(South[2]),'\r\n'
'\t\t\t\t','C: ',', '.join(South[3]),'\r\n')
# example: deal 3 hands and extract hands in each direction
All_North = []
All_South = []
All_East = []
All_West = []
Hand_Dealer = []
Hand_Vul = []
deal_hand(3)
# print hands for one side when All_North, All_South, All_East, and All_West are defined
def print_one_side(a):
print('Hands for ',a[0],'\r\n')
for i in range(len(a)-1):
print(i+1,'. Dealer:',Hand_Dealer[i],' Vulnerable:',Hand_Vul[i])
print('S: ',', '.join(a[i+1][0]),'\r\n'
'H: ',', '.join(a[i+1][1]),'\r\n'
'D: ',', '.join(a[i+1][2]),'\r\n'
'C: ',', '.join(a[i+1][3]),'\r\n')
# example: print all the East hands dealt above
print_one_side(All_East)