Skip to content

Instantly share code, notes, and snippets.

@sli
Created December 31, 2010 23:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sli/761418 to your computer and use it in GitHub Desktop.
Save sli/761418 to your computer and use it in GitHub Desktop.
Demon-playing Python script.
import random
class Card:
def __init__(self, name, value, suit):
self.name = name
self.value = value
self.suit = suit
def __str__(self):
return '%s%s' % (self.name, self.suit[0])
# compare another card object to this one, returns 1
# if this card object has a greater value, 0 if equal
# and -1 if lower
def compare(self, card):
if self.value == card.value:
return 0
elif self.value > 1 and card.value > 1:
if self.value > card.value: return 1
else: return -1
elif self.value == 1:
return 1
elif card.value == 1:
return -1
class Demon:
cards = {
'A': 1,
'2': 2,
'3': 3,
'4': 4,
'5': 5,
'6': 6,
'7': 7,
'8': 8,
'9': 9,
'10': 10,
'J': 11,
'Q': 12,
'K': 13
}
suits = {
'S': 'Spades',
'C': 'Clubs',
'D': 'Diamonds',
'H': 'Hearts'
}
def __init__(self):
self.constructDeck()
self.demon = []
self.tableaus = (
[],
[],
[],
[]
)
self.foundations = (
[],
[],
[],
[]
)
def constructDeck(self):
self.deck = []
for c in self.cards.keys():
for s in self.suits.keys():
self.deck.append(Card(c, self.cards[c], [s, self.suits[s]]))
random.shuffle(self.deck)
def setup(self):
self.demon = self.deck[:13][::-1]
self.tableaus[0].append(self.deck[13])
self.tableaus[1].append(self.deck[14])
self.tableaus[2].append(self.deck[15])
self.tableaus[3].append(self.deck[16])
self.foundations[0].append(self.deck[17])
self.reserve = self.deck[18:]
self.waste = []
def deal(self):
if len(self.reserve) == 0:
self.reserve = self.waste[::-1]
self.waste = []
tmp = self.reserve[:3][::-1]
self.waste += tmp
self.reserve = self.reserve[3:]
if __name__ == '__main__':
# This is all testing-related stuff right now.
d = Demon()
d.setup()
for c in d.deck:
print '%s,' % str(c),
print 'Cards dealt. Table is as follows:'
print '- Demon: %s' % d.demon[0]
print '- Tableaus: %s %s %s %s' % (d.tableaus[0][0], d.tableaus[1][0], d.tableaus[2][0], d.tableaus[3][0])
print '- Foundation Value: %s' % d.foundations[0][0]
print
while 1:
d.deal()
print 'Deal:'
print '- Available card: %s' % d.waste[-1]
x = raw_input()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment