#๐Ÿ”’ Working on blackjack to practice oop

134 messages ยท Page 1 of 1 (latest)

toxic arrow
#
import random
class Card:
    def __init__(self, rank, suit):
        self.rank = rank
        self.suit = suit

    def __str__(self):
        return f'{self.rank} of {self.suit}'

    def __repr__(self):
        return f'Card({self.rank!r}, {self.suit!r})'

class Deck:
    suits = ['hearts', 'spades', 'clubs', 'diamonds']
    ranks = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']

    def __init__(self):
        self.cards = []
        self.populate()

    def populate(self):
        for suit in Deck.suits:
            for rank in Deck.ranks:
                card = Card(rank, suit)
                self.cards.append(card)
    def shuffle(self):
        random.shuffle(self.cards)
    
    def deal(self):
        return self.cards.pop()
class Hand:
    def __init__(self):
        self.cards = []
    def add_card(self, card):
        self.cards.append(card)


deck = Deck()  # Make new deck
deck.shuffle()  # Shuffle it

hand = Hand()  # Make new hand

card = deck.deal()  # Deal card from deck
hand.add_card(card)  # Add that card to the hand
print(hand.cards)  # Display what cards are in the hand right now
        
regal tendonBOT
#

@toxic arrow

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

west storm
#

3rd post nice

toxic arrow
#

hehehe

#

dagger do you look forward to my posts or what

west storm
toxic arrow
#

yes

west storm
#

i have it give notification on post created as well

toxic arrow
#

ohh thats nice

west storm
toxic arrow
#

idk whats next

mild breach
#
  • deal a card from the deck to a hand
  • compare hands (highest wins?)
  • catch a card which overflows a hand?
#

A players list which is a list of Hands?

toxic arrow
#

how about a function that has total amount of points

west storm
#

now its piecing them together

#

u already pieced deck and card together

toxic arrow
#

yes

west storm
#

now piece hand and deck together

toxic arrow
#

what does that mean

#

maybe i was rushing to project too soon im starting to feel like

west storm
west storm
#

we have our pieces

#

the deck the cards and the hand (which is essentially a player for blackjack)

#

now we make the actual blackjack game

#

using said pieces

toxic arrow
#

ok

#

we need to count the value of the hand

west storm
toxic arrow
#

yes

west storm
#

then do we need to count it u think ? pithink

west storm
toxic arrow
#

in the hand class

#

below add_cards

#

logically it makes sense to me but then again im stoopid

west storm
toxic arrow
west storm
#

but sure that too

toxic arrow
#

ahahh

west storm
#

if it makes u feel better

toxic arrow
#

haahahhah

west storm
toxic arrow
#

no no

west storm
#

cause then why our poker hand is calculating its blackjack value instead of its poker value

#

or is it doing poker value

toxic arrow
#

wait waht

west storm
#

and if its doing poker value then what about hand in blackjack

toxic arrow
#

im confused

west storm
#

this is confusing behavior

toxic arrow
#

what am i supposed to do next?

west storm
#

go ahead

toxic arrow
#

ok

#
 def get_value(self):
        total = 0
        if card.rank in ['J', 'Q', 'K']:
            total += 10
        if card.rank == "A":
            total += 11
#

but

#

how would i make the ace either 1 or 11

#

or am i even doing this right

west storm
toxic arrow
#

ok 11 it is

west storm
#

there is simple and complicated/complex ways of doing things

#

but even then there is personal prefence bias playing a role

toxic arrow
#

but the problem is not being to differentiate the two

#

able to*

west storm
#

and example of being able to differentiate would be like a condition

#

if hand.value > 15: ace_value =1 else 11

#

for example

toxic arrow
#

ohh right right

west storm
#

so either pick one of pick a random condition

#

its ur game version of blackjack after all

toxic arrow
#

ok 11 it is

#
import random
class Card:
    def __init__(self, rank, suit):
        self.rank = rank
        self.suit = suit

    def __str__(self):
        return f'{self.rank} of {self.suit}'

    def __repr__(self):
        return f'Card({self.rank!r}, {self.suit!r})'

class Deck:
    suits = ['hearts', 'spades', 'clubs', 'diamonds']
    ranks = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']

    def __init__(self):
        self.cards = []
        self.populate()

    def populate(self):
        for suit in Deck.suits:
            for rank in Deck.ranks:
                card = Card(rank, suit)
                self.cards.append(card)
    def shuffle(self):
        random.shuffle(self.cards)
    
    def deal(self):
        return self.cards.pop()
class Hand:
    def __init__(self):
        self.cards = []
    def add_card(self, card):
        self.cards.append(card)
    
    def get_value(self):
        total = 0
        if card.rank in ['J', 'Q', 'K']:
            total += 10
        if card.rank == "A":
            total += 11
        if total >= 11 and card.rank == "A":
            total += 1


deck = Deck()  # Make new deck
deck.shuffle()  # Shuffle it

hand = Hand()  # Make new hand

card = deck.deal()  # Deal card from deck
hand.add_card(card)  # Add that card to the hand
print(hand.cards)  # Display what cards are in the hand right now
#

yeah somethings not it

west storm
#
        if card.rank == "A":
            total += 11
        if total >= 11 and card.rank == "A":
            total += 1
#

if card rank is A add 11

#

if ... and card rank is A: add 1

#

u got somewhat duplicated conditions

toxic arrow
#

yeah

#

how would i implement it

#

:/

west storm
#

ace value of 1 or 11 ?

toxic arrow
#

yeah

west storm
#

i already said it

#

either pick one and fuck the other

#

or pick a condition and use said condition to switch btw the 2

toxic arrow
#
def get_value(self):
        total = 0
        if card.rank in ['J', 'Q', 'K']:
            total += 10
        if card.rank == "A":
            total += 11
        ```
#

fuck it we roll

#

i dont like how i have 2 if's statments

west storm
toxic arrow
#

1

#

then elif

west storm
#

then do that

toxic arrow
#

then what would i have for else?

west storm
#

u dont need else

#
if ___:
    pass
elif ___:
    pass
#

this is a perfectly valid if statement

toxic arrow
#

ohh i thought if i used elif

#

i need an else

west storm
#

nope

#

speaking of else

#

there is loads of other places u can use it

#

like try/except blocks

#

if u know those

toxic arrow
#

i know of them

west storm
#

then there is the most random place imaginable

#

for loops

#

!e

for i in range(5):
    print(i)
else:
    print(':)')
regal tendonBOT
west storm
#

anyways thats very offtopic

west storm
toxic arrow
#

im getting burnt out not knowing wtf to do man

west storm
toxic arrow
#

return total

#

?

west storm
#

they not one sit programn for ~1h and be done

west storm
toxic arrow
#

Total?

#

!close

regal tendonBOT
#
Python help channel closed with !close

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.