Artvv commited on
Commit
69cd84d
·
verified ·
1 Parent(s): 6a24342

Upload src/persistentpoker_bench/cards.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. src/persistentpoker_bench/cards.py +62 -0
src/persistentpoker_bench/cards.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ from dataclasses import dataclass
4
+ from enum import StrEnum
5
+
6
+ RANK_SYMBOLS = "23456789TJQKA"
7
+ RANK_TO_VALUE = {symbol: value for value, symbol in enumerate(RANK_SYMBOLS, start=2)}
8
+ VALUE_TO_RANK = {value: symbol for symbol, value in RANK_TO_VALUE.items()}
9
+
10
+
11
+ class Suit(StrEnum):
12
+ CLUBS = "c"
13
+ DIAMONDS = "d"
14
+ HEARTS = "h"
15
+ SPADES = "s"
16
+
17
+
18
+ @dataclass(frozen=True, slots=True, order=True)
19
+ class Card:
20
+ rank_value: int
21
+ suit: Suit
22
+
23
+ @property
24
+ def rank_symbol(self) -> str:
25
+ return VALUE_TO_RANK[self.rank_value]
26
+
27
+ def to_notation(self) -> str:
28
+ return f"{self.rank_symbol}{self.suit.value}"
29
+
30
+ @classmethod
31
+ def from_notation(cls, notation: str) -> "Card":
32
+ token = notation.strip().upper()
33
+ if len(token) != 2:
34
+ raise ValueError(f"Invalid card notation: {notation!r}")
35
+
36
+ rank_symbol = token[0]
37
+ suit_symbol = token[1].lower()
38
+ if rank_symbol not in RANK_TO_VALUE:
39
+ raise ValueError(f"Invalid rank in card notation: {notation!r}")
40
+
41
+ try:
42
+ suit = Suit(suit_symbol)
43
+ except ValueError as exc:
44
+ raise ValueError(f"Invalid suit in card notation: {notation!r}") from exc
45
+
46
+ return cls(rank_value=RANK_TO_VALUE[rank_symbol], suit=suit)
47
+
48
+
49
+ def parse_cards(notations: list[str] | tuple[str, ...]) -> tuple[Card, ...]:
50
+ return tuple(Card.from_notation(token) for token in notations)
51
+
52
+
53
+ def cards_to_notation(cards: list[Card] | tuple[Card, ...]) -> tuple[str, ...]:
54
+ return tuple(card.to_notation() for card in cards)
55
+
56
+
57
+ def standard_deck() -> tuple[Card, ...]:
58
+ return tuple(
59
+ Card(rank_value=rank_value, suit=suit)
60
+ for suit in Suit
61
+ for rank_value in RANK_TO_VALUE.values()
62
+ )