python实现斗地主分牌洗牌

2020-06-22 12:59 来源:易采站长站 作者:于海丽 点击: 评论:

A-A+

原标题:python实现斗地主分牌洗牌

本文实例为大家分享了python实现斗地主分牌洗牌的具体代码,供大家参考,具体内容如下

1、建立一个序列,共54个元素,表示一副扑克牌,初始化时是有顺序的。
2、编写一段代码完成洗牌功能,即将刚才的序列从有序的排列变成随机的无序排列。
3、编写一段代码完成切牌功能,即找到一个1-54的随机数,将刚才洗牌后的序列从这个随机数开始。
4、编写一段代码完成发牌功能,将刚才这个切牌后的无序序列,按顺序分发到三个序列中去,并剩余三张底牌(也是一个序列)。
5、编写一段代码完成理牌功能,首先将分牌后的每个序列排序(有自带的函数),然后按照从大到小的顺序按牌型的方式显示出来。
6、完成随机叫地主功能,随机出现一个地主,将三张底牌分给这个地主,形成新的序列并且降序排列。
7、要显示出牌的名称,如:Spade2(黑桃2)、Heart2(红桃2)、Club2(梅花2)、Diamond2(方片2)等,也可以用缩写:S2、H2、C2、D2等。可以使用映射(即字典),也可以自行编写显示程序。

代码如下

poker=[]   #产生一副牌
pokers=[]
num=101    #用101代指黑桃1,102代指红桃1,201代指黑桃2
for length in range(0,52):
 poker.append(num)
 num+=1
 if (length+1)%4==0:
 num-=4
 num+=10
poker.extend([230,240])
print(poker)

import random    #洗牌 奇数为一组 偶数为一组
for leng in range(0,8): 
 poker1=[]
 poker2=[]
 for length in range(0,54):
 if length%2==0:
  poker1.append(poker[length])
 else:
  poker2.append(poker[length])
 temp=random.randrange(0,2) #为1时 偶奇相加 ,为2时奇偶相加,共2**8种结果
 if temp%2==0:
 poker=poker1+poker2
 else:
 poker=poker2+poker1
print(poker)

number=int(input("请输入切牌的位置;")) #切牌
poker1=poker[0:number]
poker2=poker[number:54]
poker=poker2+poker1
print(poker)

player1=[]    #分牌
player2=[]
player3=[]
for length in range(0,51):
 if length%3==0:
 player1.append(poker[length])
 if length%3==1:
 player2.append(poker[length])
 if length%3==2:
 player3.append(poker[length])

for length in range(51,54):  #剩下的三张牌 (地主的牌)
 num=(poker[length]-90)//10
 temp=num
 if num==11:
 temp='J'
 if num==12:
 temp='Q'
 if num==13:
 temp='K'
 
 if poker[length]%10==1:
 name=str(temp)+'S'
 pokers.append(name)
 elif poker[length]%10==2:
 name=str(temp)+'H'
 pokers.append(name)
 elif poker[length]%10==3:
 name=str(temp)+'C'
 pokers.append(name)
 elif poker[length]%10==4:
 name=str(temp)+'D'
 pokers.append(name)
 elif poker[length]==230:
 name='小王'
 pokers.append(name)
 elif poker[length]==240:
 name='大王'
 pokers.append(name)
print(pokers)

power=random.randrange(1,4)
if power==1:
 player1.extend(poker[51:54])
if power==2:
 player2.extend(poker[51:54])
if power==3:
 player3.extend(poker[51:54])
player1.sort()    #降序
player2.sort()  
player3.sort()
player1=player1[::-1]
player2=player2[::-1]
player3=player3[::-1]


player1s=[]    #显示
player2s=[]
player3s=[]
for length in range(0,len(player1)):  #显示 player1
 num=(player1[length]-90)//10
 temp=num
 if num==11:
 temp='J'
 if num==12:
 temp='Q'
 if num==13:
 temp='K'
 
 if player1[length]%10==1:
 name=str(temp)+'S'
 player1s.append(name)
 elif player1[length]%10==2:
 name=str(temp)+'H'
 player1s.append(name)
 elif player1[length]%10==3:
 name=str(temp)+'C'
 player1s.append(name)
 elif player1[length]%10==4:
 name=str(temp)+'D'
 player1s.append(name)
 elif player1[length]==230:
 name='小王'
 player1s.append(name)
 elif player1[length]==240:
 name='大王'
 player1s.append(name)

for length in range(0,len(player2)):  #显示 player2
 num=(player2[length]-90)//10
 temp=num
 if num==11:
 temp='J'
 if num==12:
 temp='Q'
 if num==13:
 temp='K'
 
 if player2[length]%10==1:
 name=str(temp)+'S'
 player2s.append(name)
 elif player2[length]%10==2:
 name=str(temp)+'H'
 player2s.append(name)
 elif player2[length]%10==3:
 name=str(temp)+'C'
 player2s.append(name)
 elif player2[length]%10==4:
 name=str(temp)+'D'
 player2s.append(name)
 elif player2[length]==230:
 name='小王'
 player2s.append(name)
 elif player2[length]==240:
 name='大王'
 player2s.append(name)
for length in range(0,len(player3)):  #显示 player3
 num=(player3[length]-90)//10
 temp=num
 if num==11:
 temp='J'
 if num==12:
 temp='Q'
 if num==13:
 temp='K'
 
 if player3[length]%10==1:
 name=str(temp)+'S'
 player3s.append(name)
 elif player3[length]%10==2:
 name=str(temp)+'H'
 player3s.append(name)
 elif player3[length]%10==3:
 name=str(temp)+'C'
 player3s.append(name)
 elif player3[length]%10==4:
 name=str(temp)+'D'
 player3s.append(name)
 elif player3[length]==230:
 name='小王'
 player3s.append(name)
 elif player3[length]==240:
 name='大王'
 player3s.append(name)
if len(player1s)==17:
 print('农民:',player1s)
else:
 print('地主:',player1s)
if len(player2s)==17:
 print('农民:',player2s)
else:
 print('地主:',player2s)
if len(player3s)==17:
 print('农民:',player3s)
else:
 print('地主:',player3s)

【易采站长站编辑:秋军】