-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack21_score.pl
More file actions
227 lines (175 loc) · 5.16 KB
/
stack21_score.pl
File metadata and controls
227 lines (175 loc) · 5.16 KB
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
#!/usr/bin/perl
## Stack21
## You have $number_of_stacks and a deck of cards.
## Come as close to 21 as you can get in those $number_of_stacks piles.
## You have $passes skip cards.
##
## By Augie De Blieck Jr.
## 02 February 2006
##
use strict;
use stack;
use deck;
use card;
## Subroutines (at the end of script):
## ------------------------------------
## * create_stacks
## * show_stacks
## * find_total (of all stacks)
##
my @stacks; # Series of stack objects
my $number_of_stacks = 8;
my $passes = 3; # Number of skips left to play
my $no_new_card = 0;
my $score = 0;
my $counter = 1;
my $choice = 0;
my $top_card = 0;
##
## Create stacks, deck
##
create_stacks( $number_of_stacks, \@stacks );
my $deck = deck->new();
$deck->shuffle();
MAINGAME:
while (
( find_total( \@stacks, $number_of_stacks ) != ( $number_of_stacks * 21 ) )
&& ( $choice !~ /END/i ) )
{
if ( $no_new_card == 0 ) {
$top_card = $deck->get_next_card();
$no_new_card = 0;
}
$score = find_total( \@stacks, $number_of_stacks );
show_stacks( $number_of_stacks, @stacks );
my $deck_size = $deck->count_cards();
my $card = $top_card->get_value_english;
my $suit = $top_card->get_suit_english;
print qq~
SCORE: $score PASSES LEFT: $passes CARDS LEFT: $deck_size
-----------------------------------------------------------
NEW CARD: $card of $suit
Please select a stack (1 - $number_of_stacks), (E)ND, or (P)ASS:~;
chomp( $choice = <STDIN> );
print "\n\n\n\n\n\n\n";
## If ENDING THE GAME
if ( $choice =~ /(E|END)/i ) {
$choice = "END";
last MAINGAME;
}
## If PASSING on a card
if ( $choice =~ /(P|PASS)/i ) {
if ( $passes > 0 ) {
$passes--;
$no_new_card = 0;
}
else {
$no_new_card = 1;
print "SORRY - YOU'RE OUT OF PASSES\n\n\n";
}
next MAINGAME;
}
if ( ( $choice !~ /\d/ )
|| ( $choice > $number_of_stacks )
|| ( $choice < 1 ) )
{
$no_new_card = 1;
print "SORRY - INVALID CHOICE - PLEASE TRY AGAIN\n\n\n";
next MAINGAME;
}
## If a STACK IS CHOSEN
## Is STACK FULL or CLOSED or CAN'T HOLD THIS CARD?
if ( $stacks[$choice]->is_full ) {
$no_new_card = 1;
print "SORRY - THAT STACK IS ALREADY FULL\n";
next MAINGAME;
}
elsif (( $stacks[$choice]->stack_score == 21 )
&& ( $stacks[$choice]->has_an_ace == 0 ))
{
print "That stack is closed at 21. Please choose another.\n";
$no_new_card = 1;
}
elsif (( $stacks[$choice]->stack_score + $top_card->value_of() > 21 )
&& ( $top_card->get_value() eq 'A' ) )
{
## Total is greater than 21, but you've drawn an ACE, so it's 1, not 11
$stacks[$choice]->add_card($top_card);
$no_new_card = 0;
}
elsif ( ( $stacks[$choice]->stack_score + $top_card->value_of() > 21 )
&& $stacks[$choice]->has_an_ace == 0 )
{
## Total is greater than 21 without an ace -- you're busted
$no_new_card = 1;
print "\n SORRY - THAT WOULD BUST YOUR STACK\n";
next MAINGAME;
}
elsif ( $choice > $number_of_stacks ) {
## Invalid key stroke
$no_new_card = 1;
print "INVALID STACK NUMBER (1 to $number_of_stacks)\n\n\n";
next MAINGAME;
}
else {
## I have no further reason to say no. Add the card to the stack.
$stacks[$choice]->add_card($top_card);
$no_new_card = 0;
}
}
print "\n\n\n";
print "Congrats, your final score is $score.\n";
# Functions ------------------------------------------
sub create_stacks {
my $maxstacks = shift;
my $stacks = shift;
foreach my $counter ( 1 .. $maxstacks ) {
@$stacks[$counter] = stack->new;
}
return;
}
sub show_stacks {
my $many_stacks = shift;
my @stacks = @_;
print "\n# --------------------------------------------------------------- #\n";
foreach my $mystack ( 1 .. $many_stacks ) {
print "STACK #$mystack: (" . $stacks[$mystack]->stack_score;
my $spaces = 10;
if ( $stacks[$mystack]->stack_score > 9 ) {
$spaces = 9;
}
if ( $stacks[$mystack]->is_soft21 ) {
print " SOFT) ";
}
else {
print ")" . ( " " x $spaces );
}
foreach my $position ( 1 .. 5 ) {
my $tmp_card = "card$position";
if ( $stacks[$mystack]->{$tmp_card} eq "0" ) {
print "-- ";
}
else {
my $tmp = $stacks[$mystack]->{$tmp_card};
my $formatted = $tmp->get_value . $tmp->get_suit;
print $formatted . " ";
}
}
print "\n";
}
return;
}
sub find_total {
## Add up all stacks
## for final score
my $self = shift;
my $num_stacks = shift;
my $counter = 1;
my $total = 0;
while ( $counter < ( $num_stacks + 1 ) ) {
$total += @$self[$counter]->stack_score;
$counter++;
}
print "\n";
return $total;
}