比较扑克程序中的高牌

问题描述 投票:0回答:1

我在比较扑克程序中的高牌时遇到了麻烦。我想做的 适合符号而不是字母,所以我将一个简单的程序复杂化并将其转换为“中等”水平的编程。

import random, time, json
from random import shuffle
from collections import Counter
from enum import Enum

class CardSuits(Enum):
    Hearts = "Hearts"
    Diamonds = "Diamonds"
    Clubs = "Clubs"
    Spades = "Spades"

class CardValues(Enum):
    Two = 2
    Three = 3
    Four = 4
    Five = 5
    Six = 6
    Seven = 7
    Eight = 8
    Nine = 9
    Ten = 10
    Jack = "J"
    Queen = "Q"
    King = "K"
    Ace = "A"


class Card:

    _symbols = {"Hearts": "♥️", "Diamonds": "♦️", "Clubs": "♣️", "Spades": "♠"}

    def __init__(self, suit: CardSuits, value: CardValues) -> None:
        self.suit = suit
        self.value = value

    def __str__(self) -> str:
        return f"{self.value.value}{self._symbols[self.suit.name]}"

    def __repr__(self) -> str:
        return f"{self.value.value}{self._symbols[self.suit.name]}"

#GAME START
def start():
global game_cycles
scores = load_scores()
play = input("You wanna play? ")
if play.lower() in ("yes", "sure", "yeah"):
    all_cards = [Card(suits, value) for suits in CardSuits for value in CardValues]
    shuffle(all_cards)
    unique_cards = set()
    while len(unique_cards) < 5:
        unique_cards.add(random.choice(all_cards))
    cards = list(unique_cards)

    #OPPONENTS CARD CREATION
    # First opponent
    unique_cards = set()
    while len(unique_cards) < 5:
        unique_cards.add(random.choice(all_cards))
    first_opponent_cards = list(unique_cards)
    # Second
    unique_cards = set()
    while len(unique_cards) < 5:
        unique_cards.add(random.choice(all_cards))
        second_opponent_cards = list(unique_cards)
    #CHECKING PHASE
    def checking_phase(cards):
        High_Card, Pair, Two_Pair, Three_of_a_Kind, Straight, Flush, Full, Four_of_a_Kind, Straight_Flush, Royal_Flush = ("High Card", "Pair", "Two Pair", "Three of a Kind", "Straight", "Flush", "Full", "Four of a Kind", "Straight Flush", "Royal Flush")
        #if/elif series for returning other combinations
        else:
            return High_Card

    user_hand = checking_phase(cards)
    first_opponent_hand = checking_phase(first_opponent_cards)
    second_opponent_hand = checking_phase(second_opponent_cards)
    
    #SCORE DASHBOARD
    players = [user_hand, first_opponent_hand, second_opponent_hand]
    scores = [0, 0, 0] if not scores else scores
    # Determina il giocatore con la combinazione più alta
    max_hand_index = max(range(len(players)), key=lambda i: players[i])
    # Incrementa il punteggio del giocatore con la combinazione più alta
    if user_hand == "High Card" and first_opponent_hand == "High Card" and second_opponent_hand == "High Card":
        cards_remove_cycle = 0
        for player in players:
            for card in cards:
                user_high = max(cards)
                first_op_high = max(first_opponent_cards)
                second_op_high = max(second_opponent_cards)
                high_list = [user_high, first_op_high, second_op_high]
                #caso in cui ci siano due o più player con max identici
                if user_high == first_op_high or user_high == second_op_high or first_op_high == second_op_high:                    

                else: #caso in cui il max di ogni player sia diverso, quindi solo 1 vincitore
                    high_card_winner = max(high_list)
                    HC_winner_index = high_list.index(high_card_winner)
                    scores[HC_winner_index] += 1
                                  
    else:
        scores[max_hand_index] += 1

在这种情况下,终端打印错误:

user_high = max(卡片)
类型错误:“Card”和“Card”实例之间不支持“>”

所以我尝试使用值:

if user_hand == "High Card" and first_opponent_hand == "High Card" and second_opponent_hand == "High Card":
        cards_remove_cycle = 0
        user_high = max(card.value for card in cards)
        first_op_high = max(card.value for card in first_opponent_cards)
        second_op_high = max(card.value for card in second_opponent_cards)
        high_list = [user_high, first_op_high, second_op_high]
        #caso in cui ci siano due o più player con max identici
        if user_high == first_op_high or user_high == second_op_high or first_op_high == second_op_high:
            # for player in players:
            #     for card in cards:
            print("Eureka! it works")

但是给我返回了和以前一样的错误。所以我基本上不知道在所有玩家都有高牌的情况下如何进行比较牌的部分。我的想法是(如果两个或更多玩家拥有相同的最大牌)创建玩家手牌的副本列表并删除相同的牌(同时如果玩家拥有较低的高牌则取消其资格),直到剩下最大牌的玩家(除非 2 名玩家拥有相同的牌,在这种情况下将成为一对)。 先谢谢你了

python python-3.x dictionary class poker
1个回答
0
投票

__gt__
方法添加到您的
Card
类以支持
>
运算符。


class Card:

    def __gt__(self, other):
        '''return True or False'''
        ...

请参阅文档了解更多详细信息。

© www.soinside.com 2019 - 2024. All rights reserved.