From 7f48f1158691d512f1db0f3b260bebd23572aab0 Mon Sep 17 00:00:00 2001 From: Phxntxm Date: Sun, 16 Apr 2017 20:58:11 -0500 Subject: [PATCH] Add a sorted method --- cogs/utils/cards.py | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/cogs/utils/cards.py b/cogs/utils/cards.py index 8b8b6ff..8a004d2 100644 --- a/cogs/utils/cards.py +++ b/cogs/utils/cards.py @@ -1,8 +1,9 @@ import itertools import random +from functools import cmp_to_key suits = ['S', 'C', 'H', 'D'] -faces = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'] +faces = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A'] class Deck: def __init__(self, prefill=True): @@ -16,6 +17,11 @@ class Deck: def __iter__(self): for card in self.deck: yield card + + def refresh(self): + """A method that 'restarts' the deck, filling it back with 52 cards""" + self.deck = list(itertools.product(suits, faces)) + @property def count(self): """A property to provide how many cards are currently in the deck""" @@ -41,3 +47,22 @@ class Deck: def shuffle(self): """Shuffles the deck in place""" random.SystemRandom().shuffle(self.deck) + + def sorted_deck(self): + """Provides a sorted representation of the current deck""" + # The idea behind this one is for being useful in hands...there's no reason we need to sort in place + # So all we're going to do here is compare how we want, and return the sorted representation + def compare(first, second): + if suits.index(first[0]) < suits.index(second[0]): + return -1 + elif suits.index(first[0]) > suits.index(second[0]): + return 1 + else: + if faces.index(first[1]) < faces.index(second[1]): + return -1 + elif faces.index(first[1]) > faces.index(second[1]): + return 1 + else: + return 0 + + return sorted(self.deck, key=cmp_to_key(compare))