簡體   English   中英

如何在二十一點游戲中打印手中的價值

[英]How to print the Value in hand in game of BlackJack

在 BlackJack 游戲中,我試圖獲取手牌值的總和並打印出來。 我想知道我的代碼中的錯誤保持與我正在使用的相同的邏輯。

我正在從 Deck 類的 init_deal() 方法返回一個元組,並在下一個類(即 Hand)中調用它。 在 Hand 類的 add() 函數中,我試圖將 Cards 的值相加以計算手頭的總值。

import random

suits = ('Hearts', 'Diamonds', 'Spades', 'Clubs')
ranks = ('Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 
'Ten', 'Jack', 'Queen', 'King', 'Ace')
values = {'Two':2, 'Three':3, 'Four':4, 'Five':5, 'Six':6, 'Seven':7, 
'Eight':8, 'Nine':9, 'Ten':10, 'Jack':10, 'Queen':10, 'King':10, 'Ace':11}

class Card:    
    def __init__(self,suit,rank):
        self.suit= suit
        self.rank= rank        
    def __str__(self):
        return (f'{self.rank} of {self.suit}')

class Deck:    
    def __init__(self):
        self.deck=[]
        self.computer=[]
        self.player=[]
        for suit in suits:
            for rank in ranks:
                self.deck.append(Card(suit,rank))

    def init_deal(self):
        count=1
        while count<5:
            if count%2==0:
                comp=self.deck.pop()
                self.computer.append(comp)
                count+=1                
            elif count%2!=0:
                playr=self.deck.pop()
                self.player.append(playr)
                count+=1
            else:
                break
        return (self.computer,self.player)

class Hand(Deck):
    def __init__(self):
        self.hand=Deck().init_deal()
        self.hand_com=self.hand[0]
        self.hand_playr=self.hand[1]
        self.value_comp = 0 
        self.value_player = 0
        self.val_comp=''
        self.val_player=''        
        self.aces = 0

    def add(self):
        self.val_comp=' '.join(val.split()[0] for val in self.hand_com)
        self.val_player=' '.join(val.split()[0] for val in self.hand_playr)
        self.val_comp_lst=self.val_comp.split()
        self.val_player_lst=self.val_player.split()
        print(self.val_comp_lst)
        print(self.val_player_lst)

        for val in self.val_comp_lst:
            self.value_comp += values[val]
        for val in self.val_player_lst:
            self.value_player += values[val]
        return (self.val_comp,self.val_player)
        print(self.value_comp)
        print(self.value_player)

    def __str__():
        pass #Some logic here

x=Deck()
x.init_deal()
y=Hand(x)
y.add()

讓我們假設

Deck().init_deal()

返回

(['Two of Hearts','Eight of Diamonds'],['Five of Spades','Six of Diamonds'])

所以根據我的代碼的預期結果:

self.hand_com=['Two of Hearts','Eight of Diamonds']
self.hand_playr=['Five of Spades','Six of Diamonds']

val_comp_lst=['Two','Eight']
val_player_lst=['Five','Six']

所以最終的預期結果(實際上我無法打印):

self.value_comp = 10
self.value_player = 11

但是現在,如果我運行代碼,我會收到錯誤消息“AttributeError: 'Card' object has no attribute 'split'”

請幫助我理解我在這段代碼中犯的錯誤

您可以使用字典將單詞映射到數值,然后使用列表 comp 和 sum 來總結這些值:

val_comp_lst=['Two','Eight'] 

nums = {'One': 1, 'Two': 2 ...}

sum([nums[x] for x in val_comp_lst])

Card添加value屬性可能是最簡單的。

class Card:
    _values = {1: 11, 11: 10, 12: 10, 13: 10}
    def __init__(self, suit, rank):
        self.suit= suit
        self.rank= rank        
    def __str__(self):
        return (f'{self.rank} of {self.suit}')

    @property
    def value(self):
        # returns the value from _values, or the rank of the card

        return self._values.get(self.rank, self.rank)

然后將手中牌的value相加。 但是請注意,A 被計算為 111,因此您之后可能不得不捏造這些數字!


旁白:我對你的班級結構感到困惑。 您編寫了class Hand(Deck) ,但這意味着HandDeck ,這似乎不太可能。 你確定這是你想要的嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM