-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
388 lines (336 loc) · 13.2 KB
/
Copy pathmain.py
File metadata and controls
388 lines (336 loc) · 13.2 KB
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
import os
import discord
import random
import math
import numpy as np
from replit import db
from uptime import keep_alive
from discord.ext import commands
import asyncio
game = discord.Game("something, probably")
bot = commands.Bot(command_prefix = "&")
#------
#deck is a random list of 1-52
#hand is a list of lists of the form [suit,val,numval], i.e. a list of card params
hcp_dict = {"A":4,"K":3,"Q":2,"J":1}
def get_hcp(hand):
val = 0
for item in hand:
val+=max(item[2]-8,0)
return val
num_to_suit = {1:"Spade",2:"Heart",3:"Diamond",4:"Club"}
num_to_val = {12:"A",11:"K",10:"Q",9:"J",8:"T",7:"9",6:"8",5:"7",4:"6",3:"5",2:"4",1:"3",0:"2"}
sort_order = {b:a for a,b in num_to_val.items()}
class card():
def __init__(self,num):
suitnum = math.ceil(num/13)
self.suit = num_to_suit[suitnum]
self.val = num_to_val[num%13]
self.numval = num%13
#change list of numbers into a hand "object"
def hand_read(p):
hand = []
for item in p:
temp_card = card(item)
hand.append([temp_card.suit,temp_card.val,
temp_card.numval])
return hand
#sort the hand after returning a hand "object"
def hand_sort(hand):
s_list = []
h_list = []
d_list = []
c_list = []
suit_to_bins = {"Spade":s_list,"Heart":h_list,"Diamond":d_list,"Club":c_list}
#sort cards by suit
for suit,val,trash in hand:
suit_to_bins[suit].append(val)
hand_list = (s_list,h_list,d_list,c_list)
#sort within suits
for x in hand_list:
x.sort(key = lambda x:sort_order[x], reverse = True)
return hand_list
def deal(display = False):
deck = list(np.arange(1,53))
random.shuffle(deck)
hp1 = hand_read(deck[0:13])
hp2 = hand_read(deck[13:26])
hp3 = hand_read(deck[26:39])
hp4 = hand_read(deck[39:52])
p1 = hand_sort(hp1)
p2 = hand_sort(hp2)
p3 = hand_sort(hp3)
p4 = hand_sort(hp4)
hands_dealt = [p1,p2,p3,p4]
#suit distributions by hand
dist1 = [len(p1[0]),len(p1[1]),len(p1[2]),len(p1[3])]
dist2 = [len(p2[0]),len(p2[1]),len(p2[2]),len(p2[3])]
dist3 = [len(p3[0]),len(p3[1]),len(p3[2]),len(p3[3])]
dist4 = [len(p4[0]),len(p4[1]),len(p4[2]),len(p4[3])]
dist_list = [dist1,dist2,dist3,dist4]
hcp1 = get_hcp(hp1)
hcp2 = get_hcp(hp2)
hcp3 = get_hcp(hp3)
hcp4 = get_hcp(hp4)
hcp_list = [hcp1,hcp2,hcp3,hcp4]
if display:
mapping = {0:"N",1:"E",2:"S",3:"W"}
for i,x in enumerate(hands_dealt):
print(f"{mapping[i]}:\n")
dh(x)
return [hands_dealt,hcp_list,dist_list]
balanced = [[4,3,3,3],[5,3,3,2],[4,4,3,2]]
def m1():
hands_dealt,hcp_list,dist_list = deal()
#enum here is used to artificially map to the players
declarer = False
while not declarer:
for i,hcp in enumerate(hcp_list):
if 12<=hcp_list[i]<=21:
if dist_list[i][0] < 5 and dist_list[i][1]<5:
print(dist_list)
declarer = [hands_dealt[i],hcp_list[i],dist_list[i]]
n = (i+2)%4
partner = [hands_dealt[n],hcp_list[n],dist_list[n]]
print(declarer[1])
break
hands_dealt,hcp_list,dist_list = deal()
return declarer,partner
def NT1():
hands_dealt,hcp_list,dist_list = deal()
#enum here is used to artificially map to the players
declarer = False
while not declarer:
for i,hcp in enumerate(hcp_list):
if 15<=hcp_list[i]<=17:
if sorted(dist_list[i], reverse = True) in balanced:
declarer = [hands_dealt[i],hcp_list[i],dist_list[i]]
n = (i+2)%4
partner = [hands_dealt[n],hcp_list[n],dist_list[n]]
print(declarer[1])
break
hands_dealt,hcp_list,dist_list = deal()
return declarer,partner
def M1():
hands_dealt,hcp_list,dist_list = deal()
#enum here is used to artificially map to the players
declarer = False
while not declarer:
for i,hcp in enumerate(hcp_list):
if 12<=hcp_list[i]<=21:
if dist_list[i][0] >= 5 or dist_list[i][1]>=5:
print(dist_list)
declarer = [hands_dealt[i],hcp_list[i],dist_list[i]]
n = (i+2)%4
partner = [hands_dealt[n],hcp_list[n],dist_list[n]]
print(declarer[1])
break
hands_dealt,hcp_list,dist_list = deal()
return declarer,partner
###DUE TO THE LINEAR NATURE OF THE SYSTEM, IT IS VERY SLIGHTLY MORE LIKELY TO GENERATE SPADE FIT HANDS
def M1ns(): #one major, no support
hands_dealt,hcp_list,dist_list = deal()
#enum here is used to artificially map to the players
declarer = False
while not declarer:
for i,hcp in enumerate(hcp_list):
if 12<=hcp_list[i]<=21:
if dist_list[i][0] >= 5 or dist_list[i][1]>=5:
s_long = (dist_list[i][0]>=5)
h_long = (dist_list[i][1]>=5)
##check for spade fit
for player,dist in enumerate(dist_list):
if player !=i and s_long:
if dist_list[player][0]<=2:
declarer = [hands_dealt[i],hcp_list[i],dist_list[i]]
partner = [hands_dealt[player],hcp_list[player],dist_list[player]]
print(declarer[1])
break
##check for heart fit
print(dist_list)
for player,dist in enumerate(dist_list):
if player !=i and h_long:
if dist_list[player][1]<=2:
declarer = [hands_dealt[i],hcp_list[i],dist_list[i]]
partner = [hands_dealt[player],hcp_list[player],dist_list[player]]
print(declarer[1])
break
hands_dealt,hcp_list,dist_list = deal()
return declarer,partner
def M1ts(): #one major, three support
hands_dealt,hcp_list,dist_list = deal()
#enum here is used to artificially map to the players
declarer = False
while not declarer:
for i,hcp in enumerate(hcp_list):
if 12<=hcp_list[i]<=21:
if dist_list[i][0] >= 5 or dist_list[i][1]>=5:
s_long = (dist_list[i][0]>=5)
h_long = (dist_list[i][1]>=5)
##check for spade fit
for player,dist in enumerate(dist_list):
if player !=i and s_long:
if dist_list[player][0]==3:
declarer = [hands_dealt[i],hcp_list[i],dist_list[i]]
partner = [hands_dealt[player],hcp_list[player],dist_list[player]]
print(declarer[1])
break
##check for heart fit
print(dist_list)
for player,dist in enumerate(dist_list):
if player !=i and h_long:
if dist_list[player][1]==3:
declarer = [hands_dealt[i],hcp_list[i],dist_list[i]]
partner = [hands_dealt[player],hcp_list[player],dist_list[player]]
print(declarer[1])
break
hands_dealt,hcp_list,dist_list = deal()
return declarer,partner
def M1fs(): #one major, four support
hands_dealt,hcp_list,dist_list = deal()
#enum here is used to artificially map to the players
declarer = False
while not declarer:
for i,hcp in enumerate(hcp_list):
if 12<=hcp_list[i]<=21:
if dist_list[i][0] >= 5 or dist_list[i][1]>=5:
s_long = (dist_list[i][0]>=5)
h_long = (dist_list[i][1]>=5)
##check for spade fit
for player,dist in enumerate(dist_list):
if player !=i and s_long:
if dist_list[player][0]==4:
declarer = [hands_dealt[i],hcp_list[i],dist_list[i]]
partner = [hands_dealt[player],hcp_list[player],dist_list[player]]
print(declarer[1])
break
##check for heart fit
print(dist_list)
for player,dist in enumerate(dist_list):
if player !=i and h_long:
if dist_list[player][1]==4:
declarer = [hands_dealt[i],hcp_list[i],dist_list[i]]
partner = [hands_dealt[player],hcp_list[player],dist_list[player]]
print(declarer[1])
break
hands_dealt,hcp_list,dist_list = deal()
return declarer,partner
def M1Fs(): #one major, five support
hands_dealt,hcp_list,dist_list = deal()
#enum here is used to artificially map to the players
declarer = False
while not declarer:
for i,hcp in enumerate(hcp_list):
if 12<=hcp_list[i]<=21:
if dist_list[i][0] >= 5 or dist_list[i][1]>=5:
s_long = (dist_list[i][0]>=5)
h_long = (dist_list[i][1]>=5)
##check for spade fit
for player,dist in enumerate(dist_list):
if player !=i and s_long:
if dist_list[player][0]>=5:
declarer = [hands_dealt[i],hcp_list[i],dist_list[i]]
partner = [hands_dealt[player],hcp_list[player],dist_list[player]]
print(declarer[1])
break
##check for heart fit
print(dist_list)
for player,dist in enumerate(dist_list):
if player !=i and h_long:
if dist_list[player][1]>=5:
declarer = [hands_dealt[i],hcp_list[i],dist_list[i]]
partner = [hands_dealt[player],hcp_list[player],dist_list[player]]
print(declarer[1])
break
hands_dealt,hcp_list,dist_list = deal()
return declarer,partner
def dh(hand): #display hand
flat_h = []
for x in hand:
flat = ""
for y in x:
flat+=y
flat_h.append(flat)
# h_content = (f"S: {flat_h[0]}\nH: {flat_h[1]}\nD: {flat_h[2]}\nC: {flat_h[3]}")
h_content=""
pad = random.randint(13,20)
form = "{0: <"+str(pad)+"}"
h_content+=form.format(f"S: {flat_h[0]}")
h_content+="\n"
pad = random.randint(13,20)
form = "{0: <"+str(pad)+"}"
h_content+=form.format(f"H: {flat_h[1]}")
h_content+="\n"
pad = random.randint(13,20)
form = "{0: <"+str(pad)+"}"
h_content+=form.format(f"D: {flat_h[2]}")
h_content+="\n"
pad = random.randint(13,20)
form = "{0: <"+str(pad)+"}"
h_content+=form.format(f"C: {flat_h[3]}")
h_content+="\n"
print(h_content)
return h_content
#-------
#opening log
@bot.event
async def on_connect():
print('Successfully Logged In')
@bot.event
async def on_ready():
print('Bot ready')
await bot.change_presence(status = discord.Status.online, activity = game)
@bot.command(name = "deal", aliases = ['d'])
async def dealcom(ctx, opener = "1NT"):
print('deal command detected')
if opener =="1NT":
print("Generating 1NT hand")
d,p = NT1()
dec = dh(d[0])
part = dh(p[0])
message=f"\nDeclarer Hand:\n||{dec}||\n\nPartner Hand:\n||{part}||"
await ctx.send(message)
elif opener == "1M":
print("Generating 1M hand")
d,p = M1()
dec = dh(d[0])
part = dh(p[0])
message=f"\nDeclarer Hand:\n||{dec}||\n\nPartner Hand:\n||{part}||"
await ctx.send(message)
elif opener == "1Mns":
print("Generating 1M hand (no support opposite)")
d,p = M1ns()
dec = dh(d[0])
part = dh(p[0])
message=f"\nDeclarer Hand:\n||{dec}||\n\nPartner Hand:\n||{part}||"
await ctx.send(message)
elif opener == "1M3s":
print("Generating 1M hand (three support opposite)")
d,p = M1ts()
dec = dh(d[0])
part = dh(p[0])
message=f"\nDeclarer Hand:\n||{dec}||\n\nPartner Hand:\n||{part}||"
await ctx.send(message)
elif opener == "1M4s":
print("Generating 1M hand (four support opposite)")
d,p = M1fs()
dec = dh(d[0])
part = dh(p[0])
message=f"\nDeclarer Hand:\n||{dec}||\n\nPartner Hand:\n||{part}||"
await ctx.send(message)
elif opener == "1M5s":
print("Generating 1M hand (five support opposite)")
d,p = M1Fs()
dec = dh(d[0])
part = dh(p[0])
message=f"\nDeclarer Hand:\n||{dec}||\n\nPartner Hand:\n||{part}||"
await ctx.send(message)
elif opener == "1m":
print("Generating 1m hand...")
d,p = m1()
dec = dh(d[0])
part = dh(p[0])
message=f"\nDeclarer Hand:\n||{dec}||\n\nPartner Hand:\n||{part}||"
await ctx.send(message)
keep_alive()
bot.run(os.environ['token'])