主要利用了random模块:
import random
from random import shuffle
#生成牌
def get_cards():
num=['A','2','3','4','5','6','7','8','9','10','J','Q','K']
suits=['\u2660','\u2663','\u2665','\u2666']
pokers=['大王','小王']
cards=[ x+y for x in suits for y in num]
cards+=pokers
shuffle(cards)
return cards
def deal_cards():
#选取底牌
cards=get_cards()
bottoms=cards[51:54]
print('底牌:',bottoms)
#发牌
p1=[None]*17
p2=[None]*17
p3=[None]*17
j=0
for i in range(0,17):
p1[i]=cards[j]
p2[i]=cards[(j+1)]
p3[i]=cards[(j+2)]
j+=3
#抢地主,拿底牌
landlord=random.randint(1,3)
if landlord==1:
p1.append(bottoms)
p1.insert(0,'地主')
p2.insert(0,'农民')
p3.insert(0, '农民')
elif landlord==2:
p2.append(bottoms)
p2.insert(0,'地主')
p1.insert(0, '农民')
p3.insert(0, '农民')
else:
p3.append(bottoms)
p3.insert(0,'地主')
p1.insert(0, '农民')
p2.insert(0, '农民')
print('p1是:',p1[0],',手牌是:',p1[1:])
print('p2是:',p2[0],',手牌是:',p2[1:])
print('p3是:',p3[0],',手牌是:',p3[1:])
deal_cards()
运行如下:
底牌: ['♥3', '♥4', '♠7']
p1是: 农民 ,手牌是: ['♦2', '♣7', '♥10', '♦8', '♥A', '♠Q', '♠9', '♣Q', '♣4', '♥K', '♣5', '♥9', '♠4', '♠2', '小王', '♠10', '♣6']
p2是: 地主 ,手牌是: ['♠5', '♣3', '♥5', '大王', '♥Q', '♦5', '♠8', '♣2', '♣8', '♥7', '♦K', '♣K', '♦Q', '♣9', '♦3', '♦10', '♥6', ['♥3', '♥4', '♠7']]
p3是: 农民 ,手牌是: ['♠J', '♠A', '♦A', '♥2', '♦9', '♦6', '♦J', '♣10', '♠6', '♦4', '♣A', '♥J', '♥8', '♠3', '♠K', '♦7', '♣J']