扑克牌发牌程序(P86)

import random
n=52

def gen_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>3:
        return "ERROR"
    return color[c]

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=gen_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("Player1",end=":")
for x in a:
    print(x,end=' ')
print("\nPlayer2",end=":")
for x in b:
    print(x,end=" ")
print("\nPlayer3",end=":")
for x in c:
    print(x,end=' ')
print("\nPlayer4",end=":")
for x in d:
    print(x,end=" ")

运行结果:

[18, 44, 21, 30, 42, 48, 36, 46, 38, 12, 0, 24, 9, 10, 32, 25, 41, 23, 14, 35, 34, 7, 45, 33, 17, 28, 3, 6, 1, 40, 51, 15, 39, 11, 22, 5, 26, 29, 16, 37, 2, 19, 4, 27, 13, 49, 47, 20, 31, 8, 50, 43]
Player1:♠3 ♠4 ♠A ♣10 ♣2 ♣3 ♥6 ♥9 ♥A ♥K ♦5 ♦6 ♦A
Player2:♠10 ♠2 ♠6 ♠J ♣8 ♣9 ♣J ♣K ♣Q ♥3 ♥4 ♦7 ♦J
Player3:♠7 ♠9 ♠K ♠Q ♣4 ♣5 ♣A ♥7 ♥J ♦10 ♦2 ♦4 ♦9
Player4:♠5 ♠8 ♣6 ♣7 ♥10 ♥2 ♥5 ♥8 ♥Q ♦3 ♦8 ♦K ♦Q

猜你喜欢

转载自blog.csdn.net/ur_ytii/article/details/112509367