python模拟实现斗地主发牌
人气:0题目:趣味百题之斗地主
扑克牌是一种非常大众化的游戏,在计算机中有很多与扑克牌有关的游戏。例如,在Windows操作系统下自带的纸牌、红心大战等。在扑克牌类的游戏中,往往都需要执行洗牌操作,就是将一副牌完全打乱,使其排列没有规律。
要求:
1.54张扑克牌发个3个玩家,农民17张,地主20张。
2.自动生成一幅扑克牌组;洗牌;发牌到玩家手中;将玩家手中扑克牌按花色大小整理好。**
''' 趣味百题-发扑克牌-斗地主 请编程实现发扑克牌 Version: 0.1 Author: jasn Date: 2020-01-01 主要知识点:random的用法,类的定义和调用 主要功能点:用Python的类实现斗地主的初始化、洗牌、发牌、抢地主、理牌、花色对应。代码如下 ''' import random class doudizhu: #定义54张牌 def __init__(self): self.a=[] for i in range(54): self.a.append(i) #洗牌 def xipai(self): random.shuffle(self.a) #洗牌 n = random.randint(1, 54) b = self.a[:n] # 从n的位置切牌 c = self.a[n:] self.a = b + c #发牌 def fapai(self): self.user1=self.a[0:-3:3] #玩家1,获得牌的顺序为 0,3,6,9... self.user2=self.a[1:-3:3] #玩家2,获得牌的顺序为 1,4,7,10... self.user3=self.a[2:-3:3] #玩家3,获得牌的顺序为 2,5,8,11... self.user4=self.a[-3:] #底牌 ,顺序为51,52,53 #抢地主 def qiangdizhu(self): i=random.randint(1,3) self.dizhu=i #定义一个地主的实例 if i == 1: self.user1+=self.user4 if i == 2: self.user2+=self.user4 if i == 3: self.user3+=self.user4 #码牌 def mapai(self): self.user1.sort(reverse=True) ##从小到大码牌 self.user2.sort(reverse=True) self.user3.sort(reverse=True) #牌序和花色一一对应 def yingshe(self): huase = [(0, '方片3'), (1, '梅花3'), (2, '红桃3'), (3, '黑桃3'), (4, '方片4'), (5, '梅花4'), (6, '红桃4'), (7, '黑桃4'), (8, '方片5'), (9, '梅花5'), (10, '红桃5'), (11, '黑桃5'), (12, '方片6'), (13, '梅花6'), (14, '红桃6'), (15, '黑桃6'), (16, '方片7'), (17, '梅花7'), (18, '红桃7'), (19, '黑桃7'), (20, '方片8'), (21, '梅花8'), (22, '红桃8'), (23, '黑桃8'), (24, '方片9'), (25, '梅花9'), (26, '红桃9'), (27, '黑桃9'), (28, '方片10'), (29, '梅花10'), (30, '红桃10'), (31, '黑桃10'), (32, '方片J'), (33, '梅花J'), (34, '红桃J'), (35, '黑桃J'), (36, '方片Q'), (37, '梅花Q'), (38, '红桃Q'), (39, '黑桃Q'), (40, '方片K'), (41, '梅花K'), (42, '红桃K'), (43, '黑桃K'), (44, '方片A'), (45, '梅花A'), (46, '红桃A'), (47, '黑桃A'), (48, '方片2'), (49, '梅花2'), (50, '红桃2'), (51, '黑桃2'), (52, 'BlackJoker'), (53, 'RedJoker')] zdpai = dict(huase) paiuser1='' for i in range(len(self.user1)): paiuser1+=zdpai[self.user1[i]]+' ' #以字符串的形式将牌储存起来 paiuser2 = '' for i in range(len(self.user2)): paiuser2 += zdpai[self.user2[i]] + ' ' paiuser3 = '' for i in range(len(self.user3)): paiuser3 += zdpai[self.user3[i]] + ' ' paiuser4 = '' for i in range(len(self.user4)): paiuser4 += zdpai[self.user4[i]] + ' ' self.user1 = paiuser1 #把花色对应好的牌的序列重新赋给三个玩家的实例属性 self.user2 = paiuser2 self.user3 = paiuser3 self.user4 = paiuser4 if __name__ == '__main__': Player=doudizhu() #将类辅助给playes,方便调用 Player.xipai() Player.fapai() Player.qiangdizhu() Player.mapai() Player.yingshe() print('本局地主是:玩家{}'.format(Player.dizhu)) print('底牌:',Player.user4) print('玩家一:',Player.user1) print('玩家二:',Player.user2) print('玩家三:',Player.user3)
您可能感兴趣的文章:
加载全部内容