Python Assignment: due ?.
For this assignment, you are going to implement that same thing as
you did for
your Java and Scheme projects, the clock patience program. To help you along, we're
giving you the Python code that takes the input card list and
builds a data structure to hold the results of the deal. You can use
this code as a starting point, or write it yourself if you like.
The code is:
#***************************************************************
# each card is represented as a list. For example,
# the queen of spades is ['Q', 'S']
# The hand itself is stored in 13 'stacks', from the
# Ace stack to King. To make this searchable, the
# name of the stack is stored with the stack. For
# example [5, ['K', 'D'], [2, 'C']] means that the stack associated
# with 5 has a K of diamonds on top of a two of clubs.
#
# So, an entire hand might look something like:
# [['A', ['J', 'C'], [2, 'H'], [3, 'S'],[4, 'D']], [2, [5, 'D'], ['Q', 'D'], [7, 'H'], [2, 'C']] ..., [K ...]]
#
#To run: python patience.py
# The cards are stacked onto the piles from left to right,
# the rightmost card is top (st.push() adds elements from left to right)
#
#To play: patience(sample())
#***************************************************************
def patience(hand):
hand = deal(hand,'K')
return play(['T','S'], hand) # here the T of spades is hardcoded - you
# you will need to replace this with whatever
# is on top of the king's pile for a given deal.
def play(card, hand): # the heart of the program. The 'hand' hold
# the current card distribution as stacks
# 'card' is the current card we are operating
# on
return hand
#***************************************************************
def sample():
return [['T','S'],['Q','C'],[8,'S'],[8,'D'],['Q','H'],[2,'D'],[3,'H'],['K','H'],[9,'H'],[2,'H'],['T','H'],['K','S'],['K','C'],[9,'D'],['J','H'],[7,'H'],['J','D'],[2,'S'],['Q','S'],['T', 'D'],[2,'C'],[4,'H'],[5,'H'],['A','D'],[4,'D'],[5,'D'],[6,'D'],[4, 'S'],[9,'S'],[5,'S'],[7,'S'],['J','S'],[8,'H'],[3,'D'],[8,'C'],[3,'S'],[4,'C'],[6,'S'],[9,'C'],['A','S'],[7,'C'],['A','H'],[6,'H'],['K','D'],['J','C'],[7,'D'],['A','C'],[5,'C'],['T','C'],['Q','D'],[6,'C'],[3,'C']]
def deal(cards, rank):
if len(cards) == 0:
return initialHand()
else:
return addStack(rank, cards[0], deal(cards[1:],nextRank(rank)))
def initialHand():
return [['A'],[2],[3],[4],[5],[6],[7],[8],[9],['T'],['J'],['Q'],['K']]
def nextRank(rank): # this function just facilitates dealing
if rank == 'K':
return 'Q'
elif rank == 'Q':
return 'J'
elif rank == 'J':
return 'T'
elif rank == 'T':
return 9
elif rank == 9:
return 8
elif rank == 8:
return 7
elif rank == 7:
return 6
elif rank == 6:
return 5
elif rank == 5:
return 4
elif rank == 4:
return 3
elif rank == 3:
return 2
elif rank == 2:
return 'A'
elif rank == 'A':
return 'K'
def addStack(rank, card, hand): # find correct pile and then push
if hand[0][0]==rank:
hand[0].append(card)
return hand
else:
return [hand[0]] + addStack(rank, card, hand[1:])
def pushStack(card, st):
st.append(card)
def match(st,rank):
card = [x for x in st if x[0]==rank]
return card[0]
#plays the game:
print patience(sample())
##Other things to try for testing purposes:
#hand = deal(sample(), 'K')
#print hand
#onepile = match(hand,'T')
#print onepile
#play(pickTopCard(y,'K'),y,1)