aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Harrison <paul@harrison.sh>2022-11-18 20:45:19 +0000
committerPaul Harrison <paul@harrison.sh>2022-12-15 16:02:14 +0000
commit5393e7b79939f7da36d55570af3374cef2db2d51 (patch)
treef5ed1087877a1f82fec33c4e02be5108c0708a58
parent68bfee17f1dc7b1126de11b6f0ea3b20108266d3 (diff)
chore: Validate hand length
-rw-r--r--poker/models.py29
1 files changed, 25 insertions, 4 deletions
diff --git a/poker/models.py b/poker/models.py
index 5ecda90..1d6af05 100644
--- a/poker/models.py
+++ b/poker/models.py
@@ -1,6 +1,8 @@
+from __future__ import annotations
+
from pydantic import BaseModel, validator
-from poker.constants import Suit, Value
+from poker.constants import Rank, Suit, Value
class Card(BaseModel):
@@ -13,18 +15,37 @@ class Card(BaseModel):
"""Hash function."""
return hash(f"{self.value} {self.suit}")
+ def __le__(self, other: Card) -> bool:
+ """Less than or equal to."""
+ return self.value <= other.value
-HandType = tuple[Card, Card, Card, Card, Card]
+ def __lt__(self, other: Card) -> bool:
+ """Strictly less than."""
+ return self.value < other.value
class Hand(BaseModel):
"""Hand domain model class."""
- cards: HandType
+ cards: list[Card]
@validator("cards")
- def validate_unique(cls, cards: HandType) -> HandType:
+ def validate_unique(cls, cards: list[Card]) -> list[Card]:
"""Validate hand comprises unique cards."""
if len(cards) != len(set(cards)):
raise ValueError("Hand contains duplicate cards.")
return cards
+
+ @validator("cards")
+ def validate_length(cls, cards: list[Card]) -> list[Card]:
+ """Validate hand has five cards."""
+ if len(cards) != 5:
+ raise ValueError("Hand must have five cards.")
+ return cards
+
+
+class RankedHand(Hand):
+ """Ranked hand domain model class."""
+
+ rank: Rank
+ description: str