By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,197 Members | 974 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,197 IT Pros & Developers. It's quick & easy.

Need help pulling in library sub to shuffle deck of cards

P: 5
I have a file well call file.pl. It's a card sorting program. I need to create a lib fuction with part of the original file that shuffles the deck of cards. After it shuffles the first deck and deals a hand of cards I need it to call the shuffling function again before dealing another hand. I call the lib function file file-lib.pl.

here's the contents i have for file.pl

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. require 'file-lib.pl';
  4. my @startingdeck = ("A H","2 H","3 H","4 H","5 H","6 H","7 H","8 H",
  5.                  "9 H","10 H","J H","Q H","K H",
  6.                  "A D","2 D","3 D","4 D","5 D","6 D","7 D","8 D",
  7.                  "9 D","10 D","J D","Q D","K D",
  8.                  "A C","2 C","3 C","4 C","5 C","6 C","7 C","8 C",
  9.                  "9 C","10 C","J C","Q C","K C",
  10.                  "A S","2 S","3 S","4 S","5 S","6 S","7 S","8 S",
  11.                  "9 S","10 S","J S","Q S","K S");
  12. push@startingdeck, @startingdeck;
  13. print "@startingdeck[0..4]\n"
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxx

And here are the contents for file-lib.pl

Expand|Select|Wrap|Line Numbers
  1. sub shuffle(){
  2. for my $x(0..99){
  3. my @shuffle = (
  4.   shift(@startingdeck),
  5.   pop(@startingdeck),
  6.   shift(@startingdeck),
  7.   pop(@startingdeck),
  8.   shift(@startingdeck),
  9.   pop(@startingdeck),
  10.   shift(@startingdeck),
  11.   pop(@startingdeck),
  12. );
  13.  }
  14. }
  15. 1;
i realize some of this may not be what is considered good perl programming techniques. But I merely just want it to work.
Nov 13 '08 #1
Share this Question
Share on Google+
8 Replies


KevinADC
Expert 2.5K+
P: 4,059
If the object is to shuffle the deck you can use List::Util and call its shuffle function.

http://search.cpan.org/~gbarr/Scalar...b/List/Util.pm

Or is this school work?
Nov 13 '08 #2

P: 5
Not school work. Can u show me how it should look?
Nov 14 '08 #3

Ganon11
Expert 2.5K+
P: 3,652
A simple example of List::Util::shuffle:

Expand|Select|Wrap|Line Numbers
  1. use List::Util qw/shuffle/;
  2.  
  3. my @sorted_list = (1..100);
  4. my @unsorted_list = shuffle(@sorted_list);
  5.  
  6. print @unsorted_list, "\n"; # Will print 1-100 in some random order
Nov 14 '08 #4

P: 5
can u show me how my code should look if i didn't want to use List::Util::shuffle:
Nov 14 '08 #5

KevinADC
Expert 2.5K+
P: 4,059
can u show me how my code should look if i didn't want to use List::Util::shuffle:
Your code will not shuffle the array. Don't even bother with it unless you meant something else besides shuffling.

Look here:

http://perldoc.perl.org/perlfaq4.htm...ay-randomly%3f

Use the Fisher-Yates shuffle example to shuffle your array. If you still need help, post the code you tried to write using the Fisher-Yates example.
Nov 14 '08 #6

P: 5
From my original program below I want to remove the code that does the shuffling and include it as a function in let's say mylibfile.pl. Then from myfile.pl using (require 'mylibfile.pl') do the following. Deal a hand of cards then call the shuffling function again before dealing another, different hand.

(myfile.pl)

Expand|Select|Wrap|Line Numbers
  1. my @startingdeck = ("A H","2 H","3 H","4 H","5 H","6 H","7 H","8 H", 
  2.                  "9 H","10 H","J H","Q H","K H", 
  3.                  "A D","2 D","3 D","4 D","5 D","6 D","7 D","8 D", 
  4.                  "9 D","10 D","J D","Q D","K D", 
  5.                  "A C","2 C","3 C","4 C","5 C","6 C","7 C","8 C", 
  6.                  "9 C","10 C","J C","Q C","K C", 
  7.                  "A S","2 S","3 S","4 S","5 S","6 S","7 S","8 S", 
  8.                  "9 S","10 S","J S","Q S","K S"); 
  9. for my $x(0..99){ 
  10. my @shuffle = ( 
  11.   shift(@startingdeck), 
  12.   pop(@startingdeck), 
  13.   shift(@startingdeck), 
  14.   pop(@startingdeck), 
  15.   shift(@startingdeck), 
  16.   pop(@startingdeck), 
  17.   shift(@startingdeck), 
  18.   pop(@startingdeck), 
  19. );
  20. push@startingdeck, @startingdeck; 
  21. print "@startingdeck[0..4]\n"
Nov 14 '08 #7

KevinADC
Expert 2.5K+
P: 4,059
Well, that wasn;t much of an effort on your part. Here it is, next time I hope to see you at least try.


myfile.pl:

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3. require mylibfile.pl;#<--assumes it is in the same folder as myfile.pl
  4.  
  5. my @deck = ("A H","2 H","3 H","4 H","5 H","6 H","7 H","8 H", 
  6. "9 H","10 H","J H","Q H","K H", 
  7. "A D","2 D","3 D","4 D","5 D","6 D","7 D","8 D", 
  8. "9 D","10 D","J D","Q D","K D", 
  9. "A C","2 C","3 C","4 C","5 C","6 C","7 C","8 C", 
  10. "9 C","10 C","J C","Q C","K C", 
  11. "A S","2 S","3 S","4 S","5 S","6 S","7 S","8 S", 
  12. "9 S","10 S","J S","Q S","K S");
  13.  
  14. my @shuffled = shuffle(\@deck);
  15. print "@shuffled[0..4]";#<-- demonstration only
  16.  
mylibfile.pl:

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3.  
  4. sub shuffle {
  5.    my $deck = shift;
  6.    my $i = @$deck;
  7.    while (--$i) {
  8.       my $j = int rand ($i+1);
  9.       @$deck[$i,$j] = @$deck[$j,$i];
  10.    }
  11.    return @{$deck};
  12. }
  13. 1;#<--keep this here
  14.  
just call the shuffle() function as needed to reshuffle the deck.
Nov 14 '08 #8

P: 5
thanks for the help. ........................................
Nov 17 '08 #9

Post your reply

Sign in to post your reply or Sign up for a free account.