2012-01-28 72 views
-1

那麼腳本運行,但我沒有得到任何錯誤,它不能正常工作。更具體地說,該程序應該髮卡,然後製作一個字典,根據等級和花色給每張卡片一個值。牌洗牌,處理,然後排序。誰曾經從3C開始。計算機如何選擇卡片是通過它的卡片列表,如果它找到一個具有較高值的​​卡片,則它將播放它。那麼有時候有效,但如果我玩一張卡(通常是T(10)或更高),它不起作用,電腦會播放一張價值較低的卡。感謝您的閱讀:dPython卡片遊戲錯誤

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() 
+3

縮進代碼,你可以選擇區域和打大括號鍵「{}」(或控制類型-K)。 – DSM 2012-01-28 21:39:45

+0

您可以隨時在文本編輯器和標籤所做的一切輸入您的密碼,給它是正確的格式。 – Makoto 2012-01-28 21:40:46

+0

它必須是一個文本編輯器,它可以將製表符轉換爲空格,例如emacs – 2012-01-28 22:08:56

回答

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') 
+0

>。<對不起,原本應該是(可以在註釋中執行代碼)for我在player.hand中:if key [i]>鍵[上一個[-1]]><我很抱歉任何經歷過這個的人。儘管感謝您的快速回復。 – GnarGnar 2012-01-28 22:33:39