简体   繁体   English

尝试制作 UNO 游戏但无法处理玩家手牌

[英]Trying to make an UNO game but cant deal player hands

In the dealCards function i tried to take 7 random cards from the deck list and append it to the hand1.在 dealCards function 中,我尝试从牌组列表中随机抽取 7 张牌,然后将 append 拿到手上 1。 But when i call the function, i indeed get 7 cards but not as string, i get them like this: "< main .UnoCard object at 0x00000202E461DF10>" How can i fix this issue?但是当我调用 function 时,我确实得到了 7 张卡片但不是字符串,我得到它们是这样的:“< main .UnoCard object at 0x00000202E461DF10>” 我该如何解决这个问题?

import random

class UnoCard:
    def __init__(self, color, num):
        self.c = color
        self.n = num

    def __str__(self):
        return self.c + " " + str(self.n)

    def canplay(self, other):
        if self.c == other.c or self.n == other.n:
            return True
        else:
            return False


class CollectionOfUnoCards:
    def __init__(self):
        self.list1 = []

    def addCard(self, c):
        self.list1.append(c)

    def __str__(self):
        s = ""
        for card in self.list1:
            s += str(card) + "|"
        return s

    def makeDeck(self):
        colors = ["Yellow", "Blue", "Red", "Green"]
        numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]

        for i in range(1, 3):
            for color in colors:
                for number in numbers:
                    card = UnoCard(color, number)
                    self.addCard(card)

    def shuffle(self):
        for i in range(1, 400):
            random_value = random.randint(0, 71)
            deck.list1[0], deck.list1[random_value] = deck.list1[random_value], deck.list1[0]

    def dealCards(self):
        hand1 = []
        for i in range(1, 8):
            random_value = random.randint(0, 71)
            hand1.append(deck.list1[random_value])
        print(hand1)

    def getNumCards(self):
        print(len(deck.list1))

    def getTopCard(self):
        print(deck.list1[-1])


deck = CollectionOfUnoCards()

deck.makeDeck()

deck.shuffle()

print(deck)



length = CollectionOfUnoCards()

length.getNumCards()

print(length)


last_card = CollectionOfUnoCards()

last_card.getTopCard()

print(last_card)



player1 = CollectionOfUnoCards()

player1.dealCards()

print(player1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM