Perl 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 Perl 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: perl patience.pl
# 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) Not implemented yet...try these lines:
$rank = 'K';
$nextrank = nextRank($rank);
print "$rank, $nextrank\n";
@cards = ();
@currentcards = deal($rank, @cards);
print "Current cards:\n";
printCards(@currentcards);
#***************************************************************
################################################
#Not implemented yet in Perl
#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
#***************************************************************
sub 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']);
}
sub deal {
my ($rank, @cards) = @_;
my @tempst;
my @initCards = initialHand();
my @deck = sample();
my @card = $deck[0];
my $nextrank;
if (scalar(@cards) == 0) {
print "Adding to cards, rank= $rank\n";
for $el (0..$#initCards) {
print "$el: $initcards[$el]->[0]\n";
if ($initCards[$el]->[0] eq $rank) {
print "FOUND $rank\n";
addStack(\@card,\@{$initCards[$el]});
}
}
shift(@deck);
while (scalar(@deck) > 0) {
$rank = nextRank($rank);
@card = shift(@deck);
for $el (0..$#initCards) {
#print "$el: $initcards[$el]->[0]\n";
if ($initCards[$el]->[0] eq $rank) {
print "FOUND $rank\n";
addStack(\@card,\@{$initCards[$el]});
}
}
}
print "\n";
return @initCards;
}
}
sub initialHand {
return (['A'],[2],[3],[4],[5],[6],[7],[8],[9],['T'],['J'],['Q'],['K']);
}
sub nextRank {
my ($rank) = $_[0];
if ($rank eq 'K') { return "Q"; }
elsif ($rank eq 'Q') { return 'J';}
elsif ($rank eq 'J') { return 'T';}
elsif ($rank eq 'T' ) { return 9;}
elsif ($rank eq 9 ) { return 8;}
elsif ($rank eq 8 ) { return 7;}
elsif ($rank eq 7 ) { return 6;}
elsif ($rank eq 6 ) { return 5;}
elsif ($rank eq 5 ) { return 4;}
elsif ($rank eq 4 ) { return 3;}
elsif ($rank eq 3 ) { return 2;}
elsif ($rank eq 2 ) { return 'A';}
else# ($rank eq 'A' )
{ return 'K';}
}
sub addStack {
my @card; my @st=();
my $count = 0;
for my $aref (@_) {
if ($count ==0 ) { @card = pop @$aref; }
if ($count ==1 ) {
foreach my $el ($aref) {
push(@st,$el);
}
}
$count = $count+1;
}
push(@{$st[0]}, @card);
return @st;
}
sub printStack {
my @st=@_;
my @cards = @{$st[0]};
print "St: ($st[0][0], ";
foreach my $cd (@cards[1..$#cards]) {
print "($cd->[0],$cd->[1]) ";
#print "($cards[2][0],$cards[2][1]),$cards[3][0],$cards[3][1]))\n";
}
print ")\n";
}
sub printCards {
my (@cards) = @_;
foreach $el (@cards) {
printStack($el);
}
}
##Other things to try for testing purposes:
@cards = sample();
foreach $el (@cards) {
print "($el->[0],$el->[1]) ";
}
print "\n";
@cards = sample();
@initcards = initialHand();
@card = [8,'C'];
for $el (0..$#initcards) {
print "$el: $initcards[$el]->[0]\n";
if ($initcards[$el]->[0] eq 9) {
print "FOUND 9\n";
my @st = @{$initcards[$el]};
print printStack(addStack(\@card,\@{$initcards[$el]}));
}
}
$rank = 'K';
$nextrank = nextRank($rank);
print "$rank, $nextrank\n";
@cards = ();
@currentcards = deal($rank, @cards);
print "Current cards:\n";
printCards(@currentcards);