Ruby 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 Ruby 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: ruby patience.py
## OR use interactive mode: irb
##also 'ruby eval.rb', download eval.rb
# 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')
play(['T','S'], hand) # here the T of spades is hardcoded - you
# you will need to replace this with whatever
end # 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
hand
end
#***************************************************************
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 cards.length == 0 then
initialHand
else
addStack(rank, cards[0], deal(cards[1:],nextRank(rank)))
def initialHand
[['A'],[2],[3],[4],[5],[6],[7],[8],[9],['T'],['J'],['Q'],['K']]
def nextRank(rank) # this function just facilitates dealing
if rank == 'K' then
'Q'
elsif rank == 'Q' then
'J'
elsif rank == 'J' then
'T'
elsif rank == 'T' then
9
elsif rank == 9 then
8
elsif rank == 8 then
7
elsif rank == 7 then
6
elsif rank == 6 then
5
elsif rank == 5 then
4
elsif rank == 4 then
3
elsif rank == 3 then
2
elsif rank == 2 then
'A'
elsif rank == 'A' then
'K'
def addStack(rank, card, hand) # find correct pile and then push
if hand[0][0]==rank then
[hand[0].push(card)] + hand[1..hand.length-1]
else
[hand[0]] + addStack(rank, card, hand[1..hand.length-1])
end
end
def pushStack(card, st):
st.append(card)
#use: st.assoc(rank) in Ruby
#Python vers:
#def match(st,rank):
# card = [x for x in st if x[0]==rank]
# return card[0]
#plays the game:
patience(sample)
##Other things to try for testing purposes:
#hand = deal(sample, 'K')
#print hand
#onepile = hand.assoc('T')
#print onepile
#play(pickTopCard(y,'K'),y,1)