一只倔强的笨猪

【Python】基础-发牌程序

扑克牌发牌程序,4个牌手,计算机将52张牌发给4名牌手。打印出控制台。
import random
n=52
# 交换牌的顺序
def ge_pocker(n):
    x = 100
    while(x>0):
        x = x-1
        p1 = random.randint(0,n-1)
        p2 = random.randint(0,n-1)
        t = pocker[p1]
        pocker[p1] = pocker[p2]
        pocker[p2] = t
    return pocker
# 获取牌的花色
def getColor(x):
    color = ["草花","方块","红桃","黑桃"]
    c = int(x/13)
    if c<0 or c>=4:
        return "ERROR!"
    return color
#获取牌的字母和数字
def getValue(x):
    value = x % 13
    if value==0:
        return 'A'
    elif value >=1 and value<=9:
        return str(value+1)
    elif value == 10:
        return 'J'
    elif value == 11:
        return 'Q'
    elif value ==12:
        return 'K'
#把花色和字母数字拼接起来
def getPuk(x):
    return getColor(x) + getValue(x)
#主程序
(a,b,c,d) = ([],[],[],[])
pocker = [i for i in range(n)]
pocker = ge_pocker(n)
print(pocker)
for x in range(13):
    m = x * 4
    a.append(getPuk(pocker[m]))
    b.append(getPuk(pocker[m+1]))
    c.append(getPuk(pocker[m+2]))
    d.append(getPuk(pocker[m+3]))
a.sort()
b.sort()
c.sort()
d.sort()
print("牌手1",end=":")
for x in a:
    print(x,end=" ")
print("\n牌手2",end=":")
for x in b:
    print(x,end=" ")
print("\n牌手3",end=":")
for x in c:
    print(x,end=" ")
print("\n牌手4",end=":")
for x in d:
    print(x,end=" ")

运行效果:
《【Python】基础-发牌程序》

点赞

发表评论

电子邮件地址不会被公开。 必填项已用*标注