Playing card class in Python (with comparison)

Influenced by this post http://qiita.com/jay/items/ee5bd3bbabb0d57f294d

Trump.py


import random


class Trump:
    """ Trump Card class
    """

    MARK_SPADE = 'S'
    MARK_CLUB = 'C'
    MARK_DIA = 'D'
    MARK_HEART = 'H'

    MARK_LIST = [MARK_SPADE, MARK_CLUB, MARK_DIA, MARK_HEART]
    EFUDA_LIST = ['A'] + list(map(str, range(2, 11))) + ['J', 'Q', 'K', 'A']

    card_stacks = []

    def __init__(self, mark=None, num=None):
        self.mark = mark if mark is not None else random.choice(Trump.MARK_LIST)
        self.num = num if num is not None else random.randrange(1, 13)

    def get_efuda(self):
        return Trump.EFUDA_LIST[self.num]

    def stack_init():
        Trump.card_stacks = [Trump(mark=m, num=n + 1) for m in Trump.MARK_LIST for n in range(13)]
        random.shuffle(Trump.card_stacks)

    def stack_draw(num=1):
        cards = Trump.card_stacks[0:num]
        Trump.card_stacks = Trump.card_stacks[num:]
        return cards

    def __str__(self):
        return "<Trump: %s-%s>" % (self.mark, self.get_efuda())

    def __repr__(self):
        return self.__str__()


class IndianTrump(Trump):

    #It becomes stronger in the order of club <dia <heart <spade.
    MARK_RANK = {Trump.MARK_SPADE: 4, Trump.MARK_CLUB: 1, Trump.MARK_DIA: 2, Trump.MARK_HEART: 3}

    def __lt__(self, other):
        if self.mark is not other.mark:
            return IndianTrump.MARK_RANK[self.mark] < IndianTrump.MARK_RANK[other.mark]
        return (self.num - 1) % 13 < (other.num - 1) % 13

    def stack_init():
        IndianTrump.card_stacks = [IndianTrump(mark=m, num=n + 1) for m in IndianTrump.MARK_LIST for n in range(13)]
        random.shuffle(IndianTrump.card_stacks)

    def stack_draw(num=1):
        cards = IndianTrump.card_stacks[0:num]
        IndianTrump.card_stacks = IndianTrump.card_stacks[num:]
        return cards


    def __str__(self):
        return "<IndianTrump: %s-%s>" % (self.mark, self.get_efuda())


if __name__ == '__main__':
    print("--- Trump Draw ---")
    Trump.stack_init()
#Randomly draw 5 cards
    cards = Trump.stack_draw(5)
    print(cards)
    print(len(cards))
#Draw all remaining cards
    cards = Trump.stack_draw(52)
    print(cards)
    print(len(cards))
#The card is empty
    cards = Trump.stack_draw()
    print(cards)
    print(len(cards))
#Reset stack
    Trump.stack_init()
#Draw one card
    cards = Trump.stack_draw()
    print(cards)
    print(len(cards))

    print("--- Indian Porker ---")
    IndianTrump.stack_init()
    for i in range(8):
        card_a, card_b = IndianTrump.stack_draw(2)
        print("CardA: " + str(card_a), "CardB: " + str(card_b))
        print("CardA %s CardB" % (">" if (card_a > card_b) else "<"), "\n")

Recommended Posts

Playing card class in Python (with comparison)
case class in python
Class notation in Python
Scraping with selenium in Python
Working with LibreOffice in Python
Scraping with chromedriver in python
Debugging with pdb in Python
Null object comparison in Python
Playing with a user-local artificial intelligence API in Python
Working with sounds in Python
Scraping with Selenium in Python
Write letters in the card illustration with OpenCV python
Scraping with Tor in Python
Tweet with image in Python
Combined with permutations in Python
Error when playing with python
Number recognition in images with Python
Playing handwritten numbers with python Part 1
Testing with random numbers in Python
GOTO in Python with Sublime Text 3
Working with LibreOffice in Python: import
Landmines hidden in Python class variables
Scraping with Selenium in Python (Basic)
CSS parsing with cssutils in Python
Numer0n with items made in Python
Open UTF-8 with BOM in Python
Use rospy with virtualenv in Python3
Use Python in pyenv with NeoVim
Heatmap with Dendrogram in Python + matplotlib
Read files in parallel with Python
Examine the object's class in python
Password generation in texto with python
Use OpenCV with Python 3 in Window
Until dealing with python in Atom
[Python] Inherit a class with class variables
Get started with Python in Blender
Working with DICOM images in Python
Write documentation in Sphinx with Python Livereload
Generate a first class collection in Python
Get additional data in LDAP with python
Create a Python function decorator with Class
[Introduction to Python] How to use class in Python?
Try logging in to qiita with Python
Stress Test with Locust written in Python
Python3> in keyword> True with partial match?
Exclusive control with lock file in Python
Implement __eq__ etc. generically in Python class
Build a blockchain with Python ① Create a class
Comparison of Japanese conversion module in Python3
Tips for dealing with binaries in Python
Display Python 3 in the browser with MAMP
Post Test 3 (Working with PosgreSQL in Python)
Dealing with "years and months" in Python
Process multiple lists with for in Python
Learn Python! Comparison with Java (basic function)
Replace non-ASCII with regular expressions in Python
Connect with mysql.connector with ssh tunnel in Python 3.7
One liner webServer (with CGI) in python
Get Started with TopCoder in Python (2020 Edition)
How to use __slots__ in Python class
Easy image processing in Python with Pillow