473,390 Members | 1,235 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,390 software developers and data experts.

Need help pulling in library sub to shuffle deck of cards

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
8 5029
KevinADC
4,059 Expert 2GB
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
Not school work. Can u show me how it should look?
Nov 14 '08 #3
Ganon11
3,652 Expert 2GB
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
can u show me how my code should look if i didn't want to use List::Util::shuffle:
Nov 14 '08 #5
KevinADC
4,059 Expert 2GB
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
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
4,059 Expert 2GB
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
thanks for the help. ........................................
Nov 17 '08 #9

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

Similar topics

23
by: JC | last post by:
I am very new to programming and learning on my own. Why do I keep getting duplicate values using this code? I want to shuffle a deck of 52 cards. The logic seems right to me. Randomize For...
0
by: Limpor | last post by:
Hello, I am new to learning java, and i am trying to build the class for a calculation card game, unfortunately i can't get the public Card top() and Card takeTop() method in the Stock class. Can...
0
by: Limpor | last post by:
Hi, Iím working on a solitaire game as my course assignment, and I am having trouble to dealing with Stock class, which consists of an upturned top card plus deck. The code for Stock class: import...
10
by: Arun Nair | last post by:
Can any one help me with this im not getting it even after reading books because there is not much of discussion anywhere a> Implement a calss that represents a playing card. The class should...
4
by: Pratik | last post by:
For the time being, I'm doing a simple swap method for my deck of cards using the random number generator in cstdlib. I've created a dynamic array of a type class Card. When I go to shuffle it and...
2
by: codie | last post by:
i need to sort a list of card like the rank sample: ACE of h, ACE of d, ACE of c, ACE of s, 2, 2, 2, 2 this is my code so can anyone help me??? class Card: suitList = rankList =
4
by: tvance929 | last post by:
Hey everyone, I created a theDeck class that creates a 52 card int List. Inside of this class I have a ShuffleCards method. I simply want 2 seperate decks that I can then shuffle and...
8
by: garyrowell | last post by:
I have been at this programme for hours trying to work out what is wrong. Any help would be very much appricated. Here is the breif I received. The program This week you are going to write three...
32
by: falconsx23 | last post by:
I am making a game called Set, it is a card game: here is a brief description of the rules: The game of Set is played with 81 cards, each of which has 4 properties (number, pattern, color and...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.