纸牌蟒蛇

时间:2013-03-09 05:51:00

标签: python

我这里有一个代码,用于将卡片从卡座移到基础桩上。我已经导入了必要的细节等等。我的问题是它太长了。有没有办法让它变短?怎么样?谢谢:))

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)

1 个答案:

答案 0 :(得分:0)

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