Python斗地主不完全代码

import random

# 定义扑克牌的花色和大小
suits = ["♠", "♥", "♦", "♣"]
ranks = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]

# 创建扑克牌
deck = [(rank, suit) for suit in suits for rank in ranks]

# 洗牌
random.shuffle(deck)

# 发牌
player_1 = sorted(deck[:17], key=lambda x: (ranks.index(x[0]), suits.index(x[1])))
player_2 = sorted(deck[17:34], key=lambda x: (ranks.index(x[0]), suits.index(x[1])))
player_3 = sorted(deck[34:51], key=lambda x: (ranks.index(x[0]), suits.index(x[1])))
landlord_cards = sorted(deck[51:], key=lambda x: (ranks.index(x[0]), suits.index(x[1])))

# 地主标识
landlord = None

# 定义牌型
SINGLE = 1
PAIR = 2
TRIPLE = 3
BOMB = 4

# 检查牌型
def check_card_type(cards):
    num_cards = len(cards)
    if num_cards == 1:
        return SINGLE
    elif num_cards == 2 and cards[0][0] == cards[1][0]:
        return PAIR
    elif num_cards == 3 and cards[0][0] == cards[1][0] == cards[2][0]:
        return TRIPLE
    elif num_cards == 4 and cards[0][0] == cards[1][0] == cards[2][0] == cards[3][0]:
        return BOMB
    else:
        return None

# 主游戏逻辑
def play_game():
    players = [player_1, player_2, player_3]
    current_player = random.choice([0, 1, 2])
    pass_count = 0
    last_cards = []
    
    while True:
        player_cards = players[current_player]
        print("玩家", current_player + 1, "的牌:", player_cards)
        
        if current_player == landlord:
            print("地主的牌:", landlord_cards)
        
        if last_cards:
            print("上家出的牌:", last_cards)
        
        if pass_count == 2:
            last_cards = []
        
        if player_cards == [] or (last_cards and current_player == landlord and landlord_cards == []):
            if current_player == landlord:
                print("地主胜利!")
            else:
                print("农民胜利!")
            break
        
        option = input("请选择要出的牌,或输入'pass'跳过:")
        
        if option == "pass":
            pass_count += 1
            current_player = (current_player + 1) % 3
            continue
        
        selected_cards = []
        for card_str in option.split():
            rank, suit = card_str[:-1], card_str[-1]
            for card in player_cards:
                if card[0] == rank and card[1] == suit:
                    selected_cards.append(card)
                    break
        
        if set(selected_cards).issubset(set(player_cards)):
            selected_cards_type = check_card_type(selected_cards)
            last_cards_type = check_card_type(last_cards)
            
            if not last_cards or (selected_cards_type and selected_cards_type == last_cards_type and len(selected_cards) == len(last_cards)):
                player_cards = [card for card in player_cards if card not in selected_cards]
                players[current_player] = player_cards
                last_cards = selected_cards
                pass_count = 0
                current_player = (current_player + 1) % 3
            else:
                print("出牌不符合规则!")
        else:
            print("选择的牌不存在或不在手牌中!")

# 随机确定地主
landlord = random.randint(0, 2)

# 开始游戏
play_game()