2017-04-20 55 views
1

我正在嘗試做一個小二十一點遊戲,並且在一個單獨的文件中我已經調用了playing_cards.py,它包含具有以下內容的「deck」。我的問題是,如何在不需要大量if語句(仍然導致問題)的情況下執行所有這些操作並仍然生成正確的數字。我還是一個初學者,所以任何幫助將是美妙的Python Blackjack Game

deck = ['AH','2H','3H','4H','5H','6H','7H','8H','9H','TH','JH','QH','KH', 
    'AD','2D','3D','4D','5D','6D','7D','8D','9D','TD','JD','QD','KD', 
    'AS','2S','3S','4S','5S','6S','7S','8S','9S','TS','JS','QS','KS', 
    'AC','2C','3C','4C','5C','6C','7C','8C','9C','TC','JC','QC','KC'] 



import playing_cards 


player_hand = [] 
card = playing_cards.deal_one_card() 
player_hand.append(card) 
card = playing_cards.deal_one_card() 
player_hand.append(card) 

dealer_hand = [] 
card = playing_cards.deal_one_card() 
dealer_hand.append(card) 
card = playing_cards.deal_one_card() 
dealer_hand.append(card) 
print(dealer_hand) 

if player_hand[0][0] == "A": 
    player_hand[0] = 11 
    print(player_hand) 
elif player_hand[1][0] == "A": 
    player_hand[1] = 11 
    print(player_hand) 
else: 
    print(player_hand) 


if player_hand[0][0] == "2": 
    player_hand[0] = 2 
    print(player_hand) 
elif player_hand[1][0] == "2": 
    player_hand[1] = 2 
    print(player_hand) 
else: 
    print(player_hand) 


if player_hand[0][0] == "3": 
    player_hand[0] = 3 
    print(player_hand) 
elif player_hand[1][0] == "3": 
    player_hand[1] = 3 
    print(player_hand) 
else: 
    print(player_hand) 


if player_hand[0][0] == "4": 
    player_hand[0] = 4 
    print(player_hand) 
elif player_hand[1][0] == "4": 
    player_hand[1] = 4 
    print(player_hand) 
else: 
    print(player_hand) 


if player_hand[0][0] == "5": 
    player_hand[0] = 5 
    print(player_hand) 
elif player_hand[1][0] == "5": 
    player_hand[1] = 5 
    print(player_hand) 
else: 
    print(player_hand) 


if player_hand[0][0] == "6": 
    player_hand[0] = 6 
    print(player_hand) 
elif player_hand[1][0] == "6": 
    player_hand[1] = 6 
    print(player_hand) 
else: 
    print(player_hand) 


if player_hand[0][0] == "7": 
    player_hand[0] = 7 
    print(player_hand) 
elif player_hand[1][0] == "7": 
    player_hand[1] = 7 
    print(player_hand) 
else: 
    print(player_hand) 


if player_hand[0][0] == "8": 
    player_hand[0] = 8 
    print(player_hand) 
elif player_hand[1][0] == "8": 
    player_hand[1] = 8 
    print(player_hand) 
else: 
    print(player_hand) 


if player_hand[0][0] == "9": 
    player_hand[0] = 9 
    print(player_hand) 
elif player_hand[1][0] == "9": 
    player_hand[1] = 9 
    print(player_hand) 
else: 
    print(player_hand) 


if player_hand[0][0] == "T": 
    player_hand[0] = 10 
    print(player_hand) 
elif player_hand[1][0] == "T": 
    player_hand[1] = 10 
    print(player_hand) 
else: 
    print(player_hand) 


if player_hand[0][0] == "J": 
    player_hand[0] = 10 
    print(player_hand) 
elif player_hand[1][0] == "J": 
    player_hand[1] = 10 
    print(player_hand) 
else: 
    print(player_hand) 


if player_hand[0][0] == "Q": 
    player_hand[0] = 10 
    print(player_hand) 
elif player_hand[1][0] == "Q": 
    player_hand[1] = 10 
    print(player_hand) 
else: 
    print(player_hand) 


if player_hand[0][0] == "K": 
    player_hand[0] = 10 
    print(player_hand) 
elif player_hand[1][0] == "K": 
    player_hand[1] = 10 
    print(player_hand) 
else: 
    print(player_hand) 
+0

您可以定義一個字典,在該字典中定義'card to result mapping relation',然後可以調用'compare/assign/print function'一次。 – nick

回答

0

而是教你關於詞典(這可能是在一個不同的課),我會教你str.index()(< - 鏈接)。

您可以使用str.index,或str.find,返回的整數值,是一個較大的字符串中的子字符串的開頭的指數

一個用途的,將是更換所有你的各種if語句,通過在單一字符串安排卡行列:

ranks = "..23456789TAJQK" 

注意到我精心打造的這串,的目的,與我想要他們的信件。字符串的第一個字符有索引0:我在那裏放了一個點,因爲我不想要零。同樣,字符串的第二個字符的索引爲1:不是我想要的。所以我把'2'放在索引2處,把3放在索引3處等等。我把'T'放在索引10處,把'A'放在索引11處。然後,我把所有其他的東西放在訂購。

現在,讓我們打印出一些測試數據:

print('2', ranks.index('2')) 
print('T', ranks.index('T')) 
print('A', ranks.index('A')) 
print('Q', ranks.index('Q')) 

從這一點,我得到這個:

$ python test.py 
2 2 
T 10 
A 11 
Q 13 

這意味着我們正在軌道上,除了JQK一部分。我認爲這是非常接近:

rank = ranks.index(player_hand[0][0]) 

if rank > 11: # J,Q,K 
    rank = 10 

player_hand[0] = rank 

rank = ranks.index(player_hand[1][0]) 

if rank > 11: # J,Q,K 
    rank = 10 

player_hand[1] = rank 
0

首先,我想你在甲板上的定義更改爲類似以下,不緊湊得多,但很多容易檢查,沒有卡或訴訟已經錯過(和也許更容易修改):

suits = ['H', 'D', 'S', 'C'] 
cards = ['A', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K'] 
deck = [card + suit for suit in suits for card in cards] 

我不完全清楚你正在嘗試接下來要做的,但似乎你使用它您還沒有一個deal_one_card()方法,然後試圖讓處理卡卡的價值('10'或一張圖片卡的10,一張數字卡的號碼,以及一張ace的11張)。我認爲,最簡單的方法可能會定義點的詞典:

points_dictionary = {'A': 11, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, 'T': 10, 'J': 10, 'Q': 10, 'K': 10} 

然後,你可以查找特定卡的點,例如:

import random 
a_card = random.choice(deck) 
print(a_card) 
print(points_dictionary[a_card[0]]) 

雖然明明自己deal_one_card()方法需要跟蹤一張已經發出的卡片,因此已經不在包裝中。

0

使用dictioanry,您可以在其中映射A,K,Q,J,T和數字的值。事情是這樣的:

CARD_VALUES = { 
    "A" : 11, 
    "K" : 10, 
    "Q" : 10, 
    "J" : 10, 
    "T" : 10 
} 

你可以得到這樣的映射,

card = int(CARD_VALUES.get(player_hand[0][0], player_hand[0][0])) 
0

我會用這樣的:

suits = [ 'C', 'D', 'H', 'S' ] 
cards = [ 'A', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K' ] 
rank_values = { "A": (1, 11), "2": (2,), "3": (3,), "4": (4,), "5": (5,), "6": (6,), "7": (7,), "8": (8,), "9": (9,), "J": (10,), "Q": 10, "K": 10 } 

class Card(object): 
    def __init__(self, card_value): 
     self._card_value = card_value.upper() 

    @property 
    def suit(self): 
     return self._card_value[ 0 ] 

    @property 
    def rank(self): 
     return self._card_value[ 1 ] 

    @property 
    def values(self): 
     return sorted(rank_values[ self.rank ], reverse = True) 

def get_deck(): 
    deck_values = [ card + suit for suit in suits for card in cards ] 
    deck = [ Card(value) for value in deck_values ] 
    return deck 

這使您可以撥打get_deck()檢索的甲板卡和每張卡有一個values財產(作爲c.values訪問假設卡c),它返回的所有值的CA rd可以容納。這使您可以處理Ace是1點的情況,而不是11點,因爲它會破裂。