将类对象追加到列表中会删除其方法

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

如何让我的随机播放方法效果更好?

import random

class Card:
    def __init__(self, rank=0, suit="j", color =""):
        self.rank = rank
        self.suit = suit
        self.color = color
        self.suits = {
            "s": "Spades",
            "c": "Clubs",
            "d": "Diamonds",
            "h": "Hearts",
        }


        if rank > 13:
            raise Exception("Rank must be in the range of 1 through 13")
        elif rank < 1:
            raise Exception("Rank must be in the range of 1 through 13")

        if rank == 1:
            self.rank = "Ace"

        if suit in ["d", "h"]:
            self.color = "red"
        elif suit in ["s", "c"]:
            self.color = "black"
        elif suit == "j":
            self.color = None

        self.values = {
            1: 11,
            2: 2,
            3: 3,
            4: 4,
            5: 5,
            6: 6,
            7: 7,
            8: 8,
            9: 9,
            10: 10,
            11: 10,  # Jack
            12: 10,  # Queen
            13: 10,  # King
        }
    def getValue(self):
        """


        """
        return self.values[self.rank]
    def __str__(self):
        if self.rank == 1:
            return f"1 of {self.suits[self.suit]}"
        else:
            return f"{self.rank} of {self.suits[self.suit]}"
    def getRank(self):
        if self.rank == 1:
            return 1
        else:
            return self.rank
    def getSuit(self):
        return self.suits[self.suit]
    def getColor(self):
        return self.color
class Deck:
    def __init__(self,jokers = 0):
        self.stack = []
        self.suits = ["h", "c", "d", "s"]
        #counter = 0
        for suit in self.suits:
            for rank in range(1,14):
                #counter += 1
                #card_name = "c" + str(counter)
                c = Card(rank, suit)
                self.stack.append(c)

    def __str__(self):
        for card in self.stack:
            print(card.__str__())
    def shuffle(self):
        """Randomizes the order of the remaining cards in the deck."""
        counter = 0
        temp_list = []
        for i in range(52):
            randomNum = random.randrange(0,len(self.stack))
            temp_list.append(self.stack.pop(randomNum))
            self.stack = temp_list
    def draw(self, n = 1):
        """
        Removes and returns the top n cards from the top of the deck.
        """
        if not self.stack:
            return None
        x = []
        for i in range(n):
            x.append(self.stack.pop())
            if len(x) == 1:
                return x[0]
            else:
                return x
def main():
    gameOn = True
    values = {
        1: 11,
        2: 2,
        3: 3,
        4: 4,
        5: 5,
        6: 6,
        7: 7,
        8: 8,
        9: 9,
        10: 10,
        11: 10,  # Jack
        12: 10,  # Queen
        13: 10,  # King
    }
    deck = Deck()
    deck.shuffle()
    hand = []
    counter = 0
    card_values = 0
    while gameOn:
        card = deck.draw()
        hand.append(card)
        card_values += card.getValue()
        counter += 1
        if counter == 5:
            gameOn == False
        elif card_values >= 21:
            print(f"{hand},{card_values}")
            gameOn == False

main()

我正在终端中创建一个二十一点游戏来完成任务,我有几个问题?

  1. 在将卡片附加到
    .shuffle
    后,在
    temp_list
    方法中,它不再是
    Card
    类的实例。这是为什么?
  2. 我无法再在附加卡上使用 use Class 方法。
    card.getValue()
    是 NoneType
    while gameOn:
        card = deck.draw()
        hand.append(card)
        card_values += card.getValue()

当附加

.pop()
列表元素时,我期望返回一个卡片类实例,但是返回了卡片的信息,就像我调用了
__str__()
方法

python oop
1个回答
0
投票

问题 1:附加到

temp_list
后丢失卡片对象方法:

在循环中创建一个新的 Card 对象并将该实例附加到 temp_list:

def shuffle(self):
    counter = 0
    temp_list = []
    for i in range(52):
        randomNum = random.randrange(0, len(self.stack))
        new_card = Card(self.stack[randomNum].rank, self.stack[randomNum].suit)
        temp_list.append(new_card)
        self.stack.pop(randomNum)
    self.stack = temp_list

问题 2:card.getValue() 返回 NoneType:

将 Card 对象本身附加到

hand
:

while gameOn:
    card = deck.draw()
    hand.append(card)  # Append the Card object instance
    card_values += card.getValue()
© www.soinside.com 2019 - 2024. All rights reserved.