Poker card in Python Let the CarteBase class present itself in the context of this exercise. It encapsulates a game card characterized by numerical values for its face and its kind. The possible faces are integers from 1 to 13, with face 1 corresponding to the ace, and faces 11, 12 and 13 corresponding respectively to the jack, the queen and the king. The other faces are simply identified by their number from 2 to 10. The possible grades are integers from 1 to 4 corresponding respectively to the cards of spade, heart, tile and clover. Analyze this class to understand how it works. You are asked to create a class called CarteDePoker that inherits CarteBase and defines or redefines the following methods: force(self) which returns the strength of the card in the poker game. In Poker, cards have the strength of their digital face from 2 to 13, except for the ace which is stronger than the other cards. So we give it a strength of 14. __eq__(self, other) to define operator behavior ==. __lt__(self, other) to define operator behavior <. __le__(self, other) to define operator behavior <=. __gt__(self, other) to define operator behavior >. __ge__(self, other) to define operator behavior >=. __ne__(self, other) to define operator behavior!=. Note that in Poker, a card is considered inferior to another card if the value of its strength is strictly inferior, or if its strength is equal, but its kind is numerically superior (i.e. pic > heart > tile > clover). Moreover, two cards are equivalent if their face and their kind are the same. Note also that only these first two (2) operators need to be defined from the attributes of the card, the other four can be deduced generically from < and ==. For example, we can define a <= b as the equivalent of a < b or a == b. To implement your class, be sure to use the basic class methods whenever possible (don’t reinvent the wheel), and be sure not to change the wheel. This is what I have to start: class CarteBase: names = {1: 'as', 11: 'jack', 12: 'lady', 13: 'king'} sorts = ['spade', 'heart', 'tile', 'clover'] def __init__(self, m, n): assert 1 <= m <= 13 and 1 <= n <= 4 self.numface = m self.numsort = n def __repr__(self): return f'{type(self). __name__}({self.numface}, {self.numsort})' def __str__(self): return f'{self.nom()} de {self.sorte()}' def name(self): # returns the name of the card for the ace, jack, queen and king, # otherwise the numerical value of its face (2 to 10) return CarteBase.noms.get(self.numface, str(self.numface)) def sorte(self): # returns the kind of card (spade, heart, tile or clover) return CarteBase.kinds[self.numsort-1] def force(self): # returns the default force of the card (the numerical value of its face) return self.numface
Poker card in Python
Let the CarteBase class present itself in the context of this exercise. It encapsulates a game card characterized by numerical values for its face and its kind. The possible faces are integers from 1 to 13, with face 1 corresponding to the ace, and faces 11, 12 and 13 corresponding respectively to the jack, the queen and the king. The other faces are simply identified by their number from 2 to 10. The possible grades are integers from 1 to 4 corresponding respectively to the cards of spade, heart, tile and clover. Analyze this class to understand how it works.
You are asked to create a class called CarteDePoker that inherits CarteBase and defines or redefines the following methods:
force(self) which returns the strength of the card in the poker game. In Poker, cards have the strength of their digital face from 2 to 13, except for the ace which is stronger than the other cards. So we give it a strength of 14.
__eq__(self, other) to define operator behavior ==.
__lt__(self, other) to define operator behavior <.
__le__(self, other) to define operator behavior <=.
__gt__(self, other) to define operator behavior >.
__ge__(self, other) to define operator behavior >=.
__ne__(self, other) to define operator behavior!=.
Note that in Poker, a card is considered inferior to another card if the value of its strength is strictly inferior, or if its strength is equal, but its kind is numerically superior (i.e. pic > heart > tile > clover). Moreover, two cards are equivalent if their face and their kind are the same. Note also that only these first two (2) operators need to be defined from the attributes of the card, the other four can be deduced generically from < and ==. For example, we can define a <= b as the equivalent of a < b or a == b.
To implement your class, be sure to use the basic class methods whenever possible (don’t reinvent the wheel), and be sure not to change the wheel.
This is what I have to start:
class CarteBase:
names = {1: 'as', 11: 'jack', 12: 'lady', 13: 'king'}
sorts = ['spade', 'heart', 'tile', 'clover']
def __init__(self, m, n):
assert 1 <= m <= 13 and 1 <= n <= 4
self.numface = m
self.numsort = n
def __repr__(self):
return f'{type(self). __name__}({self.numface}, {self.numsort})'
def __str__(self):
return f'{self.nom()} de {self.sorte()}'
def name(self):
# returns the name of the card for the ace, jack, queen and king,
# otherwise the numerical value of its face (2 to 10)
return CarteBase.noms.get(self.numface, str(self.numface))
def sorte(self):
# returns the kind of card (spade, heart, tile or clover)
return CarteBase.kinds[self.numsort-1]
def force(self):
# returns the default force of the card (the numerical value of its face)
return self.numface
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 2 images