1

I built a deck of cards that can be shuffled. I built the player module and had the dealer class inherit the deck. (as the dealer is the one to shuffle the cards, rather than the deck of cards shuffling itself) I tried to import both into a main and have the dealer shuffle. Nope, things start breaking.

After a while, I started just doing trial and error, adding random ()'s and self's and whatever you can think of. Nothing worked. The main file is pretty much erased at this point, because I'm not just understanding anything from youtube or anything, really.

import random as rd


class Card:
    card_rank = [str(n) for n in range(2, 10)]
    card_rank.extend(['Ten', 'Jack', 'Queen', 'King', 'Ace'])
    card_suit = ['Spades', 'Clubs', 'Diamonds', 'Hearts']

    def __init__(self, rank, suit):
        assert 2 <= rank <= 14 and 1 <= suit <= 4
        self.rank = rank
        self.suit = suit

    def __str__(self):
        return '{} of {}'.format(Card.card_rank[self.rank - 2], Card.card_suit[self.suit - 1])


class Deck:
    def __init__(self):
        self.cards = [Card(rank, suit) for rank in range(2, 14 + 1) for suit in range(1, 4 + 1)]

    def is_empty(self):
        return not self.cards

    def pick(self):
        rd.shuffle(self.cards)
        return self.cards.pop()
import CardDeck


class Player:
    def __init__(self):
        self.hand = []


class Human(Player):
    def __init__(self):
        super(Human, self).__init__()
        self.name = input()


class Dealer(Player, CardDeck.Deck):
    def __init__(self):
        super(Dealer, self).__init__()

    def deck_shuffle(self):
        while not CardDeck.Deck.is_empty:
            print(CardDeck.Deck.pick)
>     import CardDeck as cd
>     import Players as pl
>     
>     
>     dealer = pl.Dealer
>     
>     
>     dealer.deck_shuffle

I want the dealer to shuffle the cards. I'm looking to see if it works by printing out the deck. I plan on doing more, but for now, I'm very stuck. (sorry for the formatting, this site confuses me on that)

Alec
  • 8,529
  • 8
  • 37
  • 63
Luck Box
  • 90
  • 1
  • 13

3 Answers3

2

The deck is a list so you can import random and use random.shuffle(deck) on the deck instance u create either global or in dealer's hand.

this is a black jack script from a course i was following it will help you alot. also it got some explanition to it.

Amr Bashir
  • 103
  • 1
  • 2
  • 12
1

You have missed the ( and ) in function calls, and some minor things in your Players.py file.

To make a function call, you need to put the function arguments after its name.

i.e. is_empty() and not is_empty

Also here

dealer = pl.Dealer ()

dealer.deck_shuffle ()
import CardDeck


class Player:
    def __init__(self):
        self.hand = []


class Human(Player):
    def __init__(self):
        super(Human, self).__init__()
        self.name = input()


class Dealer(CardDeck.Deck, Player):
    def __init__(self):
        super(Dealer, self).__init__()

    def deck_shuffle(self):
        while not self.is_empty():
            print(self.pick())

Changes to note:

  • You didn't initialize the Dealer because you didn't put ()
  • You didn't make the shuffle (same reason)
  • Your Player's deck_shuffle method should have called the functions on it's instance

Without the parenthesis, you are referencing the function/method, instead of calling it. e.g.

>>> dealer.deck_shuffle
<bound method Dealer.deck_shuffle of <Players.Dealer object at 0x7fcbdc2ff4e0>>

Same with the way you tried to call is_empty method.

>>> CardDeck.Deck.is_empty
<function Deck.is_empty at 0x7fcbdc297730>

If you try to call a method which isn't static, you'll get an exception:

>>> CardDeck.Deck.is_empty()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: is_empty() missing 1 required positional argument: 'self'

You need to have an instance of that object upon which the method will function.

hingev
  • 254
  • 1
  • 2
  • 7
0

If you want to keep the inheritance, you need to make sure you initialise all super classes.

In your Dealer class you call super(Dealer, self).__init__() which only initialises the Player superclass. Therefore the cards field from Deck is never created.

You either need to add an explicit Deck.__init__(self) call in the dealer's __init__ or add super().__init__() to Player.

For more information look at this other question.

Then, in your deck_shuffle you just call self.pick().

Edit:
In summary, the code should be:
CardDeck.py unchanged
Players.py:

import CardDeck


class Player:
    def __init__(self):
        # Added superclass initialisation (for cooperative multiple inheritance)
        super(Player, self).__init__()
        self.hand = []

# Unchanged
class Human(Player):
    def __init__(self):
        super(Human, self).__init__()
        self.name = input()


class Dealer(Player, CardDeck.Deck):
    def __init__(self):
        super(Dealer, self).__init__()

    def deck_shuffle(self):
        while not self.is_empty:
            print(self.pick())

and you call it like:

import Players as pl

dealer = pl.Dealer()
dealer.deck_shuffle()
Faboor
  • 1,365
  • 2
  • 10
  • 23
  • Despite all the help, I'm still not getting it. I've tried this and that from the answers here but I'm just too stupid to get this. – Luck Box Apr 30 '19 at 22:52