Playing with cards modeling

This commit is contained in:
Dan Buch 2022-01-25 10:13:53 -05:00
parent 116ad347db
commit e30cbe2aca
Signed by: meatballhat
GPG Key ID: A12F782281063434
2 changed files with 144 additions and 0 deletions

86
hyrule/cards.py Normal file
View File

@ -0,0 +1,86 @@
import dataclasses
import enum
import random
Suit = enum.Enum(
"Suit",
"""
CLUBS
DIAMONDS
HEARTS
SPADES
""",
)
FaceValue = enum.Enum(
"FaceValue",
"""
ACE
TWO
THREE
FOUR
FIVE
SIX
SEVEN
EIGHT
NINE
TEN
JACK
QUEEN
KING
""",
)
@dataclasses.dataclass
class Card:
suit: Suit
face_value: FaceValue
def __str__(self):
return f"{self.face_value} of {self.suit}"
class Joker:
def __str__(self):
return "JOKER"
class Deck:
def __init__(self, n_jokers=2):
self._cards = list(Deck.generate(n_jokers=n_jokers))
def __repr__(self):
return f"<Deck len={len(self)}>"
def __str__(self):
as_string = []
for card in self._cards:
as_string.append(str(card))
return "\n".join(as_string)
def __len__(self):
return len(self._cards)
def draw(self):
if len(self._cards) > 0:
return self._cards.pop()
return None
def shuffle(self):
random.shuffle(self._cards)
return self
def cut(self):
cut_point = random.randint(0, len(self))
self._cards = self._cards[cut_point:] + self._cards[:cut_point]
return self
@staticmethod
def generate(n_jokers=2):
for _ in range(n_jokers):
yield Joker()
for suit in Suit:
for face_value in FaceValue:
yield Card(suit=suit, face_value=face_value)

58
hyrule/simulation.py Normal file
View File

@ -0,0 +1,58 @@
#!/usr/bin/env python3
import argparse
import pprint
import sys
import typing
import cards
class Player:
hand: typing.Set[cards.Card]
captured: typing.Set[cards.Card]
@property
def score(self):
return len(self.captured)
def main():
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument(
"--player-count", "-c", default=2, type=int, help="The number of players"
)
parser.add_argument(
"--turn-count",
"-n",
default=5,
type=int,
help="The number of turns to play",
)
args = parser.parse_args()
for turn in _simulate_game(
player_count=args.player_count, turn_count=args.turn_count
):
_show_turn(turn)
return 0
def _simulate_game(player_count=2, turn_count=5):
for turn in range(turn_count):
yield _simulate_turn(player_count=player_count)
def _simulate_turn(player_count=2):
...
def _show_turn(turn):
...
if __name__ == "__main__":
sys.exit(main())