Python_斗地主发牌程序

 

1)1副牌(52+2=54张)

2)有大小王,用提供的国王和皇后的符号表示大小王

3)54张牌发给3个玩家,每人17张牌,并有3张底牌。

4)输出3个玩家的牌,按照由小到大,黑红梅方的顺序。

5)输出3张底牌

5)输出每个玩家有几个炸弹(4张面值一样的牌,或者大小王在一起,为一个炸弹。)

'''
注:斗地主发牌程序
Player_Num:      代表玩家数目           
默认为3 可更改
times:           代表有几副扑克牌       
默认为1 可更改
leftover:        代表给地主留的底牌数目 
默认为3 可更改
order            代表牌排列顺序
参数值为1(默认值) 按照从小到大顺序排列
参数值为0:        按照手机斗地主排列顺序 从大牌到小牌 黑红梅方顺序排列
'''


order = 1               # 牌排列顺序 默认为1 即从小到大顺序排列  0:从大到小排列
Player_Num = 3          # 三名玩家 
N = 54                  # 一副牌有54张(包含大小王)
times = 1               # 一副牌
leftover = 3            # 3张底牌

# -*- coding: utf-8 -*-
"""
Created on Fri Oct 12 10:33:57 2018

@author: LIU Zhendong
"""



'''
注:斗地主发牌程序
Player_Num:      代表玩家数目           
默认为3 可更改
times:           代表有几副扑克牌       
默认为1 可更改
leftover:        代表给地主留的底牌数目 
默认为3 可更改
order            代表牌排列顺序
参数值为1(默认值) 按照从小到大顺序排列
参数值为0:        按照手机斗地主排列顺序 从大牌到小牌 黑红梅方顺序排列
'''


order = 1               # 牌排列顺序 默认为1 即从小到大顺序排列  0:从大到小排列
Player_Num = 3          # 三名玩家 
N = 54                  # 一副牌有54张(包含大小王)
times = 1               # 一副牌
leftover = 3            # 3张底牌




import random
card0 = list(range(0, N))
card0 = card0 * times
#print(card0)
schar = '♠♥♣♦♛♚'
card1 = []
left = N * times
for i in range (N * times):
    rdi = random.randint(0,left-1)
    card1.append(card0[rdi])
    card0.pop(rdi)
    left-=1
    if left==0:
        break
#print(card1)
Player = []
for i in range (Player_Num):
    Player.append([])
for i in range (N * times - leftover):
    Player[i % Player_Num].append(card1[i])
Player.append([])
for i in range (N * times - leftover, N * times):
    Player[Player_Num].append(card1[i])
for i in range(Player_Num + 1):
#    print(Player[i])
    Player[i].sort()
    if order == 0:
        Player[i].reverse()
#    print(Player[i])
card_output = []
for i in range (Player_Num + 1):
    card_output.append([])
for i in range (Player_Num + 1):
    for j in range(len(Player[i])):
        if Player[i][j] == 53 or Player[i][j] == 52:
            card_output[i].append(str(schar[Player[i][j]%2 + 4]).rjust(3))
        else:
            cardnumber = (Player[i][j] // 4 + 2) % 13 + 1
            cardflower = 3 - Player[i][j] % 4
            if cardnumber == 1:
                card_output[i].append('A'.rjust(2)+str(schar[cardflower]))
            elif cardnumber == 11:
                card_output[i].append('J'.rjust(2)+str(schar[cardflower]))
            elif cardnumber == 12:
                card_output[i].append('Q'.rjust(2)+str(schar[cardflower]))
            elif cardnumber == 13:
                card_output[i].append('K'.rjust(2)+str(schar[cardflower]))
            else:
                card_output[i].append(str(cardnumber).rjust(2)+str(schar[cardflower]))
#print(card_output)
for i in range (Player_Num):
    print('玩家{:>2}的手牌为:'.format(i+1),' '.join(card_output[i]))
print('剩余的{}张底牌为:'.format(leftover),' '.join(card_output[Player_Num]))
#for i in range(Player_Num + 1):
#    print(Player[i])

# 统计炸弹数目

boom = []
for i in range (Player_Num + 1):
    boom.append([])
count = 0
last_cardnumber = -1
for i in range (Player_Num + 1):
    if 53 in Player[i] and 52 in Player[i]:
        boom[i].append('炸弹{}: 王炸'.format(len(boom[i])+1))
    for data in Player[i]:
        if data == 53 or data == 52:
            continue
        else:
            cardnumber = (data // 4 + 2) % 13 + 1
            if last_cardnumber == cardnumber:
                count += 1
            else:
                if count >= 3:
                    if last_cardnumber == 1:
                        last_cardnumber = 'A'.rjust(2)
                    elif last_cardnumber == 11:
                        last_cardnumber = 'J'.rjust(2)
                    elif last_cardnumber == 12:
                        last_cardnumber = 'Q'.rjust(2)
                    elif last_cardnumber == 13:
                        last_cardnumber = 'K'.rjust(2)
                    else:
                        last_cardnumber = str(last_cardnumber).rjust(2)
                    boom[i].append('炸弹{}:{}个{:>2}'.format(len(boom[i])+1,count + 1, last_cardnumber))
                count = 0
                last_cardnumber = cardnumber
boom_num = 0  #炸弹数目
for i in range (Player_Num):
    if len(boom[i]) != 0:
        print('玩家{:>2}有{:>2}个炸弹,为:'.format(i+1,len(boom[i])),'   '.join(boom[i]))
        boom_num += 1
if len(boom[Player_Num]) != 0:
    print('底牌中有{:>2}个炸弹,为:'.format(len(boom[Player_Num])),'   '.join(boom[Player_Num]))
    boom += 1
if boom_num == 0:
    print('本局未产生炸弹')

 

你可能感兴趣的:(python)