Python Forum
More of my learning
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
More of my learning
#3
The way you're using a class is really just as a sexier dict.  In other languages, it'd be a nice little struct/typedef.  Classes are (in my opinion) best used to bundle functionality together.  If you'll notice, your playerhit() and cpuhit() functions are almost exactly the same, except the values they use to make a decision are different.  Which means I'd probably start just by changing those two functions to instead be a single function.  

Also, deck has instances of Card, but instead of re-using those instances (that you already have, right there!), you create brand new Card instances.  ...why?

I don't think the Card should be the thing you need a class for, I think a player is what you need a class for.  Something like:
#...this maybe should be called Hand, since it doesn't clear itself...
class Player(object):
    def __init__(self):
        # maybe keep track of how many chips they start with?
        self.cards = []

    def current_total(self):
        total = 0
        for card in self.cards:
            total += card.value

        return total

    def hit(self, new_card):
        self.cards.append(new_card)

    def should_hit(self, dealer_showing=0):
        total = self.current_total()
        # not actually basic strategy...
        if dealer_showing <= 12:
            return total <= 11
        return total <= 16
Then you'd have two or more instances of Player, which each keep track of whatever cards they're dealt and use that information to decide for themselves if they should keep getting more cards:
players = [Player() for _ in range(5)] # it's full table, why not
dealer = Player()
dealer.hit(pick_a_card())
for player in players:
    player.hit(pick_a_card())

dealer_total = dealer.current_total()
for player in players:
    while player.should_hit(dealer_total):
        player.hit(pick_a_card())
    player_total = player.current_total()

while dealer.should_hit():
    dealer.hit(pick_a_card())
dealer_total = dealer.current_total()

for player in players:
    if player_total > dealer_total:
        # winner!
    # don't forget to handle busts... and the dealer's hidden card
Now, let's talk about those card pics... They're really all the same, except you change a single character.  I hate (and you should too!) seeing the same thing over and over.  If you find yourself doing the copy-paste dance, there's probably a better way to do it.  And there is.  In this instance, probably string formatting, with a base of what all cards look like, using a placeholder for the actual value of the card.  Maybe...
>>> card_template = '''
... |---|
... | {0:2}|
... |---|'''

>>> cards = [str(i) for i in range(2, 11)] + list('AKQJ')
>>>
>>> cards
['2', '3', '4', '5', '6', '7', '8', '9', '10', 'A', 'K', 'Q', 'J']
>>> for card in cards:
...   print(card_template.format(card))
...
Which would give you...
Output:
|---| | 2 | |---| --snip-- |---| | 10| |---| |---| | A | |---| |---| | K | |---| --snip--
Reply


Messages In This Thread
More of my learning - by LordHill - Dec-01-2016, 05:37 AM
RE: More of my learning - by stranac - Dec-01-2016, 09:10 AM
RE: More of my learning - by nilamo - Dec-01-2016, 08:50 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020