57 lines
1.3 KiB
Python
57 lines
1.3 KiB
Python
|
|
# -*- coding: utf-8 -*-
|
||
|
|
# games/solitaire/base/solitaire_card.py
|
||
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
from dataclasses import dataclass
|
||
|
|
|
||
|
|
from resources.code_res.game_constants import (
|
||
|
|
CARD_FILE_PATTERN,
|
||
|
|
RED_SUITS,
|
||
|
|
SUIT_SYMBOLS,
|
||
|
|
)
|
||
|
|
|
||
|
|
SOLITAIRE_RANKS: tuple[str, ...] = (
|
||
|
|
"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"
|
||
|
|
)
|
||
|
|
RANK_VALUES: dict[str, int] = {rank: idx + 1 for idx, rank in enumerate(SOLITAIRE_RANKS)}
|
||
|
|
|
||
|
|
|
||
|
|
@dataclass
|
||
|
|
class SolitaireCard:
|
||
|
|
suit: str
|
||
|
|
rank: str
|
||
|
|
face_up: bool = False
|
||
|
|
deck_id: int = 0
|
||
|
|
|
||
|
|
@property
|
||
|
|
def value(self) -> int:
|
||
|
|
return RANK_VALUES[self.rank]
|
||
|
|
|
||
|
|
@property
|
||
|
|
def is_red(self) -> bool:
|
||
|
|
return self.suit in RED_SUITS
|
||
|
|
|
||
|
|
@property
|
||
|
|
def filename(self) -> str:
|
||
|
|
return CARD_FILE_PATTERN.format(suit=self.suit, rank=self.rank)
|
||
|
|
|
||
|
|
@property
|
||
|
|
def label(self) -> str:
|
||
|
|
return f"{self.rank}{SUIT_SYMBOLS.get(self.suit, self.suit)}"
|
||
|
|
|
||
|
|
def to_dict(self) -> dict:
|
||
|
|
return {
|
||
|
|
"s": self.suit,
|
||
|
|
"r": self.rank,
|
||
|
|
"u": 1 if self.face_up else 0,
|
||
|
|
"d": self.deck_id,
|
||
|
|
}
|
||
|
|
|
||
|
|
@classmethod
|
||
|
|
def from_dict(cls, data: dict) -> SolitaireCard:
|
||
|
|
return cls(
|
||
|
|
suit=data["s"],
|
||
|
|
rank=data["r"],
|
||
|
|
face_up=bool(data.get("u", 0)),
|
||
|
|
deck_id=int(data.get("d", 0)),
|
||
|
|
)
|