0

How could I call the shuffle and takecard methods, belonging to the Deck class and call it in the Dealer class without inheriting the whole class.

#!/usr/bin/python
import random
class Card:
 """A card object with a suit and rank."""
 RANKS = (2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14)
 SUITS = ('Spades', 'Diamonds', 'Hearts', 'Clubs')
 def __init__(self, rank, suit):
 self._rank = rank
 self._suit = suit
 def getRank(self):
 return self._rank
 def getSuit(self):
 return self._suit
 def __str__(self):
 translate = {11:'Jack', 12:'Queen', 13:'King', 14: 'Ace'}
 r = self._rank
 if r in [11, 12, 13, 14]:
 myrank = translate[r]
 else:
 myrank = str(r)
 return myrank + " of " + self._suit
 def __lt__(self, other):
 return( self._rank < other.getRank() )
class Deck:
 def __init__(self):
 self._cards = []
 for suit in Card.SUITS:
 for rank in Card.RANKS:
 c = Card(rank, suit)
 self._cards.append(c)
 def shuffle(self):
 random.shuffle(self._cards)
 def takecard(self):
 if len(self._cards) == 0:
 return None
 else:
 #print self._cards.pop()
 return self._cards.pop()
 def __len__(self):
 return len(self._cards)
 def __str__(self):
 result = ""
 for c in self._cards:
 result = result + str(c) + '\n'
 return result
class Dealer(object):
 def __init__(self, name):
 self.name = name
 def shuffle(self):
 deck=self.deck
 self.deck.shuffle() 
 def deal(self):
 self.deck.takecard()

self.deck.takecard and self.deck.shuffle doesn't return the Deck methods if I declare it this way: Dealer('Bob',Deck)

Andrew
3,8812 gold badges23 silver badges17 bronze badges
asked Mar 16, 2015 at 1:12
2
  • You need to create an instance of Deck and assign it to an attribute of an instance of Dealer. The logical place to do this is inside the Dealer.__init__() method. It could create the instance of Deck itself, by calling Deck(), or the result of doing so could be passed to it as an argument. In addition, you don't really need the deck=self.deck in Dealer.shuffle() and Dealer.deal() should probably return self.deck.takecard(). Commented Mar 16, 2015 at 4:20
  • Thank you for the prompt reply I am calling it this way. Commented Mar 16, 2015 at 17:29

2 Answers 2

1

If you want to pass in an instance of Deck as you create an instance of Dealer, attach the deck to the dealer as an attribute, and then call the deck's functions from within the dealer's functions, you can do something like this:

class Dealer(object):
 def __init__(self, name, deck):
 self.name = name
 self.deck = deck
 def shuffle(self):
 self.deck.shuffle()
 def deal(self):
 return self.deck.takecard()

The object you pass in as the argument 'deck' will need to be an instance of Deck:

a_deck = Deck()
answered Mar 16, 2015 at 1:25
Sign up to request clarification or add additional context in comments.

1 Comment

Both the solutions work just great, although as martineau pointed out there was nothing to return through shuffle.
1

I believe the problem with your code is that you need to return from the Dealer functions, not just from the Deck's functions, like below. You also need to set the deck in your initialiser:

class Dealer(object):
 def __init__(self, name, deck):
 self.name = name
 self.deck = deck
 def shuffle(self):
 deck=self.deck
 return self.deck.shuffle()
 def deal(self):
 return self.deck.takecard()

Finally, you should init this class with:

my_dealer = Dealer("Bob", Deck())

Just using Deck without the parenthesis will result in the class being given to the dealer, not an instance of the class, which is what you want.

answered Mar 16, 2015 at 1:28

2 Comments

The return self.deck.shuffle() you have in Dealer.shuffle() will return None since Deck.shuffle() doesn't return anything.
Of course, you are right. Deck.shuffle() should return the deck, if that is what is wanted.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.