Skip to content

Instantly share code, notes, and snippets.

@Ayyko
Created June 7, 2017 04:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ayyko/ba5691a8c44a6a9be99c4bbc03a6d4fe to your computer and use it in GitHub Desktop.
Save Ayyko/ba5691a8c44a6a9be99c4bbc03a6d4fe to your computer and use it in GitHub Desktop.
import random
class Deck:
## A deck of cards
def __init__(self, **args):
##Setting initial conditions, see readme for descriptions of each parameter
error = 0
errorStr = ''
if 'size' in args:
##Number of cards
self.cardNum = args['size']
if (type(self.cardNum) is not int):
error = 1
errorStr += 'The \'size\' argument must be an Integer. \n'
elif 'size' not in args:
self.cardNum = 52
if 'suits' in args:
##Names of suits
self.cardSuits = args['suits']
if type(self.cardSuits) is not list:
error = 1
errorStr += 'The \'suits\' argument must be a list. \n'
elif 'suits' not in args:
self.cardSuits = ['club', 'spade', 'diamond', 'heart']
if 'names' in args:
##Names of cards
self.cardNames = args['names']
if type(self.cardNames) is not list:
error = 1
errorStr += 'The \'names\' argument must be a list. \n'
elif 'names' not in args:
self.cardNames = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']
if 'jokerSize' in args:
##Number of joker cards
self.cardJoker = args['jokerSize']
if type(self.cardJoker) is not int:
error = 1
errorStr += 'The \'jokerSize\' argument must be an integer. \n'
elif 'jokerSize' not in args:
self.cardJoker = 0
if 'deckType' in args:
##Process in which deck is constructed
self.cardType = args['deckType']
if type(self.cardType) is not int or self.cardType > 1 or self.cardType < 0:
error = 1
errorStr += 'The \'deckType\' argument must be either 0 or 1 (Integer). \n'
elif 'deckType' not in args:
self.cardType = 0
if 'size' not in errorStr and 'suits' not in errorStr and 'names' not in errorStr:
if (int(self.cardNum/len(self.cardSuits))) > len(self.cardNames):
error = 1
errorStr += 'If you wish to have more cards than the length of the \'names\' list per suit (\'size\' / the number of items in the \'suits\' list), you must create a list of names with a proper length by passing it using the \'names\' arg.'
if error == 1:
print 'An error has occured during deck construction!\n'
print errorStr
print 'If you need help understanding how to set arguments, check the readme.'
elif error != 1:
print 'No error' ##debug
self.construct()
def construct(self):
##Constructs deck. Calling after initialization will recreate the entire deck
self.deck = []
perSuit = (int(self.cardNum/len(self.cardSuits)))
perSuitOverflow = (self.cardNum%len(self.cardSuits))
if self.cardType == 0:
print 'construct0' ##debug
for y in range(len(self.cardSuits)):
suit = str(self.cardSuits[y])
for x in range(perSuit):
self.deck.append(self.cardNames[x] + suit)
if perSuitOverflow > 0:
self.deck.append(self.cardNames[-1] + suit)
perSuitOverflow -= 1
while self.cardJoker > 0:
self.deck.append('Joker')
self.cardJoker -= 1
print self.deck ##debug
print perSuit ##debug
print perSuitOverflow ##debug
elif self.cardType == 1:
print 'construct1' ##debug
for y in range(len(self.cardSuits)):
for x in range(perSuit):
self.deck.append(self.cardNames[x])
if perSuitOverflow > 0:
self.deck.append(self.cardNames[-1])
perSuitOverflow -= 1
while self.cardJoker > 0:
self.deck.append('Joker')
self.cardJoker -= 1
print self.deck ##debug
def shuffle(self):
##Shuffle items in the deck list
random.shuffle(self.deck)
print self.deck ##debug
def getDeck(self):
##Returns deck
return self.deck
def setDeck(self, newDeck):
##Replace the current deck with a new list. Read the Readme before using this.
self.deck = newDeck
print self.deck ##debug
def draw(self, num):
##Draws #num# cards and returns them as a list
hand = []
for a in range(num):
x = self.deck.pop(0)
hand.append(x)
return hand
def drawStr(self, num, spaces = 1):
## draws #num# cards and returns them as a str, with #spaces# spaces between each card.
drah = self.draw(num)
drahStr = ''
for x in drah:
drahStr += str(x) + (' '*spaces)
return drahStr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment