shuffle a deck of cards
and take the top card,
then prompt to the user if the next card will be higher or lower than this card,
after they answer the next card in the deck is shown,
if the user was right the program will loop and flip another first card over..ask them again...
but if they get it wrong the program exits.
####
Shuffle deck;
pop top card
while( cards_left ) {
show card
prompt Higher or Lower?
Get answer
next_card = pop cards
last unless next_card > top_card and answer = Higher
or next_card < top_card and answer = Lower
top_card == next_card
}
Game over.
####
s
#! perl -slw
use strict;
use List::Util qw[shuffle];
sub card_value{ $_[0] % 13 || 13 }
sub show_card{
my( $suit, $face ) = ( $_[0] / 13, $_[0] % 13 );
printf '[ %c %2s %c ] ',
3+$suit,
substr( ' A 2 3 4 5 6 7 8 910 J Q K', $face * 2, 2 ),
3+$suit;
}
my @cards = shuffle 0 .. 51;
my $top_card = pop @cards;
while( @cards ) {
show_card( $top_card );
my $answer = '';
$answer = do{
printf 'Will the next card be (H)igher or (L)ower? ';
} until $answer =~ m[^[H|L]]i;
my $next_card = pop @cards;
unless( card_value( $next_card ) >= card_value( $top_card )
and $answer =~ m[^H]i
or card_value( $next_card ) <= card_value( $top_card )
and $answer =~ m[^L]i ) {
show_card( $next_card );
print 'Bad luck! You lose.';
last;
}
$top_card = $next_card;
}
print 'Game over.';
print 'You beat the pack' unless @cards;