High Low Game (PERL Coding)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Rhodge09
    New Member
    • Nov 2009
    • 17

    High Low Game (PERL Coding)

    Well this game is fairly easy you have to chose what you think the card is going to be either higher or lower or in the middle of the two images but im having a little bit of a glitch. Here is my code and sometimes it works but when the cards are like say 5 and 6 and i chose it to be higher and the image that comes up is a 8 but it says i lose thats what im having problems with and heres the site: http://ctech.smccme.ed u/~rhodge/cw/hilow/hilow.pl

    Code:
    #!/usr/bin/perl
    use CGI ':standard';
    use CGI::Carp "fatalsToBrowser";
    
    print header;
    
    #####variables#######
    $card1=param('card1');
    $card2=param('card2');
    $card3=param('card3');
    $pick=param('pick');
    
    $wcount=param('wcount');
    $lcount=param('lcount');
    $results=param('param');
    
    
    ######main script######
    
    print start_html('Hi Low Game');
    
    &hash;
    &cards;
    if($pick eq ' ' ){
        &form;
    }else{
        &results;
    }
    
    
    
    #####initializing a hash array w/a key value ######
    sub hash{
    %value=(
        "1",1, "2",1, "3",1, "4",1,
        "5",13, "6",13, "7",13, "8",13,
        "9",12, "10",12, "11",12, "12",12,
        "13",11, "14",11, "15",11, "16",11,
        "17",10, "18",10, "19",10, "20",10,
        "21",9, "22",9, "23",9, "24",9,
        "25",8, "26",8, "27",8, "28",8,
        "29",7, "30",7, "31",7, "32",7,
        "33",6, "34",6, "35",6, "36",6,
        "37",5, "38",5, "39",5, "40",5,
        "41",4, "42",4, "43",4, "44",4,
        "45",3, "46",3, "47",3, "48",3,
        "49",2, "50",2, "51",2, "52",2,
    );
    
    }
    
    
    ####The for loop -- initializer; test; increment/decrement####
    
    #for($x=1; $x<53; $x++){
    #    print"    the Key is $x <br>
    #        <img src=images/$x.png> <br>
    #        the value is $value{$x}
    #        <hr>
    #         ";
    #}
    
    ####first two cards chosen####
    sub cards{
        $card1=int(rand(52)+1);
        $card2=int(rand(52)+1);
       
        while($card2 == $card1){
        $card2=int(rand(52)+1);}
    
        print" <img src=images/$card1.png> <img src=images/$card2.png";
    
        $c1v=$value{$card1};
        $c2v=$value{$card2};
    
    }
    
    
    
    #######form to choose card#######
    sub form{
        print"
            <form method=post action=hilow.pl>
            <input type=hidden value='$c1v'>
            <input type=hidden value='$c2v'>
            <input type=radio name=pick value=Higher>Higher<br>
            <input type=radio name=pick value=Middle>Middle<br>
            <input type=radio name=pick value=Lower>Lower<br>
            <input type=hidden value='$wcount'>
            <input type=hidden value='$lcount'>
            <input type=submit value='Ba Bam'>
            </form>
        ";
    
    }
    
    
    
    #####win or loss computed#####
    sub results{
        $card3=int(rand(52)+1);
        while($card3 == $card1 || $card3 == $card2){
        $card3=int(rand(52)+1);}
       
        $c3v=$value{$card3};
    
        if($pick eq "Higher" && $card3 > $card1 && $card3 > $card2){
        $results='You Win!';
        }elsif($pick eq "Middle" && $card3 > $card1 && $card3 < $card2 ||
                 $pick eq "Middle" && $card3 < $card1 && $card3 > $card2){
                      $results='You Win!';
        }else{$results='You Lose!';}
    
        print"<img src=images/$card3.png> <br /> $results";
    
    }
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Have you considered printing all of the values for each turn to see what each card is for each hand to try and determine where you're going wrong?

    For example print:
    Code:
    else{$results='You Lose! You picked:".$pick." card1 was: ".$card1.", card2 was: ".$card2.", card3 was: ".$card3;}

    Comment

    • Rhodge09
      New Member
      • Nov 2009
      • 17

      #3
      Well eventually i want the two images on the first page to go over to the second page but not change and have the random card "card3" to be in the middle.

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Sounds cool but first we should try to debug why your logic isn't correct :)

        -Frinny

        Comment

        • Rhodge09
          New Member
          • Nov 2009
          • 17

          #5
          I just tried your method and all that happened was it showed two random images on the next page

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            Maybe I wasn't all that clear about what I was recommending you try.

            Don't print the image because this in-itself could be a problem (you could be grabbing the wrong image).

            Just print the values that were generated....wh en the output is wrong (when you lose instead of wining) take a look at what the actual values were and compare them to your IF statements.

            From there you should be able to tell if you are missing a piece of logic, or if your logic does not properly describe the cases for winning....


            By the way, your link doesn't work...I can't access the website.

            -Frinny

            Comment

            • Rhodge09
              New Member
              • Nov 2009
              • 17

              #7

              Comment

              • Rhodge09
                New Member
                • Nov 2009
                • 17

                #8
                I really dont know i've tried everything that you told me and none of it works.

                Comment

                • Frinavale
                  Recognized Expert Expert
                  • Oct 2006
                  • 9749

                  #9
                  I went to the site and I do not see the output that I recommended for debugging.

                  Comment

                  • Rhodge09
                    New Member
                    • Nov 2009
                    • 17

                    #10
                    I did what you said and two random images came up tell me exactly what you want me to take out and what you want me to add

                    Comment

                    • Frinavale
                      Recognized Expert Expert
                      • Oct 2006
                      • 9749

                      #11
                      You have the following:
                      Code:
                       $card1=param('card1');
                       $card2=param('card2');
                       $card3=param('card3');
                      But your hidden fields (that I'm assuming should be storing these values) are not given an ID or Name.

                      Are you sure that you are retrieving $card1 and $card2 from param() properly?
                      Are you always calling the cards() method? Should you be? Or should you only be calling the method if the $card1 and $card2 variables are empty?

                      -Frinny

                      Comment

                      • Rhodge09
                        New Member
                        • Nov 2009
                        • 17

                        #12
                        no idea of course the two random pictures are coming up on the first screen but its just when i hit the submit is when two different pictures come up

                        Comment

                        Working...