Question : Help in solving , completing this Python program, I have added the cl
ID: 3829235 • Letter: Q
Question
Question: Help in solving , completing this Python program, I have added the class Deck, thanks for the assistance.
For this project I have to implement to the classes the following specifications.
Class Card) Have to Overload appropriate operators for class Card so that you can compare cards based on rank:
>>> Card(' 3 ', ' spade') < Card(' 8 ', ' Diamond')
True
>>> Card(' 3 ', ' spade') > Card(' 8 ', ' Diamond')
False
>>> Card(' 3 ', ' spade') >= Card(' 8 ', ' Diamond ')
True
>>> Card(' 3 ', ' spade ') <= Card(' 8 ', ' Diamond ')
False
Class Hand) Also have to Implement to class Hand that represents a hand of playing cards. The class should have a constructor that takes as input the player ID (a string). It should support method addCard()that takes a card as input and adds it to the hand and method showHand() that displays the player’s hand in the format shown.
>>> hand = Hand( ' House ')
>>> deck = Deck()
>>> deck.shuffle()
>>> hand.addCard(deck.dealCard())
>>> hand.addCard(deck.dealCard())
>>> hand.addCard(deck.dealCard())
>>> hand.showHand()
House: 10 heart 8 spade 2 spade
>>> print(hand)
House: 10 8 2
Note that the last two outputs ( from__str__() and showhand() ) are the same.
*You may use the class Deck included in the slides.
class Deck:
'represents a deck of 52 cards'
# ranks and suits are Deck class variables
ranks = {'2','3','4','5','6','7','8','9','10','J','Q','K','A'}
# suits is a set of 4 Unicode symbols representing the 4 suits
suits = {'u2660', 'u2661', 'u2662', 'u2663'}
def __init__(self):
'initialize deck of 52 cards'
self.deck = [] # deck is initially empty
for suit in Deck.suits: # suits and ranks are Deck
for rank in Deck.ranks: # class variables
# add Card with given rank and suit to deck
self.deck.append(Card(rank,suit))
def dealCard(self):
'deal (pop and return) card from the top of the deck'
return self.deck.pop()
def shuffle(self):
'shuffle the deck'
random.shuffle(self.deck) #not doing anything; should randomize a list?
Explanation / Answer
import random
class Card:
def __init__(self, rank, suite):
rank = rank.strip()
self._rank = rank
self.rank = rank
if rank == 'A':
self.rank = 14
elif rank == 'J':
self.rank = 11
elif rank == 'Q':
self.rank = 12
elif rank == 'K':
self.rank = 13
else:
self.rank = int(rank)
self.suite = suite.strip()
def __lt__(self,other):
return int(self.rank) < int(other.rank)
def __le__(self,other):
return int(self.rank) <= int(other.rank)
def __gt__(self,other):
return int(self.rank) > int(other.rank)
def __ge__(self,other):
return int(self.rank) >= int(other.rank)
def __eq__(self,other):
return int(self.rank) == int(other.rank)
def __ne__(self,other):
return not(self.__eq__)
def __str__(self):
return (self._rank + " " + self.suite)
class Deck:
'represents a deck of 52 cards'
# ranks and suits are Deck class variables
ranks = {'2','3','4','5','6','7','8','9','10','J','Q','K','A'}
# suits is a set of 4 Unicode symbols representing the 4 suits
suits = {'u2660', 'u2661', 'u2662', 'u2663'}
def __init__(self):
'initialize deck of 52 cards'
self.deck = [] # deck is initially empty
for suit in Deck.suits: # suits and ranks are Deck
for rank in Deck.ranks: # class variables
# add Card with given rank and suit to deck
self.deck.append(Card(rank,suit))
def dealCard(self):
'deal (pop and return) card from the top of the deck'
return self.deck.pop()
def shuffle(self):
'shuffle the deck'
random.shuffle(self.deck) #not doing anything; should randomize a list?
class Hand:
def __init__(self, id):
self .id = id.strip()
self.cards = []
def addCard(self, card):
if not card in self.cards:
self.cards.append(card)
def showHand(self):
print(self)
def __str__(self):
result = self.id + ": "
for card in self.cards:
result += str(card) + " "
return result
hand = Hand( ' House ')
deck = Deck()
deck.shuffle()
hand.addCard(deck.dealCard())
hand.addCard(deck.dealCard())
hand.addCard(deck.dealCard())
hand.showHand()
print(hand)
# code link: https://paste.ee/p/1q9FI
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.