Python卡游戏错误

时间:2012-01-28 21:37:55

标签: python playing-cards

然后脚本运行但是我没有收到任何错误,而且它运行不正常。更具体地说,该程序应该处理卡片,然后制作一个字典,根据等级和套装给每张卡片一个值。卡片被洗牌,处理然后分类。曾经有过3C首发的人。计算机如何通过其卡片列表,如果它找到一个比之前的卡更高的值,它会播放它。好吧,有时工作,但如果我玩一张卡(通常是T(10)或更高),它不起作用,计算机播放一张价值更低的卡。感谢阅读:D

import random

class player():
    def __init__(self, hand, name):
        self.hand = hand
        self.passed = False
        self.name = name

#computer ai for playing        
def complay(player):
    if '3C' in player.hand:
        playCard(player, '3C')
        return
    else:
        print previous[-1]
        for i in player.hand:
            if i > previous[-1]:
                playCard(player, i)
                return
            else:
                print 'Nothing higher', i, key[i]

#human options for playing a card
def humplay(player):
    if preplayer[-1] == 'player1':
        print 'hi'
        return
    else:
        done = False
        low = 0
        while done == False:
            print player.hand
            if low > 1:
                choose = raw_input('That card is to low, what card do you want to play?\n')
            else:
                choose = raw_input('What card do you want to play?\n')
                if choose not in player.hand:
                    print 'not in'
                    continue
                if key[choose] > key[previous[-1]]:
                    playCard(player, choose)
                    done = True
                else:
                    low == 1


#Function for playing a card
def playCard(player, card):
    place = player.hand.index(card)
    playcard = player.hand.pop(place)

    print player.name, 'has played the', playcard
    previous.append(playcard)
    preplayer.append(player.name)

#sorts the cards in hand        
def sort(player):
    for n in range(len(player.hand)):
        for i in player.hand:
            place = player.hand.index(i)
            try:
                if key[i] > key[player.hand[place + 1]]:
                    player.hand.insert(place + 1, player.hand.pop(place))
            except IndexError:
                pass

def main():
    global previous
    global preplayer
    global key
    suits = 'CSHD'
    mark = '3456789TJQKA2'
    deck = []
    players = []
    previous = ['3l']
    preplayer = []

    for card in mark:
        for suit in suits:
            deck.append(card+suit)

    key = {}
    for i in range(len(deck)):
        key[deck[i]] = i

    key['3l'] = -1

    for i in deck:
        print i, '\t\t', key[i]

    random.shuffle(deck)
    player1 = player(deck[0::2], 'player1')
    player2 = player(deck[1::2], 'player2')
    player3 = player(deck[2::4], 'player2')
    player4 = player(deck[3::4], 'player3')

    players = [player1, player2, player3, player4]

    sort(player1)
    sort(player2)
    sort(player3)
    sort(player4)

    if '3C' in player1.hand:
        preplayer.append(player2.name)
    if '3C' in player2.hand:
        preplayer.append(player1.name)

    while len((player1.hand or player2.hand or player3.hand or player4.hand)) != 0:
        if preplayer[-1] == 'player1':
            complay(player2)
        if preplayer[-1] == 'player2':
            humplay(player1)


main()

1 个答案:

答案 0 :(得分:2)

要确定计算机是否应该播放卡片,请使用

for i in player.hand:
    if i > previous[-1]:
        playCard(player, i)
        return

但你的卡片只是“3C”和“TD”之类的字符串。所以当你使用“>”时比较,它将它们作为字符串进行比较。但是作为字符串'TD'> 'QD'等你应该写一个小函数来确定卡的价值并使用它的结果。

PS:你能看到下面的其他问题吗?

player1 = player(deck[0::2], 'player1')
player2 = player(deck[1::2], 'player2')
player3 = player(deck[2::4], 'player2')
player4 = player(deck[3::4], 'player3')