python - BlackJack Class Difficulties -


i'm trying simulate dealer of game of blackjack, , i'm confused.

basically, i'm using deck of cards consists of j, q, k, a, , 2-10 simplify stuff, rather using ordinary card deck setup.

i realize of pretty messy, i've been working classes of 3 hours.

class deck:     '''simulates deck of cards.'''      def __init__(self):         '''deck() -> deck         creates 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 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):         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 reason, whenever call jack.play_dealer(), it'll stop because thinks hand on 21.

for example:

>>> deck = deck() >>> jack = blackjackplayer(deck) >>> jack.play_dealer() 7 'bust' >>> jack.get_value() 40 

i'm guessing something's wrong get_value(), can't figure out.

thanks in advance!

your deck cards 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 '2' , '3', etc. strings.

yet try treat them integers:

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

x never in range(1, 11), because values strings. above if statements 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 drawn, not in range(1, 11) still true, not equal 'a', count cards value 10.

you'll need rethink how handle number cards here. can test alphanumeric ordering:

if x >= 'a': 

would work numeric cards too, numbers sort before capitals in ascii standard. use instead of not in range(1, 11) test.

next problem trying add value using strings when cards numeric:

else:     self.value += x 

but adds strings; string value always greater numbers (in python 3, comparing strings , numbers raises error).

use int() sum actual numbers:

else:     self.value += int(x) 

now @ least have working:

>>> deck = deck() >>> jack = blackjackplayer(deck) >>> jack.play_dealer() a,a a,a,9 'bust' >>> jack.get_value() 60 

but still high. that's because use self.value store calculation never reset it. keep on adding each new calculation.

use local variable instead of self.value:

def get_value(self):     value = 0     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 game works expected:

>>> jack = blackjackplayer(deck()) >>> jack.play_dealer() j ja jaa jaa6 

Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -