1
0
Fork 0
mirror of synced 2024-05-20 20:42:27 +12:00

Add a sorted method

This commit is contained in:
Phxntxm 2017-04-16 20:58:11 -05:00
parent 53f9183ab6
commit 7f48f11586

View file

@ -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))