2013-12-09 23 views
2

我正在爲我的項目製作大酒杯遊戲。我在列表中製作了一副牌,包括隊伍和套裝,但是當我嘗試添加玩家的手時,我只有一個值返回,而不是總和。爲大酒杯添加列表組件

我不知道如何管理它。首先,我必須得到列表中的所有元素,然後刪除西裝以獲取數字,但我的一些數字是字母(傑克,王后,王,王牌...),並具有特定的價值。我怎樣才能將它們加在一起?我嘗試着和其他事物一起,但徒勞無功。

有關如何實現此目的的任何提示?

這裏是我的代碼示例:

def create_deck(): #this creates the deck of cards 
    suit_string = 'hdcs' 
    rank_string = '23456789TJQKA' 
    global deck 
    deck = [] 
    for suit in range(4): 
     for suit in range(13): 
      cards = rank_string[rank] + suit_string[suit] 
      deck.append(cards) 
      random.shuffle(deck) 
    return deck 

def deal_cards(): #This takes one card from the deck and gives it to the player 
    return deck.pop(0) 

def hand(): #Initial two cards 
    global player1_hand 
    player1_hand = [] 
    print "Player's hand:" 
    for i in range(2): #initial cards 
     player1_hand.append(deal_cards()) 
    print player1_hand 
    print value() 

def value(): 
    i = 0 
    while i < len(player1_hand): 
     card = player1_hand[i] 
     value = card[i] 
     if value in ('T','J','Q','K'): 
      return 10 
     elif value == 'A': 
      print "Please choose between 1 and 11." 
      value_a = input("---> ") 
      return value_a 
     else: 
      return value 
    i += 1 

,現在這是它給了我:

Player's hand: 
['Ks','Th'] 
Value 10 

我知道,我真的不添加值在一起,但我不知道如何管理它。

任何幫助,將不勝感激。

希望我很清楚,因爲英語不是我的主要語言。

+0

創建一個持有該卡的等級和套裝的「卡牌」類別/名稱爲「tuple」。然後,只需編寫一個計算卡片值的'get_value(card)'函數。 – Blender

+0

我不知道如何用班級管理。我對python相當陌生,請你能更深入地解釋一下嗎? – CarlD

+0

也請包括您的if __name__ ==「__main__」語句或您用來調用您的函數的任何其他內容。 – asdf

回答

0

通過使用「等級值」的dict你可以通過玩家的手環,總起來的值。

def value(): 
    hand_total = 0 
    # Add in a dict of "rank values" (excluding the Ace) 
    rank_values = {'2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, 
        '9':9, '10':10, 'J':10, 'Q':10, 'K':10} 
    # No need for the while loop 
    for i in xrange(len(player1_hand)): 
     card = player1_hand[i][0] # the added index will get the card   
     if card == 'A':   # without the suit. 
      print "Please choose between 1 and 11." 
      value_a = input("---> ") 
      hand_total += int(value_a) # add a type check before this 
     else: 
      hand_total += rank_values[card] 
    return hand_total 

使用dict你不會有花牌或卡編號來區分,只需要使用鑰匙(卡)值和總金額。

+0

感謝大家的幫助,現在一切正常!我試過每種方法,他們都運行良好。我可能會使用這個。 – CarlD

0

你已經寫了一個函數value(),看起來它的目的是計算總價值player1_hand。但是,這個功能只是返回它看到的第一張牌的值:

while i < len(player1_hand): 
    if value in ('T','J','Q','K'): 
     return 10 
    elif value == 'A': 
     ... 
     return value_a 
    else: 
     ... 
     return value 

return語句導致value()立即返回,而無需通過循環仍在繼續。我想你想在這裏做的是有循環的每一步中卡的價值增加了​​手的總價值:

total_value = 0 
for card in player1_hand: 
    rank = card[0] 
    if rank in ('T', 'J', 'Q', 'K'): 
     total_value += 10 
    elif rank == 'A': 
     print "Please choose between 1 and 11." 
     value_a = input("---> ") 
     total_value += int(value_a) 
    else: 
     total_value += int(rank) 
return total_value 
0

玉以及首先第一件事情有一個在你的代碼的一些錯誤,這可能是造成一些問題。

1)在你的create_deck方法中,兩個for循環都使用變量suit作爲他們的迭代器,我假設第一個應該設置爲rank而不是?

2)您應該使用return語句結束所有方法,即使它們不返回任何內容。這將迫使該方法退出並且是良好的編碼實踐。

好的,所以要解決你的問題,你應該讓你的價值方法看起來像這樣。在返回手牌總值之前,您現在退出該方法,只返回一個值。爲了解決這個創建while循環外的全球價值和價值元素添加到它:

def value(): 
    i = 0 
    total_value = 0 
    while i < len(player1_hand): 
     card = player1_hand[i] 
     value = card[i] 
     if value in ('T','J','Q','K'): 
      total_value += 10 
     elif value == 'A': 
      print "Please choose between 1 and 11." 
      value_a = input("---> ") 
      total_value += value_a 
     else: 
      total_value += value 
    i += 1 
    print(value) 
    return