Influencé par ce 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):
#Il devient plus fort dans l'ordre du club <dia <coeur <pique.
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()
#Piochez 5 cartes au hasard
cards = Trump.stack_draw(5)
print(cards)
print(len(cards))
#Piochez toutes les cartes restantes
cards = Trump.stack_draw(52)
print(cards)
print(len(cards))
#La carte est vide
cards = Trump.stack_draw()
print(cards)
print(len(cards))
#Réinitialiser la pile
Trump.stack_init()
#Piochez une carte
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