Created
July 4, 2018 00:06
-
-
Save bobpaw/ebca0696cacde405bf85602e560a22c0 to your computer and use it in GitHub Desktop.
Stuff for playing cards
This file contains 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
from random import choice | |
class Deck: | |
"""Card deck""" | |
def __init__(self, deck=None): | |
"""Initializes the default deck, or use whatever list is given""" | |
self._deck = deck or [str((["Ace"]+list(range(2,11))+["Jack","Queen","King"])[i-1])+" of "+s+"s" for s in ["Spade","Heart","Club","Diamond"] for i in range(1,14)] | |
def output(self): | |
"""Return copy of internal list""" | |
return list(self._deck) | |
def dict_output(self): | |
"""Return internal list as a {value,suit} list""" | |
return [{"value":i[:i.find(" ")], "suit":i[i[::-1].find(" ")*-1:]} for i in self._deck] | |
def shuffle(self, ret=None): | |
"""Emulate a bridge shuffle of internal deck, call with value to return deck""" | |
retval = [] | |
half = [self._deck[:int(len(self._deck)/2)],self._deck[int(len(self._deck)/2):]] | |
lastval = 0 | |
last = [[1,1,0], [1,0,0]] | |
while len(half[0]) > 0 and len(half[1]) > 0: | |
lastval = choice(last[lastval]) | |
retval.append(half[lastval].pop(0)) | |
if len(half[0]) > 0: | |
retval.extend(half[0]) | |
elif len(half[1]) > 0: | |
retval.extend(half[1]) | |
else: | |
return False | |
self._deck = retval | |
return ret and list(retval) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment