Python实现炸金花小游戏

炸金花小游戏

规则:

一付扑克牌,去掉大小王,每个玩家发3张牌,最后比大小,看谁赢。

有以下几种牌:

豹子:三张一样的牌,如3张6.

顺金:又称同花顺,即3张同样花色的顺子, 如红桃 5、6、7

顺子:又称拖拉机,花色不同,但是顺子,如红桃5、方片6、黑桃7,组成的顺子

对子:2张牌一样

单张:单张最大的是A

这几种牌的大小顺序为, **豹子>顺金>顺子>对子>单张

目的:

自己写一个程序,实现发牌、比大小判断输赢。

源码:
import random

infor = {}
fluents = ['234', '345', '456', '567', '678', '789', '910J', '10JQ', 'JQK', 'QKA']
same_color = ['黑桃黑桃黑桃', '红桃红桃红桃', '方块方块方块', '梅花梅花梅花 ']
infor1 = {}


def transformed(figure1):
    for m in range(0, 3):
        if figure1[m] == '2':
            figure1[m] = 2
        if figure1[m] == '3':
            figure1[m] = 3
        if figure1[m] == '4':
            figure1[m] = 4
        if figure1[m] == '5':
            figure1[m] = 5
        if figure1[m] == '6':
            figure1[m] = 6
        if figure1[m] == '7':
            figure1[m] = 7
        if figure1[m] == '8':
            figure1[m] = 8
        if figure1[m] == '9':
            figure1[m] = 9
        if figure1[m] == '10':
            figure1[m] = 10
        if figure1[m] == 'J':
            figure1[m] = 11
        if figure1[m] == 'Q':
            figure1[m] = 12
        if figure1[m] == 'K':
            figure1[m] = 13
        if figure1[m] == 'A':
            figure1[m] = 14
    return figure1


def judge(list_element, n):
    figure = []
    first_one = list_element[0]
    second_one = list_element[1]
    third_one = list_element[2]
    card_color_of_flowers1 = first_one[0:1]
    card_color_of_flowers2 = second_one[0:1]
    card_color_of_flowers3 = third_one[0:1]
    card_value1 = first_one[first_one.find(' ') + 1:]
    card_value2 = second_one[second_one.find(' ') + 1:]
    card_value3 = third_one[third_one.find(' ') + 1:]
    figure.append(card_value1)
    figure.append(card_value2)
    figure.append(card_value3)
    transformed(figure)
    connect_value = card_value1 + card_value2 + card_value3
    connect_color = card_color_of_flowers1 + card_color_of_flowers2 + card_color_of_flowers3
    connect_value1 = card_value1 + card_value3 + card_value2
    connect_value2 = card_value2 + card_value1 + card_value3
    connect_value3 = card_value2 + card_value3 + card_value1
    connect_value4 = card_value3 + card_value2 + card_value1
    connect_value5 = card_value3 + card_value1 + card_value2
    b = (connect_value in fluents) or (connect_value1 in fluents) or (connect_value2 in fluents) or (
            connect_value3 in fluents) or (connect_value4 in fluents) or (connect_value5 in fluents)
    if card_value1 == card_value2 == card_value3:
        infor1["player" + str(n)] = {"牌型": "豹子", "手牌": figure}
        return
    if ((connect_value in fluents) or (connect_value1 in fluents) or (connect_value2 in fluents) or (
            connect_value3 in fluents) or (connect_value4 in fluents) or (connect_value5 in fluents)) and (
            connect_color in same_color):
        infor1["player" + str(n)] = {"牌型": "同花顺", "手牌": figure}
        return
    if card_value1 == card_value2 or card_value1 == card_value3 or card_value2 == card_value3:
        infor1["player" + str(n)] = {"牌型": "对子", "手牌": figure}
        return
    if (b) and (connect_color not in same_color):
        infor1["player" + str(n)] = {"牌型": "顺子", "手牌": figure}
        return
    if ((connect_value not in fluents) and (connect_value1 not in fluents) and (connect_value2 not in fluents) or (
            connect_value3 not in fluents) and (connect_value4 not in fluents) and (connect_value5 not in fluents)):
        infor1["player" + str(n)] = {"牌型": "单张", "手牌": figure}
        return


a = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']
b = ['红桃', '黑桃', '方块', '梅花']
sumcards = []
for i in range(0, len(a)):
    for j in range(0, len(b)):
        f = b[j] + ' ' + a[i]
        sumcards.append(f)

player1 = random.sample(sumcards, 3)
for l1 in range(0, len(player1)):
    sumcards.remove(player1[l1])
player2 = random.sample(sumcards, 3)
for l2 in range(0, len(player2)):
    sumcards.remove(player2[l2])
player3 = random.sample(sumcards, 3)
for l3 in range(0, len(player3)):
    sumcards.remove(player3[l3])
player4 = random.sample(sumcards, 3)
for l4 in range(0, len(player4)):
    sumcards.remove(player4[l4])
player5 = random.sample(sumcards, 3)
for l5 in range(0, len(player5)):
    sumcards.remove(player5[l5])
player_card_info = []
player_card_info.append(player1)
player_card_info.append(player2)
player_card_info.append(player3)
player_card_info.append(player4)
player_card_info.append(player5)
# print(player1, player2, player3, player4, player5)
print(player_card_info)
for i in range(1, 6):
    judge(player_card_info[i - 1], i)
card_class = []
hand_card = []
player1_card = infor1['player1']
player2_card = infor1['player2']
player3_card = infor1['player3']
player4_card = infor1['player4']
player5_card = infor1['player5']
card_type1 = player1_card['牌型']
card_type2 = player2_card['牌型']
card_type3 = player3_card['牌型']
card_type4 = player4_card['牌型']
card_type5 = player5_card['牌型']
hand_card.append(player1_card['手牌'])
hand_card.append(player2_card['手牌'])
hand_card.append(player3_card['手牌'])
hand_card.append(player4_card['手牌'])
hand_card.append(player5_card['手牌'])
card_class.append(card_type1)
card_class.append(card_type2)
card_class.append(card_type3)
card_class.append(card_type4)
card_class.append(card_type5)
for i in range(0, 5):
    hand_card[i] = sorted(hand_card[i])
re_hand_card = hand_card
list_one = [0, 0, 0, 0, 0]
list_two = [0, 0, 0, 0, 0]
list_three = [0, 0, 0, 0, 0]
list_four = [0, 0, 0, 0, 0]
list_five = [0, 0, 0, 0, 0]
list_six = [0, 0, 0, 0, 0]
list_seven=[0,0,0,0,0]
if '单张' in card_class and '豹子' not in card_class and '顺子' not in card_class and '同花顺' not in card_class and '对子' not in card_class:
    for i in range(0, 5):
        if card_class == '单张':
            list_one[i] = re_hand_card[i][2]
    maxvalue = max(list_one)
    maxnum = list_one.count(maxvalue)
    if maxnum == 1:
        number = list_one.index(maxvalue)
        print("player", number + 1, "is the winner!")
    if maxnum>=2:
        for i in range(0,5):
            if maxvalue==list_one[i]:
                list_two[i]=re_hand_card[i][1]
        max_next=max(list_two)
        maxnum1=list_two.count(max_next)
        if maxnum1==1:
            number9=list_two.index(max_next)
            print("player", number9+1,"is the winner!")
        if maxnum1>=2:
            for i in range(0,5):
                if max_next==list_two[i]:
                    list_seven[i]=re_hand_card[i][0]
            max_next1=max(list_seven)
            maxnum2=list_seven.count(max_next1)
            if maxnum2==1:
                number10=list_seven.index(max_next1)
                print("player", number10+1,"is the winner!")
            if maxnum2>=2:
                print("This round is tie!")


if '豹子' in card_class and card_class.count('豹子') == 1:
    number1 = card_class.index('豹子')
    print("player", number1 + 1, "is the winner!")
if '豹子' in card_class and card_class.count('豹子') >= 2:
    for i in range(0, 5):
        if card_class[i] == '豹子':
            list_three[i] = re_hand_card[i][2]
    max_value4 = max(list_three)
    number3 = list_three.index(max_value4)
    print("player", number3 + 1, "is the winner!")
if '同花顺' in card_class and '豹子' not in card_class and card_class.count('同花顺') == 1:
    number2 = card_class.index('同花顺')
    print("player", number2 + 1, "is the winner!")
if '同花顺' in card_class and '豹子' not in card_class and card_class.count('同花顺') >= 2:
    for i in range(0, 5):
        if card_class[i] == '同花顺':
            list_four[i] = re_hand_card[i][0]
    max_value5 = max(list_four)
    number5 = list_four.index(max_value5)
    print("player", number5 + 1, "is the winner!")
if '顺子' in card_class and '同花顺' not in card_class and '豹子' not in card_class and card_class.count('顺子') == 1:
    number3 = card_class.index('顺子')
    print("player", number3 + 1, "is the winner!")
if '顺子' in card_class and '同花顺' not in card_class and '豹子' not in card_class and card_class.count('顺子') >= 2:
    for i in range(0, 5):
        if card_class[i] == '顺子':
            list_five[i] = re_hand_card[i][0]
    max_value6 = max(list_four)
    number6 = list_four.index(max_value6)
    print("player", number6 + 1, "is the winner!")
if '对子' in card_class and '顺子' not in card_class and '同花顺' not in card_class and '豹子' not in card_class and card_class.count(
        '对子') == 1:
    number7 = card_class.index('对子')
    print("player", number7 + 1, "is the winner!")
if '对子' in card_class and '顺子' not in card_class and '同花顺' not in card_class and '豹子' not in card_class and card_class.count(
        '对子') >= 2:
    for i in range(0, 5):
        if card_class[i] == '对子':
            list_six[i] = re_hand_card[i][2]
    max_value7 = max(list_six)
    if list_six.count(max_value7) == 1:
        number8 = list_six.index(max_value7)
        print("player", number8 + 1, "is the winner!")

  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值