洗牌程序

#  -*- coding: cp936 -*-
import  random
'''
方法1.使用random.shuffle进行洗牌
H:Hearts
S:Spade
C:Clubs
D:Diamonds
'''
suits 
=  [ ' H ' , ' S ' , ' C ' , ' D ' ]
numbs 
=  [ ' 2 ' , ' 3 ' , ' 4 ' , ' 5 ' , ' 6 ' , ' 7 ' , ' 8 ' , ' 9 ' , ' 10 ' , ' J ' , ' Q ' , ' K ' , ' A ' ]
jokers 
=  [ ' BJ ' , ' RJ ' ]

cards 
=  []
#  生成2-A的所有牌
for  s  in  suits:
    
for  n  in  numbs:
        cards.append(s 
+  n)
#  生成二王
cards.extend(jokers)
print  cards
#  洗牌
random.shuffle(cards)

B1 
=  []
B2 
=  []
H1 
=  []
H2 
=  []
''' 取牌:
B1:使用索引为4的倍数
B2:使用余数为2的索引
H1:使用余数为1的索引
H2:使用余数为3的索引
'''
for  card  in  cards[: - 6 ]:
    
if  cards.index(card)  %   4   ==  0:
        B1.append(card)
    
elif  cards.index(card)  %   4   ==   2 :
        B2.append(card)
    
elif  cards.index(card)  %   4   ==   1 :
        H1.append(card)
    
else :
        H2.append(card)
#  打印洗牌结果
print  B1,len(B1)
print  B2,len(B2)
print  H1,len(H1)
print  H2,len(H2)

cards 
=  []
#  生成2-A的所有牌
for  s  in  suits:
    
for  n  in  numbs:
        cards.append(s 
+  n)
#  生成二王
cards.extend(jokers)
print  cards
B1 
=  []
B2 
=  []
H1 
=  []
H2 
=  []
'''
方法2:使用random.choice取牌
取牌:
共12此取牌,每轮B1,H1,B2,H2随机取牌,并从列表中删除,以后从剩余的牌中选取
'''
for  i  in  range( 12 ):
    card 
=  random.choice(cards)
    B1.append(card)
    cards.remove(card)

    card 
=  random.choice(cards)
    B2.append(card)
    cards.remove(card)

    card 
=  random.choice(cards)
    H1.append(card)
    cards.remove(card)

    card 
=  random.choice(cards)
    H2.append(card)
    cards.remove(card)
#  打印取牌结果
print  B1,len(B1)
print  B2,len(B2)
print  H1,len(H1)
print  H2,len(H2)
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值