簡體   English   中英

蟒蛇游戲

[英]Python Uno Game

我正在嘗試玩 uno

試試這個: if __name__ == "main": main() ,現在我得到這個錯誤; NameError: 名稱 'Deck' 未定義

import random

class UnoCard:
    def __init__(self, c,n):
        self.color = c
        self.num = n
    def __str__(self):
        if self.c == 0:
            return "green" + str(self.n)
        if self.c == 1:
            return "yellow" + str(self.n)
        if self.c == 2:
            return "blue" + str(self.n)
        if self.c == 3:
            return "red" + str(self.n)
    def canPlay(self,other):
        if (self.num == other.num) or (self.color == other.color):
            return True
        return False 

class CollectionOfUnoCards:
    def __init__(self):
        self.l = []
    def addCard (self,c):
        self.l.append(c)
    def __str__(self):
        if(self.l):
            col_str = ""
            for i in range (0, len(self.l)):
                col_str = col_str + ' ' + str(self.l[i])
            return col_str
    def makeDeck(self):
        for num in range (1,10):
            for color in ["Yellow", "Red", "Blue", "Green"]:
                newcard = UnoCard(color,num)
                Deck.addCard(newcard)
                Deck.addCard(newcard)


    def shuffle(self):
        shuffle = random.randint(0,73)

    def getNumCards(self):
        return len(CollectionOfUnoCards)

    def getTopCard(self):
        return CollectionOfUnoCards[0]

    def canPlay(self, other):
        for card in self.l:
            if(card.canPlay(other)):
                return True
        return False        

    def getCard(self,index):
        return makeDeck(CollectionOfUnoCards)


class Uno:
    def __init__(self):
        self.Deck = CollectionOfUnoCards()
        self.Deck.makeDeck()
        self.Deck.shuffle()
        self.hand1 = CollectionOfUnoCards()
        self.hand2 = CollectionOfUnoCards()
        for i in range(7):
            self.hand1.Collection.append(deck.Collection.pop( ))
            self.hand1.Collection.append(deck.Collection.pop( ))

    def playGame(self):
        self.hand1.l.pop()
        self.lastPlayedCard = hand1.l.pop()
        while(True):
            self.playTurn(1)
            self.playTurn(2)
            if len(self.hand1.l) == 0 or len(self.hand2.l) == 0 :
                if len(self.hand1.l) == 0:
                    print("Player1 is winner")
                elif len(self.hand2.l) == 0:
                    print("player2 is winner")
                else:
                    print("The game ends in a draw")
                return False


    def playTurn(self, Player):
        if Player == 1:
            if canPlay(lastPlayedCard):
                hand1.pop()
                lastPlayedCard = hand1.pop()
            else: 
                hand1.Collection.append(deck.Collection.pop())
        else: 
            if canPlay(lastPlayedCard):
                hand2.pop()
                lastPlayedCard = hand2.pop()
            else: 
                hand2.Collection.append(deck.Collection.pop())

    def printResult(self):
        print (hand1)
        print (hand2)

def main():
    my_game = Uno()
    my_game.playGame()

我在 Windows 10 上用 Anaconda 編寫。程序在我運行時自動關閉。 我沒有得到任何輸出..

我認為你正在錯誤地構建甲板。

makeDeck 函數中沒有 Deck 變量。 相反,您想像這樣將卡添加到自身

def makeDeck(self):
    duplicates = 2
    for num in range (1,10):
        for color in ["Yellow", "Red", "Blue", "Green"]:
            newcard = UnoCard(color,num)
            for _ in range(duplicates):
                self.addCard(newcard)

此外, color必須是數字而不是字符串, self.c == 0行才能工作,實際上應該說self.color == 0 沒有self.cself.n

注意:UNO 有跳過、反向和通配符

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM