0

我这里有将卡片从牌堆移动到基础堆的代码。我已经导入了必要的细节等。我的问题是,它太长了。有什么办法可以让它变短吗?如何?谢谢 :)

def dtof():
    suit = raw_input("enter suit: ")
    v = trash.pop()
    if suit == "D":
        if card.suitNumber[v.suit] == 1:
            if card.rankNumber[v.rank] == 0:
                Diamond.append(v)
            elif card.rankNumber[v.rank] == card.rankNumber[Diamond[-1].rank] + 1:
                Diamond.append(v)
            else:
                trash.append(v)
            return Diamond[-1]
        else:
            trash.append(v)
    elif suit == "H":
        if card.suitNumber[v.suit] == 2:
            if card.rankNumber[v.rank] == 0:
                Heart.append(v)
            elif card.rankNumber[v.rank] == card.rankNumber[Heart[-1].rank] + 1:
                Heart.append(v)
            else:
                trash.append(v)
            return Heart[-1]
        else:   
            trash.append(v)
    elif suit == "C":
        if card.suitNumber[v.suit] == 4:
            if card.rankNumber[v.rank] == 0:
                Clubs.append(v)
            elif card.rankNumber[v.rank] == card.rankNumber[Clubs[-1].rank] + 1:
                Clubs.append(v)
            else:
                trash.append(v)
            return Clubs[-1]
        else:
            trash.append(v)
    elif suit == "S":
        if card.suitNumber[v.suit] == 3:
            if card.rankNumber[v.rank] == 0:
                Spade.append(v)
            elif card.rankNumber[v.rank] == card.rankNumber[Spade[-1].rank] + 1:
                Spade.append(v)
            else:
                trash.append(v)
            return Spade[-1]
        else:
            trash.append(v)
    else:
        trash.append(v)
4

1 回答 1

0

考虑将 Diamond、Heart、Clubs 和 Spade 合并到一个字典中,键是花色。

于 2013-03-09T09:37:49.520 回答