chore: Validate hand length

This commit is contained in:
Paul Harrison 2022-11-18 20:45:19 +00:00
parent 68bfee17f1
commit 5393e7b799
1 changed files with 25 additions and 4 deletions

View File

@ -1,6 +1,8 @@
from __future__ import annotations
from pydantic import BaseModel, validator from pydantic import BaseModel, validator
from poker.constants import Suit, Value from poker.constants import Rank, Suit, Value
class Card(BaseModel): class Card(BaseModel):
@ -13,18 +15,37 @@ class Card(BaseModel):
"""Hash function.""" """Hash function."""
return hash(f"{self.value} {self.suit}") 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): class Hand(BaseModel):
"""Hand domain model class.""" """Hand domain model class."""
cards: HandType cards: list[Card]
@validator("cards") @validator("cards")
def validate_unique(cls, cards: HandType) -> HandType: def validate_unique(cls, cards: list[Card]) -> list[Card]:
"""Validate hand comprises unique cards.""" """Validate hand comprises unique cards."""
if len(cards) != len(set(cards)): if len(cards) != len(set(cards)):
raise ValueError("Hand contains duplicate cards.") raise ValueError("Hand contains duplicate cards.")
return 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