Created
November 25, 2021 17:56
-
-
Save raeq/78181c72a66b0c5f166fd26435aa8e89 to your computer and use it in GitHub Desktop.
Simulate playing cards
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
from typing import NamedTuple | |
class Card(NamedTuple): | |
suit: str | |
face: str | |
face_value: int | |
@property | |
def description(self): | |
return f"{self.face} of {self.suit} ({self.face_value})" | |
class Pack(): | |
def __init__(self, ): | |
self.faces: list = [('A', 11), ('2', 2), ('3', 3), ('4', 4), | |
('5', 5), ('6', 6), ('7', 7), ('8', 8), | |
('9', 9), ('10', 10), ('J', 10), ('Q', 10), ('K', 10)] | |
self.suits: list = ['Diamonds', 'Hearts', 'Clubs', 'Spades'] | |
self.cards: list = [] | |
for s in self.suits: | |
for f in self.faces: | |
self.cards.append(Card(suit=s, face=f[0], face_value=f[1])) | |
def __repr__(self): | |
return f'{self.__class__.__name__}(suits={repr(self.suits)},faces={repr(self.faces)})' | |
class Shoe(): | |
cards: list | |
shuffled: bool = False | |
def __init__(self, packs:int = 1): | |
self.cards = [] | |
for _ in range(packs): | |
self.cards.extend(Pack().cards[::]) | |
self.shuffle() | |
def shuffle(self): | |
for _ in range(7): | |
random.shuffle(self.cards) | |
self.shuffled = True | |
def draw(self): | |
return self.cards.pop() | |
s = Shoe(4) | |
while s.cards: | |
print(s.draw()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment