Engine: Card a farby/hodnoty su hashovatelne
Vlastne __eq__ rusilo zdedeny __hash__, takze karty ani enumy sa nedali pouzit ako kluce dictov a v setoch. Hash doplneny konzistentne s __eq__. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
+12
@@ -19,6 +19,10 @@ class Card_colors(Enum):
|
||||
return self.name == other.name
|
||||
return NotImplemented
|
||||
|
||||
# vlastne __eq__ rusi zdedeny __hash__ -- obnovit konzistentne s __eq__
|
||||
def __hash__(self):
|
||||
return hash(self.name)
|
||||
|
||||
|
||||
class Card_values(Enum):
|
||||
C7 = 1
|
||||
@@ -51,6 +55,10 @@ class Card_values(Enum):
|
||||
return self.name == other.name
|
||||
return NotImplemented
|
||||
|
||||
# vlastne __eq__ rusi zdedeny __hash__ -- obnovit konzistentne s __eq__
|
||||
def __hash__(self):
|
||||
return hash(self.name)
|
||||
|
||||
|
||||
class Card():
|
||||
def __init__(self, color: Card_colors, value: Card_values):
|
||||
@@ -63,6 +71,10 @@ class Card():
|
||||
and self.value == other.value
|
||||
return NotImplemented
|
||||
|
||||
# vlastne __eq__ rusi zdedeny __hash__ -- obnovit konzistentne s __eq__
|
||||
def __hash__(self):
|
||||
return hash((self.color, self.value))
|
||||
|
||||
def __str__(self):
|
||||
return '{}_{}'.format(self.color.name, self.value.name)
|
||||
|
||||
|
||||
@@ -121,6 +121,16 @@ class StashCase(unittest.TestCase):
|
||||
self.assertRaises(BridzikException, s.get_active_player)
|
||||
|
||||
|
||||
class CardCase(unittest.TestCase):
|
||||
def test_hashable_consistent_with_eq(self):
|
||||
# vlastne __eq__ nesmie zrusit hashovatelnost (dict/set pouzitie)
|
||||
heart_7 = Card(Card_colors['HEARTS'], Card_values['C7'])
|
||||
self.assertEqual(hash(heart_7), hash(Card(Card_colors['HEARTS'], Card_values['C7'])))
|
||||
self.assertEqual(len(set(cards)), 32)
|
||||
self.assertEqual({Card_colors['HEARTS']: 1}[Card_colors['HEARTS']], 1)
|
||||
self.assertEqual({Card_values['ACE']: 1}[Card_values['ACE']], 1)
|
||||
|
||||
|
||||
class RoundCase(unittest.TestCase):
|
||||
def test_round_constructor(self):
|
||||
self.assertRaises(BridzikException, Round, round_number=8, first_player=0)
|
||||
|
||||
Reference in New Issue
Block a user