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

issue with my subroutine/library script.

Expand Messages
  • fast.linux
    I created a card shuffling program and it works fine. However, for my project I am to place the shuffling part of my code in a separate script as a subroutine.
    Message 1 of 2 , Sep 3, 2008
    • 0 Attachment
      I created a card shuffling program and it works fine. However, for my
      project I am to place the shuffling part of my code in a separate
      script as a subroutine.

      I am to remove the code that does the shuffling and include it as
      another function in obj13-lib.pl. So I did this and after the first
      "hand" is dealt, call the shuffling function again before dealing
      another, different hand.

      I call it obj-lib.pl and my main script obj13.pl.

      I did put it together but my obj13.pl script is not shuffling my cards
      and I need to print out two sets of five elements. And I was not able
      to get it to print out 2 sets of five elements.

      Where am I going wrong? Looking for some help and a point in the right
      direction. Thanks everyone.

      Below is my code:
      obj13.pl

      #!/usr/bin/perl

      require 'obj13-lib.pl';

      my @startingdeck = ("A H","2 H","3 H","4 H","5 H","6 H","7 H","8 H",
      "9 H","10 H","J H","Q H","K H",
      "A D","2 D","3 D","4 D","5 D","6 D","7 D","8 D",
      "9 D","10 D","J D","Q D","K D",
      "A C","2 C","3 C","4 C","5 C","6 C","7 C","8 C",
      "9 C","10 C","J C","Q C","K C",
      "A S","2 S","3 S","4 S","5 S","6 S","7 S","8 S",
      "9 S","10 S","J S","Q S","K S");


      print "the top five cards are @startingdeck[0..4]\n";

      #_END_
      _________________________________________________________________________
      Here is my second library script for my shuffle program:

      obj13-lib.pl

      #!/usr/bin/perl

      sub shuffle(){

      my @right;
      my @left;
      SHUFFLE:
      unshift @left, pop @startingdeck for 1..26;
      @right = @startingdeck;
      @startingdeck = ();
      while(@left or @right){
      if (rand() < 0.5){
      @left and push @startingdeck, shift @left
      }else{
      @right and push @startingdeck, shift @right
      }
      };

      rand() < 0.9 and goto SHUFFLE;
      }

      1;

      #_END_
    • Charles K. Clarkson
      ... Are you following a course on this? How old is the course. ... [snip] You never even call shuffle() the first time. The require call does not automatically
      Message 2 of 2 , Sep 3, 2008
      • 0 Attachment
        fast.linux wrote:

        : I am to remove the code that does the shuffling and include it as
        : another function in obj13-lib.pl.

        Are you following a course on this? How old is the course.


        : So I did this and after the first "hand" is dealt, call the shuffling
        : function again before dealing another, different hand.
        [snip]

        You never even call shuffle() the first time. The require call
        does not automatically run the subroutines in obj13-lib.pl.


        : Where am I going wrong? Looking for some help and a point in the right
        : direction. Thanks everyone.
        :
        : Below is my code:
        : obj13.pl
        :
        : #!/usr/bin/perl
        :
        : require 'obj13-lib.pl';
        :
        : my @startingdeck = ("A H","2 H","3 H","4 H","5 H","6 H","7 H","8 H",
        : "9 H","10 H","J H","Q H","K H",
        : "A D","2 D","3 D","4 D","5 D","6 D","7 D","8 D",
        : "9 D","10 D","J D","Q D","K D",
        : "A C","2 C","3 C","4 C","5 C","6 C","7 C","8 C",
        : "9 C","10 C","J C","Q C","K C",
        : "A S","2 S","3 S","4 S","5 S","6 S","7 S","8 S",
        : "9 S","10 S","J S","Q S","K S");
        :
        :
        : print "the top five cards are @startingdeck[0..4]\n";
        :
        : #_END_
        [snip]

        There is no code here that calls shuffle(). If you do not call it,
        it won't run. This prints the first five cards of the ordered deck
        every time.

        the top five cards are A H 2 H 3 H 4 H 5 H


        HTH,

        Charles K. Clarkson
        --
        Mobile Homes Specialist
        Free Market Advocate
        Web Programmer

        254 968-8328

        http://www.clarksonenergyhomes.com/wordpress/about/
      Your message has been successfully submitted and would be delivered to recipients shortly.