简体   繁体   中英

BlackJack Class Difficulties

I'm trying to simulate the dealer of a game of blackjack, and I'm really confused.

Basically, I'm using a deck of cards that only consists of J, Q, K, A, and 2-10 to simplify this stuff, rather than using an ordinary card deck setup.

I realize all of this is pretty messy, I've been working with classes for all of 3 hours.

class Deck:
    '''Simulates a deck of cards.'''

    def __init__(self):
        '''Deck() -> Deck
        Creates a deck of cards'''
        self.Deck = ['J', 'J', 'J', 'J', 'Q', 'Q', 'Q', 'Q', 'K', 'K', 'K', 'K', 'A', 'A', 'A', 'A', '2', '2', '2', '2', '3', '3', '3', '3', '4', '4', '4', '4', '5', '5', '5', '5', '6', '6', '6', '6', '7', '7', '7', '7', '8', '8', '8', '8', '9', '9', '9', '9', '10', '10', '10', '10']

    def __str__(self):
        '''str(Deck) -> str
        Returns string showing number of cards left'''
        output = str(len(self.Deck)) + ' cards'
        return(output)

    def shuffle(self):
        '''shuffle(self) -> Deck
        Shuffles deck.'''
        import random
        random.shuffle(self.Deck)

     def draw(self):
        if len(self.Deck) > 0:
            return(self.Deck.pop(0))
        else:
            return(-1)

    def add(self, card):
        self.Deck.append(str(card))

class BlackJackPlayer(Deck):
    '''Represents someone playing blackjack.'''

    def __init__(self, Deck):
        self.BlackJackPlayer = Deck
        self.hand = ''
        self.value = 0

    def __str__(self):
        return self.hand

    def clean_up(self):
        self.BlackJackPlayer = Deck
        self.hand = ''
        self.value = 0

    def get_value(self):
        for x in self.hand:
            if x != ',':
                if x not in range(1, 11):
                    if x != 'A':
                        self.value += 10
                    else:
                        if (self.value + 11) > 17:
                            self.value += 1
                        else:
                            self.value += 11
                else:
                    self.value += x
        return(self.value)

    def hit(self):
        Deck.shuffle(self.BlackJackPlayer)
        card = Deck.draw(self.BlackJackPlayer)
        return(card)

    def play_dealer(self):
        while BlackJackPlayer.get_value(self) < 17:
            self.hand += BlackJackPlayer.hit(self)
            print(self.hand)
            if BlackJackPlayer.get_value(self) < 17:
                self.hand += ','
        if BlackJackPlayer.get_value(self) > 21:
            return('BUST')
        else:
            return

For some reason, whenever I call jack.play_dealer(), it'll stop too early because it thinks the hand is already over 21.

For example:

>>> deck = Deck()
>>> jack = BlackJackPlayer(deck)
>>> jack.play_dealer()
7
'BUST'
>>> jack.get_value()
40

I'm guessing something's wrong with my get_value(), but I can't figure it out.

Thanks in advance!

Your Deck cards are all strings:

self.Deck = ['J', 'J', 'J', 'J', 'Q', 'Q', 'Q', 'Q', 'K', 'K', 'K', 'K', 'A', 'A', 'A', 'A', '2', '2', '2', '2', '3', '3', '3', '3', '4', '4', '4', '4', '5', '5', '5', '5', '6', '6', '6', '6', '7', '7', '7', '7', '8', '8', '8', '8', '9', '9', '9', '9', '10', '10', '10', '10']

Note the '2' and '3' , etc. strings.

Yet you try to treat them as integers:

for x in self.hand:
    if x != ',':
        if x not in range(1, 11):

x will never be in range(1, 11) , because all your values are strings. So the above if statements are always True .

The next part is:

if x != 'A':
    self.value += 10
else:
    if (self.value + 11) > 17:
        self.value += 1
    else:
        self.value += 11

When 'number' cards are drawn, the not in range(1, 11) is still true, they are not equal to 'A' , so you count all cards as value 10.

You'll need to rethink how to handle the number cards here. You can test for alphanumeric ordering:

if x >= 'A':

would work for your numeric cards too, as all numbers sort before capitals in the ASCII standard. Use that instead of the not in range(1, 11) test.

Next problem is that you are then trying to add up the value using strings when the cards are numeric:

else:
    self.value += x

but that adds up strings; a string value is always greater than numbers (in Python 3, comparing strings and numbers raises an error).

Use int() to sum actual numbers:

else:
    self.value += int(x)

Now at least you have something working:

>>> deck = Deck()
>>> jack = BlackJackPlayer(deck)
>>> jack.play_dealer()
A
A,A
A,A,9
'BUST'
>>> jack.get_value()
60

But this is still too high. That's because you use self.value to store the calculation but never reset it . You keep on adding for each new calculation.

Use a local variable instead of self.value :

def get_value(self):
    value = 0
    for x in self.hand:
        if x >= 'A':
            if x != 'A':
                value += 10
            else:
                if value > 6:
                    value += 1
                else:
                    value += 11
        else:
            value += int(x)

    return value

Now the game works as expected:

>>> jack = BlackJackPlayer(Deck())
>>> jack.play_dealer()
J
JA
JAA
JAA6

Here's how you should replace the logic for getting the score. The problem is that your treating strings as integers and that you reevaluate the entire hand without resetting the value.

def get_value(self):
    self.value = 0 # reset for reevaluation
    aces11 = 0
    for x in self.hand.split(','):
        x = x.strip() # strip off excess whitespace
        if x in '2345678910': # numeric
            self.value += int(x)
        elif x == 'A': #An ace
            self.value += 11
            aces11 += 1
        else: # a face card
            self.value += 10
        #check to reduce aces if greater than 17
        while self.value>17 and aces11>0:
            aces11 -= 1
            self.value -= 10
    return(self.value)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM