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

High Low Game (PERL Coding)

P: 17
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.edu/~rhodge/cw/hilow/hilow.pl

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. use CGI ':standard';
  3. use CGI::Carp "fatalsToBrowser";
  4.  
  5. print header;
  6.  
  7. #####variables#######
  8. $card1=param('card1');
  9. $card2=param('card2');
  10. $card3=param('card3');
  11. $pick=param('pick');
  12.  
  13. $wcount=param('wcount');
  14. $lcount=param('lcount');
  15. $results=param('param');
  16.  
  17.  
  18. ######main script######
  19.  
  20. print start_html('Hi Low Game');
  21.  
  22. &hash;
  23. &cards;
  24. if($pick eq ' ' ){
  25.     &form;
  26. }else{
  27.     &results;
  28. }
  29.  
  30.  
  31.  
  32. #####initializing a hash array w/a key value ######
  33. sub hash{
  34. %value=(
  35.     "1",1, "2",1, "3",1, "4",1,
  36.     "5",13, "6",13, "7",13, "8",13,
  37.     "9",12, "10",12, "11",12, "12",12,
  38.     "13",11, "14",11, "15",11, "16",11,
  39.     "17",10, "18",10, "19",10, "20",10,
  40.     "21",9, "22",9, "23",9, "24",9,
  41.     "25",8, "26",8, "27",8, "28",8,
  42.     "29",7, "30",7, "31",7, "32",7,
  43.     "33",6, "34",6, "35",6, "36",6,
  44.     "37",5, "38",5, "39",5, "40",5,
  45.     "41",4, "42",4, "43",4, "44",4,
  46.     "45",3, "46",3, "47",3, "48",3,
  47.     "49",2, "50",2, "51",2, "52",2,
  48. );
  49.  
  50. }
  51.  
  52.  
  53. ####The for loop -- initializer; test; increment/decrement####
  54.  
  55. #for($x=1; $x<53; $x++){
  56. #    print"    the Key is $x <br>
  57. #        <img src=images/$x.png> <br>
  58. #        the value is $value{$x}
  59. #        <hr>
  60. #         ";
  61. #}
  62.  
  63. ####first two cards chosen####
  64. sub cards{
  65.     $card1=int(rand(52)+1);
  66.     $card2=int(rand(52)+1);
  67.  
  68.     while($card2 == $card1){
  69.     $card2=int(rand(52)+1);}
  70.  
  71.     print" <img src=images/$card1.png> <img src=images/$card2.png";
  72.  
  73.     $c1v=$value{$card1};
  74.     $c2v=$value{$card2};
  75.  
  76. }
  77.  
  78.  
  79.  
  80. #######form to choose card#######
  81. sub form{
  82.     print"
  83.         <form method=post action=hilow.pl>
  84.         <input type=hidden value='$c1v'>
  85.         <input type=hidden value='$c2v'>
  86.         <input type=radio name=pick value=Higher>Higher<br>
  87.         <input type=radio name=pick value=Middle>Middle<br>
  88.         <input type=radio name=pick value=Lower>Lower<br>
  89.         <input type=hidden value='$wcount'>
  90.         <input type=hidden value='$lcount'>
  91.         <input type=submit value='Ba Bam'>
  92.         </form>
  93.     ";
  94.  
  95. }
  96.  
  97.  
  98.  
  99. #####win or loss computed#####
  100. sub results{
  101.     $card3=int(rand(52)+1);
  102.     while($card3 == $card1 || $card3 == $card2){
  103.     $card3=int(rand(52)+1);}
  104.  
  105.     $c3v=$value{$card3};
  106.  
  107.     if($pick eq "Higher" && $card3 > $card1 && $card3 > $card2){
  108.     $results='You Win!';
  109.     }elsif($pick eq "Middle" && $card3 > $card1 && $card3 < $card2 ||
  110.              $pick eq "Middle" && $card3 < $card1 && $card3 > $card2){
  111.                   $results='You Win!';
  112.     }else{$results='You Lose!';}
  113.  
  114.     print"<img src=images/$card3.png> <br /> $results";
  115.  
  116. }
  117.  
Dec 4 '09 #1
Share this Question
Share on Google+
11 Replies


Frinavale
Expert Mod 5K+
P: 9,731
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:
Expand|Select|Wrap|Line Numbers
  1. else{$results='You Lose! You picked:".$pick." card1 was: ".$card1.", card2 was: ".$card2.", card3 was: ".$card3;}
Dec 4 '09 #2

P: 17
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.
Dec 4 '09 #3

Frinavale
Expert Mod 5K+
P: 9,731
Sounds cool but first we should try to debug why your logic isn't correct :)

-Frinny
Dec 4 '09 #4

P: 17
I just tried your method and all that happened was it showed two random images on the next page
Dec 4 '09 #5

Frinavale
Expert Mod 5K+
P: 9,731
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....when 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
Dec 4 '09 #6

P: 17
I really dont know i've tried everything that you told me and none of it works.
Dec 4 '09 #8

Frinavale
Expert Mod 5K+
P: 9,731
I went to the site and I do not see the output that I recommended for debugging.
Dec 4 '09 #9

P: 17
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
Dec 4 '09 #10

Frinavale
Expert Mod 5K+
P: 9,731
You have the following:
Expand|Select|Wrap|Line Numbers
  1.  $card1=param('card1');
  2.  $card2=param('card2');
  3.  $card3=param('card3');
  4.  
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
Dec 4 '09 #11

P: 17
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
Dec 5 '09 #12

Post your reply

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