From 1b14ace2cd7dc7ba66a0149d1e8574128c6cdfc1 Mon Sep 17 00:00:00 2001 From: Tim Date: Mon, 6 Jul 2026 23:33:23 +0200 Subject: [PATCH] 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 --- bridzik.py | 12 ++++++++++++ tests/test_engine.py | 10 ++++++++++ 2 files changed, 22 insertions(+) diff --git a/bridzik.py b/bridzik.py index 062767e..3d80e8f 100644 --- a/bridzik.py +++ b/bridzik.py @@ -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) diff --git a/tests/test_engine.py b/tests/test_engine.py index 8aef6f9..7dd048a 100644 --- a/tests/test_engine.py +++ b/tests/test_engine.py @@ -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)