2017-04-20 13 views
1

私はちょっとブラックジャックゲームを作ろうとしています。別のファイルでplaying_cards.pyを呼び出しました。それには以下のような「デッキ」が含まれています。私の質問は、どのように(これはまだ問題を引き起こしている)文の束を持っていなくても、すべてこれを行うことができ、正しい数字を生成することです。 ( - リンク<)私はまだ初心者ですので、任意の助けがPythonブラックジャックゲーム

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

「カードと結果のマッピング関係」を定義した辞書を定義すると、 'compare/assign/print function'を一度呼び出すことができます。 – nick

答えて

0

代わりに(おそらく別のレッスンに来る)の辞書について教えあなたの素晴らしいだろう、私はあなたについてのstr.index()を教えてあげます。

あなたは大きな文字列内の部分文字列の最初のインデックスある整数値を返すために、str.index、またはstr.findを使用することができます。

ことの1つの用途は、単一の文字列でのカードのランクを配置することで、すべてのあなたの様々なif文を置き換えるために、次のようになります。

私は慎重にこの文字列を、目的に を構築しています
ranks = "..23456789TAJQK" 

お知らせ私はそれらが欲しいところの手紙。文字列の最初の文字にはインデックス0があります。ゼロを必要としないので、そこにドットを置きます。同様に、文字列の2番目の文字にはインデックス1があります。私が望むものではありません。私はインデックス2に '2'を、インデックス3に3を、その他にはインデックス10に 'T'を、インデックス11に 'A'を入れました。注文。

それでは、いくつかのテストデータをプリントアウトしてみましょう:私たちはJQK部分を除いて、軌道に乗っている意味

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

:それから、

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

を、私はこれを取得します。私はそれが十分に近いと思う:

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、ピクチャーカード、数字カードの場合は11、エースの場合は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

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がバストになるため、11ポイントではなく1ポイントの場合も処理できます。

関連する問題