/
solitare.pl
executable file
·241 lines (197 loc) · 6.32 KB
/
solitare.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#!/usr/bin/perl -Ilib
# need to make the first element of an array (0) to always be the top
# of any stack as it might sit on the table
use Card;
use strict;
my $debug = 3;
my $deck = Card->new();
my %table;
&deal_table();
while ( &draw_hand ) {
my $count = 0;
while ( &move_aces() ) { &flip_all_cards(); $count++; }
$count = 0 and &debug_table() if $count;
while ( &move_tableau() ) { &flip_all_cards(); $count++; }
$count = 0 and &debug_table() if $count;
while ( &play_card_in_hand() ) { &flip_all_cards(); $count++; }
$count = 0 and &debug_table() if $count;
}
### Subroutines
=head2 deal_table()
Deals out the cards for a standard solitaire starting position.
=cut
sub deal_table {
for my $col ( 1 .. 7 ) { $table{$col}{showing} = []; }
for ( my $start = 1; $start < 8; $start++ ) {
for my $col ( $start .. 7 ) {
my $card = $deck->draw();
unshift @{$table{$col}{hidden}}, $card;
}
}
$table{AC} = [];
$table{AD} = [];
$table{AH} = [];
$table{AS} = [];
for ( 1 .. 3 ) {
my $card = $deck->draw();
unshift @{$table{hand}}, $card;
}
$table{discard} = [];
&flip_all_cards();
&debug_table() if $debug;
}
=head2 debug_table()
Displays the current standing of the table
=cut
sub debug_table {
return undef unless $debug > 1;
print STDERR "\n";
for my $col ( 1 .. 7 ) {
print STDERR "Tableau $col : (", join(',', @{$table{$col}{hidden}}), ") ",
join(',', @{$table{$col}{showing}}), "\n";
}
for my $ace ( qw/AC AD AH AS/ ) {
print STDERR "Ace $ace : ", join(', ', @{$table{$ace}}), "\n";
}
print STDERR "In Hand : ", join(', ', @{$table{hand}}), "\n";
print STDERR "In Discard : ", join(', ', @{$table{discard}}), "\n\n";
}
=head2 draw_hand()
Discards the current hand and draws three cards. Returns the number of cards drawn
=cut
sub draw_hand {
while ( scalar(@{$table{hand}}) > 0 ) {
push @{$table{discard}}, shift @{$table{hand}};
}
for ( 1 .. 3 ) {
my $card = $deck->draw();
next unless $card;
unshift @{$table{hand}}, $card;
}
print STDERR "Drawing cards\n" if $debug;
return scalar(@{$table{hand}});
}
=head2 flip_all_cards()
Goes through the table and turns over the top card on any un-turned stacks.
=cut
sub flip_all_cards {
for my $col ( 1 .. 7 ) {
if ( scalar(@{$table{$col}{showing}}) == 0 and scalar(@{$table{$col}{hidden}}) > 0 ) {
push @{$table{$col}{showing}}, shift @{$table{$col}{hidden}};
}
}
&debug_table() if $debug;
}
=head2 need_aces()
Returns a hash or hashref keyed by the cards that are currently needed on
the ace stacks. The value for that key is the stack on which it belongs.
=cut
sub need_aces {
my %need;
for my $stack ( qw/AC AD AH AS/ ) {
$need{$stack} = $stack and next unless scalar(@{$table{$stack}}) > 0;
my ($value,$suit) = split '', $table{$stack}[0];
next if $value eq 'K';
my $new = $value eq 'A' ? '2'
: $value =~ /^[2-8]$/ ? $value + 1
: $value eq '9' ? '0'
: $value eq '0' ? 'J'
: $value eq 'J' ? 'Q'
: $value eq 'Q' ? 'K'
: $value; # ERROR!
warn "Error computing next ace value based on $table{$stack}[0]\n" if $new eq $value;
$need{$value.$suit} = $stack;
}
print "Need Aces: ", join(', ', keys %need), "\n" if $debug > 2;
return wantarray ? %need: \%need;
}
=head2 need_tableau()
Given a card, returns an arrayref of the two cards it can sit upon on the
tableau.
=cut
sub need_tableau {
my $card = shift @_;
my ($value,$suit) = split '', $card, 2;
return [] if $value eq 'K';
my $newval = $value =~ /^[2-8]$/ ? $value + 1
: $value eq 'A' ? '2'
: $value eq '9' ? '0'
: $value eq '0' ? 'J'
: $value eq 'J' ? 'Q'
: $value eq 'Q' ? 'K'
: $value; # ERROR!
if ( $suit eq 'C' or $suit eq 'S' ) {
return [ $newval.'D', $newval.'H' ];
} elsif ( $suit eq 'D' or $suit eq 'H' ) {
return [ $newval.'C', $newval.'S' ];
} else { die "Can't parse suit in &need_tableau()" }
}
=head2 move_aces()
Checks all currently showing cards and the hand to see if any of them
can be put on the aces and then moves them there, returning the number of
cards moved.
=cut
sub move_aces {
my $need = &need_aces();
my $count = 0;
for my $needed ( keys %$need ) {
# Table
for my $col ( 1 .. 7 ) {
if ( $table{$col}{showing}->[0] eq $needed ) {
push @{$table{$need->{$needed}}}, shift @{$table{$col}{showing}};
print STDERR "MOVE: $needed from $col to $need->{$needed}\n" if $debug > 1;
$count++;
}
}
# Hand
if ( $table{hand}->[0] eq $needed ) {
push @{$table{$need->{$needed}}}, shift @{$table{hand}};
print STDERR "MOVE: $needed from the hand to $need->{$needed}\n" if $debug > 1;
$count++;
}
}
print STDERR "No acestack cards to move.\n";
return $count;
}
sub move_tableau {
for my $col ( 1 .. 7 ) {
next if scalar(@{$table{$col}{showing}}) < 1;
my $card = $table{$col}{showing}->[-1];
my $test = &need_tableau($card);
for my $test_card ( @$test ) {
#print STDERR "$card on the top of the column $col needs $test_card\n";
for my $check_col ( 1 .. 7 ) {
next if $col == $check_col; # Don't apply this card to itself
next unless $test_card eq $table{$check_col}{showing}->[0];
print STDERR "Moving tableau stack on $card (needing $test_card) to $table{$check_col}{showing}->[0].\n";
my @lift = @{$table{$col}{showing}};
$table{$col}{showing} = [];
push @{$table{$check_col}{showing}}, @lift;
return 1;
}
}
}
print STDERR "No cards to move on the tableau\n";
return 0;
}
sub play_card_in_hand {
if ( scalar(@{$table{hand}}) < 1 ) {
print STDERR "Hand empty\n";
return 0;
}
my $handcard = $table{hand}->[0];
my $desired = &need_tableau($handcard);
for my $col ( 1 .. 7 ) {
next if scalar(@{$table{$col}{showing}}) < 1;
my $card = $table{$col}{showing}->[0];
for my $desired_card ( @$desired ) {
next unless $card eq $desired_card;
print STDERR "Moving hand card $handcard (needing $desired_card) to $table{$col}{showing}->[0]?\n";
my $lift = shift @{$table{hand}};
push @{$table{$col}{showing}}, $lift;
return 1;
}
}
print STDERR "No cards to move from the hand\n";
return 0;
}