Loading ...
Sorry, an error occurred while loading the content.

Re: question & learning about developing library funtions "lib.pl"

Expand Messages
  • draktrax
    ... could be ... my code ... the perl ... select ... randomize the ... array. ... random card ... cards ... write an ... Same vein, different approach
    Message 1 of 4 , Sep 1, 2008
    • 0 Attachment
      --- In perl-beginner@yahoogroups.com, "fast.linux" <fast.linux@...> wrote:
      >
      > Thanks for the suggestions and thanks for the critique of my shuffling
      > code. Your examples are very cool. I'm off to tweak my code a bit
      > more. Thanks for your help.
      >
      > --- In perl-beginner@yahoogroups.com, "Charles K. Clarkson"
      > <cclarkson@> wrote:
      > >
      > > fast.linux wrote:
      > >
      > > : Here is a basic card shuffling program I wrote. I am sure it
      could be
      > > : tweaked but it works.
      > > :
      > > : I am learning about developing perl libraries. How do I remove
      my code
      > > : that does the shuffling and include it as another function in a
      > > : lib.pl.
      > >
      > > You don't. At least, not in modern perl. We tend to use modules to
      > > hold reusable code. You can learn more about modules by reading
      the perl
      > > documentation. You might also have a look at the source for the
      > > List::Util module. It has a shuffle routine in it.
      > >
      > > This is a great first effort for shuffling cards. The algorithm
      > > simulates a human splitting the deck into two equal piles and
      > > interlacing the two piles back into one deck.
      > >
      > > Humans have to shuffle cards because we are unable to easily
      select
      > > random cards from a deck of cards. It's more convenient to
      randomize the
      > > deck and to deal from the top than it is to randomly choose several
      > > cards from a deck.
      > >
      > > Computers are good at choosing random items from a list or an
      array.
      > > If we look at the deck of cards as an array, then choosing a
      random card
      > > is trivial. For example, to select a single card from the deck of
      cards
      > > in your program we only need this:
      > >
      > > my $random_card = $startingdeck[rand @startingdeck];
      > >
      > >
      > > If we pop that card out of the deck array, then we can randomly
      > > select a card from the remaining deck without fear of selecting the
      > > first card. Let's place the deck into an array named @deck and
      write an
      > > example of this.
      > >
      > > # Select a first random card
      > > my $first_random_card_index = rand @deck;
      > > printf "The first random card is:\t%s\n",
      > $deck[$first_random_card_index];
      > >
      > > # Remove this card
      > > splice @deck, $first_random_card_index, 1;
      > >
      > > # Select a second random card
      > > my $second_random_card_index = rand @deck;
      > > printf "The second random card is:\t%s\n",
      > $deck[$second_random_card_index];
      > >
      > > # Remove this card
      > > splice @deck, $second_random_card_index, 1;
      > >
      > >
      > > So, to print three random cards we could do this.
      > >
      > > #!/usr/bin/perl
      > >
      > > use strict;
      > > use warnings;
      > >
      > > foreach my $position ( qw(first second third) ) {
      > > printf "The %s random card is:\t%s\n", $position,
      > remove_random_card();
      > > }
      > >
      > > BEGIN {
      > > my @deck = qw(
      > > 2H 3H 4H 5H 6H 7H 8H 9H 10H JH QH KH AH
      > > 2D 3D 4D 5D 6D 7D 8D 9D 10D JD QD KD AD
      > > 2C 3C 4C 5C 6C 7C 8C 9C 10C JC QC KC AC
      > > 2S 3S 4S 5S 6S 7S 8S 9S 10S JS QS KS AS
      > > );
      > >
      > > sub remove_random_card {
      > >
      > > # Select a random card
      > > my $random_card_index = rand @deck;
      > >
      > > # Remove this card
      > > my $random_card = splice @deck, $random_card_index, 1;
      > >
      > > # Return the card name
      > > return $random_card;
      > > }
      > > }
      > >
      > > __END__
      > >
      > >
      > > Still, many people prefer to work with a full deck of randomized
      > > cards. We could roll our own shuffle ...
      > >
      > > #!/usr/bin/perl
      > >
      > > use strict;
      > > use warnings;
      > >
      > > my @deck = qw(
      > > 2H 3H 4H 5H 6H 7H 8H 9H 10H JH QH KH AH
      > > 2D 3D 4D 5D 6D 7D 8D 9D 10D JD QD KD AD
      > > 2C 3C 4C 5C 6C 7C 8C 9C 10C JC QC KC AC
      > > 2S 3S 4S 5S 6S 7S 8S 9S 10S JS QS KS AS
      > > );
      > >
      > > my @shuffled_deck = @{ shuffle_deck( \@deck ) };
      > >
      > > my $i = 0;
      > > foreach my $position ( qw(first second third fourth fifth ) ) {
      > > printf "The %s random card is:\t%s\n", $position,
      > $shuffled_deck[$i++];
      > > }
      > >
      > > sub shuffle_deck {
      > >
      > > # Work on copy of the deck
      > > my @deck = @{ $_[0] };
      > >
      > > my @shuffled_deck;
      > >
      > > foreach ( @deck ) {
      > > push @shuffled_deck, splice @deck, rand @deck, 1;
      > > }
      > >
      > > return \@shuffled_deck;
      > > }
      > >
      > > __END__
      > >
      > > ... Or we could re-use a (much) more efficient method from the
      > > List::Util module.
      > >
      > > #!/usr/bin/perl
      > >
      > > use strict;
      > > use warnings;
      > >
      > > use List::Util 'shuffle';
      > >
      > > my @shuffled_deck = shuffle(
      > > qw(
      > > 2H 3H 4H 5H 6H 7H 8H 9H 10H JH QH KH AH
      > > 2D 3D 4D 5D 6D 7D 8D 9D 10D JD QD KD AD
      > > 2C 3C 4C 5C 6C 7C 8C 9C 10C JC QC KC AC
      > > 2S 3S 4S 5S 6S 7S 8S 9S 10S JS QS KS AS
      > > )
      > > );
      > >
      > > my $i = 0;
      > > foreach my $position ( qw(first second third fourth fifth ) ) {
      > > printf "The %s random card is:\t%s\n", $position,
      > $shuffled_deck[$i++];
      > > }
      > >
      > >
      > > __END__
      > >
      > >
      > > HTH,
      > > Charles K. Clarkson
      > > --
      > > Mobile Homes Specialist
      > > Free Market Advocate
      > > Web Programmer
      > >
      > > 254 968-8328
      > >
      > > http://www.clarksonenergyhomes.com/wordpress/about/
      > >
      >

      Same vein, different approach

      #!/usr/bin/perl -w

      $c = 0;
      foreach $b ("Spades", "Hearts", "Clubs", "Diamonds") {
      foreach $a ("Ace", 2..10, "Jack", "Queen", "King") {
      $deck[$c] = "$a of $b\n";
      $c++;
      }
      }

      $d = 1;
      @ran_ten = ();
      while ($d < 11) {
      $card = $deck [rand @deck];
      push (@ran_ten, $card);
      if (grep (/$card/, @ran_ten) == 2) {
      delete ($ran_ten[-1]);
      next;
      }
      $d++;
      }

      print " @ran_ten";
    Your message has been successfully submitted and would be delivered to recipients shortly.