直接上代码:
import random
class cards:
def __init__(self): # 生成一副54张的牌
self.allcards = ['joker','Joker'] # 表示大王、小王
for i in range(1,14):
for k in range(4):
self. allcards.append(i) # 这里我没有区分红桃黑桃这些,留给大家尝试
def shuffle_card(self): # 洗牌,打乱顺序
random.shuffle(self.allcards) #这个函数是用来改变一个数组的顺序
def take_card(self,n=1): # 抽取一定数量的牌,默认为1张
your_card = []
for i in range(n):
your_card.append(self.allcards.pop())
return your_card
def recovery(self,Ifshuffle=True): # 收牌,重新开始,默认洗牌
if Ifshuffle:
self.__init__()
self.shuffle_card()
else:
self.__init__()
试试用它玩21点游戏:
# 模拟两个人玩21点游戏
card = cards()
print('原始牌:',card.allcards)
card.shuffle_card()
print('洗牌后:',card.allcards)
print('21点双人比拼,第一轮游戏')
print('you\t\t\tme')
for i in range(10):
you = card.take_card(random.choice([2,3,]))
me = card.take_card(random.choice([2,3,]))
print(you,' \t\t',me)
print('\n21点双人比拼,第二轮游戏')
card.recovery()
print('you\t\t\tme')
for i in range(10):
you = card.take_card(random.choice([2,3,]))
me = card.take_card(random.choice([2,3,]))
print(you,' \t\t',me)