Coverage for app/beaverbunch/src/game/card.py: 100.0%

23 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-08-05 15:01 +0000

1import functools 

2 

3 

4@functools.total_ordering 

5class Card: 

6 """ 

7 Represents a card in the game with a specific value. 

8 """ 

9 

10 CARD_VALUES = {i: str(i) for i in range(10)} 

11 CARD_VALUES.update({10: "PEEK", 11: "SWAP", 12: "DRAW", 50: "END_GAME"}) 

12 

13 def __init__(self, value: int) -> None: 

14 """ 

15 Initialises a Card instance with a specific value. 

16 

17 Args: 

18 value (int): The value of the card, must be one of the predefined values. 

19 Raises: 

20 ValueError: If the value is not one of the predefined card values. 

21 """ 

22 self.value = value 

23 if value not in self.CARD_VALUES.keys(): 

24 raise ValueError(f"Invalid card value: {value}") 

25 

26 def __eq__(self, other) -> bool: 

27 if isinstance(other, Card): 

28 return self.value == other.value 

29 return False 

30 

31 def __gt__(self, other) -> bool: 

32 if isinstance(other, Card): 

33 return self.value > other.value 

34 return NotImplemented 

35 

36 def __hash__(self) -> int: 

37 return hash(self.value) 

38 

39 def __repr__(self) -> str: 

40 return f"Card(value={self.value})" 

41 

42 def __str__(self) -> str: 

43 return self.CARD_VALUES[self.value]