扑克牌可以洗牌
扑克牌有花色和数字
四个人的版本
每个人13张牌
对牌可以实现自动排序(先按照花色,再按照数字)
import random
def gen_pocker(n):
"""生成初始扑克牌并洗牌"""
pocker=[i for i in range(n)]
for i in range(100):
p1=random.randint(0,n-1)
p2=random.randint(0,n-1)
pocker[p1],pocker[p2]= pocker[p2],pocker[p1]
return pocker
def getColor(x):
"""获取牌的花色"""
color=["♣ ","♦ ","♥ ","♠ "]
c = x//13
if c<0 or c>=4:
return "ERROR!"
return color[c]
def getValue(x):
"""获取牌的牌面大小"""
value=x % 13 + 1
if value==1:
return 'A'
elif value>=2 and value<=10:
return str(value)
elif value==11:
return 'J'
elif value==12:
return 'Q'
elif value==13:
return 'K'
def getPuk(x):
return getColor(x)+getValue(x)
pocker= gen_pocker(52)
(a,b,c,d)=([],[],[],[])
for x in range(13):
p=x*4
a.append(pocker[p])
b.append(pocker[p+1])
c.append(pocker[p+2])
d.append(pocker[p+3])
# 排序,先按"♣ ","♦ ","♥ ","♠ ",再按数字顺序
a=sorted(a)
b=sorted(b)
c=sorted(c)
d=sorted(d)
a=[getPuk(x) for x in a]
b=[getPuk(x) for x in b]
c=[getPuk(x) for x in c]
d=[getPuk(x) for x in d]
print("牌手1:" + ' '.join(a))
print("牌手2:" + ' '.join(b))
print("牌手3:" + ' '.join(c))
print("牌手4:" + ' '.join(d))