#!/usr/bin/perl use strict; use utf8; use warnings; ########## S U B R O U T I N E S ######################## #deck creator using a really simpla algorithm #DECK MAKER sub deck_maker { my @deck = (); #the deck for returning my @types = ('spades', 'hearts', 'diamonds', 'clubs'); my @cards = ( [2,3,4,5,6,7,8,9,10,'J','Q','K','A'], [2,3,4,5,6,7,8,9,10,'J','Q','K','A'], [2,3,4,5,6,7,8,9,10,'J','Q','K','A'], [2,3,4,5,6,7,8,9,10,'J','Q','K','A'] ); for ( my $i=0; $i < 4; $i++) { for ( my $j=0; $j < 13; $j++) { #pops an element from array my $newcard = pop($cards[$i]) . " of " . $types[$i]; push(@deck, $newcard); } } return @deck; #return the new deck } #SWAP sub swap_two { #really simple swap ($_[0], $_[1]) = ($_[1], $_[0]); } #SHUFFLE sub shuffle { #simple shuffling algorithm #there are 52 cards always but $#shuffle+1 can be used too... it`s useless #unless there are people that don`t know there are 52 cards in total. for (my $i=0; $i < 52; $i++) { my $card1 = int rand(52); my $card2 = int rand(52/2); &swap_two($_[$card1], $_[$card2]); } my (@cut1) = @_[0..($#$_+1)/2]; my (@cut2) = @_[($#$_+1)/2..$#$_+1]; push(@_ , @cut2); push(@_, @cut1); } #PICK A CARD sub pick_a_card { my $deck = shift; return pop @$deck; } sub show_my_hand { print "~~"x40,"\n"; foreach my $cards (@_) { print "[", $cards, "]"; } print "\n", "~~"x40,"\n"; } ################################################# # THE GAME STARTS BELOW ############################ ################################################ # init the deck and shuffle it my @deck = &deck_maker; &shuffle(@deck); # end of deck manipulations my (@hand) = (); push (@hand, &pick_a_card(\@deck)); push (@hand, &pick_a_card(\@deck)); push (@hand, &pick_a_card(\@deck)); push (@hand, &pick_a_card(\@deck)); push (@hand, &pick_a_card(\@deck)); &show_my_hand(@hand); #game loop here