Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

logic problem with perl game

by mynameisG (Novice)
on Dec 04, 2010 at 01:16 UTC ( #875306=perlquestion: print w/ replies, xml ) Need Help??
mynameisG has asked for the wisdom of the Perl Monks concerning the following question:

So some of you might have responded to another post I made, regarding a game involving corn. This game is similar, but instead of using math I have the user select the picture that ryhms with the random word. I am trying to make it so every time that they pick the correct picture they get +1 to # of wins, and +1 to # of losses if they pick the wrong picture. The only problem...regardless if the user selects the right picture or not the +1 always goes to # of losses. I think it's something in the logic? As you can see in my code I am storing the random word($theword) from the array(@words) in a new param($correctryhm) because the word changes every time the user presses the submit button(fight). Here's the code. Any help would be great, thanks

#!/usr/bin/perl use Template; print "Content-type: text/html\n\n"; use CGI qw(:standard); @words=("sparrow","wax","field","tear"); $theword=$words[rand @words]; $startwin=0; $startlose=0; $game=param('submit'); $win=param('win'); $lose=param('lose'); $response=param('response'); $arrow=param('arrow'); $axe=param('axe'); $shield=param('shield'); $spear=param('spear'); $correctryhm=param('correctryhm'); #another way of setting up a hash #needs to know where the templates are my $config={ INCLUDE_PATH =>'../../projectTemplate', #or list ref INTERPOLATE => 1, #expand '$var' in plain text POST_CHOMP => 1, #cleans up whitespace EVAL_PERL =>1, #evaluate Perl code blocks }; #<body background="../../projectTemplate/indianbackground"> $output=<<_html_; <html> <body> </body> </html> _html_ print $output; #create a template object #-> means 'send to' my $template=Template->new($config); if($game eq "Fight") { if($response eq "arrow" and $correctryhm eq "sparrow") { $arrow=param('arrow'); $correctryhm=param('correctryhm'); $response=param('response'); $win=param('win'); $win=$win+1; } if($response eq "axe" and $correctryhm eq "wax") { $axe=param('axe'); $correctryhm=param('correctryhm'); $response=param('response'); $win=param('win'); $win=$win+1; } if($response eq "shield" and $correctryhm eq "field") { $shield=param('shield'); $correctryhm=param('correctryhm'); $response=param('response'); $win=param('win'); $win=$win+1; } if($response eq "spear" and $correctryhm eq "tear") { $spear=param('spear'); $correctryhm=param('correctryhm'); $response=param('response'); $win=param('win'); $win=$win+1; } else { $lose=param('lose'); $lose=$lose+1; } if ($win <= 9 and $lose <= 10) { print "<h3>Ryhm the words to win the battle!</h3>"; print "<form method=\"post\" action=\"ryhm2.cgi\">"; print "<input name=\"submit\" type=\"submit\" value=\"Fight\" /> < +br>"; print "<input type=\"hidden\" name=\"win\" value=\"$win\">"; print "<input type=\"hidden\" name=\"lose\" value=\"$lose\">"; print "<input type=\"hidden\" name=\"arrow\" value=\"$arrow\">"; print "<input type=\"hidden\" name=\"axe\" value=\"$axe\">"; print "<input type=\"hidden\" name=\"shield\" value=\"$shield\">"; print "<input type=\"hidden\" name=\"spear\" value=\"$spear\">"; print "<input type=\"hidden\" name=\"response\" value=\"$response\ +">"; print "<input type=\"hidden\" name=\"correctryhm\" value=\"$thewor +d\">"; print "Wins: $win <br>"; print "Losses: $lose <br>"; #goes into var1 from projectTemplate and creates image map my $var1=<<_html_; <html> <body> <IMG src="../../projectTemplate/arrow.jpg\" align="left" /><input name +="response" type="radio" value="arrow" /> <input type="hidden" name="arrow" value="$arrow"> <input type="hidden" name="response" value="$response"> </body> </html> _html_ #goes into var2 from projectTemplate and creates image map my $var2=<<_html_; <html> <body> <IMG src="../../projectTemplate/axe.jpg\" /><input name="response" typ +e="radio" value="axe" /> <input type="hidden" name="axe" value="$axe"> <input type="hidden" name="response" value="$response"> </body> </html> _html_ #goes into var3 and writes the random word in the box my $var3="<h3>$theword</h3>"; print "<input type=\"hidden\" name=\"correctryhm\" value=\"$theword\"> +"; #goes into var4 from projectTemplate and creates image map my $var4="<IMG src=\"../../projectTemplate/shield.jpg\" align=\"left\" +> <input name=\"response\" type=\"radio\" value=\"shield\" />"; #goes in +to var5 from projectTemplate print "<input type=\"hidden\" name=\"shield\" value=\"$shield\">"; #my $var4="<IMG src=\"../../projectTemplate/shield.jpg\">"; #$var4=<<_html_; #<html> #<body> #<IMG src="../../projectTemplate/shield.jpg\" align="left" /><input na +me="response" type="radio" value="shield" /> #<input type="hidden" name="shield" value="$shield"> #</body> #</html> #_html_ #goes into var5 from projectTemplate my $var5="<IMG src=\"../../projectTemplate/spear.jpg\"> <input name=\"response\" type=\"radio\" value=\"spear\" />"; print "<input type=\"hidden\" name=\"spear\" value=\"$spear\">"; #my $var5=<<_html_; #<html> #<body> #<IMG src="../../projectTemplate/spear.jpg\" /><input name="response" +type="radio" value="spear" /> #<input type="hidden" name="spear" value="$spear"> #</body> #</html> #_html_ #set up another hash with var1, var2, var3, var4, var5 my $vars={ var1=>$var1, var2=>$var2, var3=>$var3, var4=>$var4, var5=>$var5, }; #gets from template file my $inputfile='ryhmgame.tpl'; $template->process($inputfile,$vars) || die print "not done"; print "</form>"; } if ($win == 10) { print "<h2>You won the battle!</h2>"; print "Congradualations you held off the enemy!"; } if ($lose > 10) { print "<h2>You failed to kill enough people</h2>"; print "Your family is dead <br>"; print "Game Over <br>"; $output=<<_html_; <a href="http://esprit.champlain.edu/~ggrillone32001/gamestart.htm">Re +-start game</a> _html_ print $output; } } else{ print "<h1>You are about to enter a battle!</h1>"; print "<form action=\"ryhm2.cgi\" method=\"post\">"; print "<input name=\"submit\" type=\"submit\" value=\"Fight\" />"; print "<input type=\"hidden\" name=\"win\" value=\"$startwin\">"; print "<input type=\"hidden\" name=\"lose\" value=\"$startlose\">" +; print"</form>"; }

Comment on logic problem with perl game
Download Code
Re: logic problem with perl game
by PeterPeiGuo (Hermit) on Dec 04, 2010 at 01:34 UTC

    1) As a starter, for those if conditions, instead of using "and", use &&. "and" has a very low precedence.

    2) Your else is only paired with the last if. Chain those if's together with if/else if/else.

    Peter (Guo) Pei

      So you mean like this?

      if(($response eq "arrow" && $correctryhm eq "sparrow")) { $correctryhm=param('correctryhm'); $response=param('response'); $win=param('win'); $win=$win+1; } elsif(($response ne "arrow" && $correctryhm eq "sparrow")) { $correctryhm=param('correctryhm'); $response=param('response'); $lose=param('lose'); $lose=$lose+1; } if(($response eq "axe" && $correctryhm eq "wax")) { $correctryhm=param('correctryhm'); $response=param('response'); $win=param('win'); $win=$win+1; } elsif(($response ne "axe" && $correctryhm eq "wax")) { $correctryhm=param('correctryhm'); $response=param('response'); $lose=param('lose'); $lose=$lose+1; } if(($response eq "shield" && $correctryhm eq "field")) { $correctryhm=param('correctryhm'); $response=param('response'); $win=param('win'); $win=$win+1; } elsif(($response ne "shield" && $correctryhm eq "field")) { $correctryhm=param('correctryhm'); $response=param('response'); $lose=param('lose'); $lose=$lose+1; } if(($response eq "spear" && $correctryhm eq "tear")) { $correctryhm=param('correctryhm'); $response=param('response'); $win=param('win'); $win=$win+1; } elsif(($response ne "spear" && $correctryhm eq "tear")) { $correctryhm=param('correctryhm'); $response=param('response'); $lose=param('lose'); $lose=$lose+1; }

      and then have that last else statement at the end

        Like the following, and you need to use && not "and" (unless you get the precedence straight by using ()):

        if(($response eq "arrow" && $theword eq "sparrow")) { ... } elsif(($response eq "axe" && $theword eq "wax")) { ... } elsif(($response eq "shield" && $theword eq "field")) { ... } elsif(($response eq "spear" && $theword eq "tear")) { ... } else { }

        Peter (Guo) Pei

Re: logic problem with perl game
by McDarren (Abbot) on Dec 04, 2010 at 01:52 UTC
    Somebody has to say it....
    use strict; use warnings;

      ^^did that, just needed to add 'my' to the beginning params etc...

        You need to be a lot more serious towards those two lines.

        Peter (Guo) Pei

Re: logic problem with perl game
by apl (Monsignor) on Dec 04, 2010 at 14:38 UTC
    You should consider changing
    { $shield=param('shield'); $correctryhm=param('correctryhm'); $response=param('response'); $win=param('win'); $win=$win+1; }

    to

    { $shield = param( 'shield' ); process(); } # .... sub process { $correctryhm = param('correctryhm'); $response = param('response'); $win = param('win'); $win=$win+1; }

    This will reduce the size of your code.

      Sorry guys I made a mistake, I just realized something very important. For some reason $correctryhm is storing the value of the previous $theword. for example, if the first $theword equals field and I click on the picture of the shield I get +1 # of losses, but if the next $theword equals arrow and I choose shield again I get +1 to # of wins... so that's what needs to be fixed

Re: logic problem with perl game
by liverpole (Monsignor) on Dec 05, 2010 at 02:50 UTC
    Hi mynameisG,

    A couple of suggestions about simplifying this part of your code:

    if($game eq "Fight") { if($response eq "arrow" && $correctryhm eq "sparrow") { $correctryhm=$correctryhm; $response=param('response'); $win=param('win'); $win=$win+1; } elsif($response ne "arrow" && $correctryhm eq "sparrow") { $correctryhm=$correctryhm; $response=param('response'); $lose=param('lose'); $lose=$lose+1; } if($response eq "axe" && $correctryhm eq "wax") { $correctryhm=$correctryhm; $response=param('response'); $win=param('win'); $win=$win+1; } elsif($response ne "axe" && $correctryhm eq "wax") { $correctryhm=$correctryhm; $response=param('response'); $lose=param('lose'); $lose=$lose+1; }code: ...

    1. The line $correctryhm=$correctryhm; doesn't do anything useful (it assigns variable $correctryhm to its current value). You can safely remove it.
    2. The line $win=param('win'); can safely be done outside of the if...elsif...else clause, to avoid the repetition.  Same thing with $response=param('response');
    3. A common shorthand for $X = $X + 1 is ++$X.  Same thing for $x = $X - 1 => --$X.

    Just the above changes will already significantly reduce the complexity of the one big conditional clause:

    $response = param('response'); $win = param('win'); $lose = param('lose'); if($game eq "Fight") { if($response eq "arrow" && $correctryhm eq "sparrow") { ++$win; } elsif($response ne "arrow" && $correctryhm eq "sparrow") { ++$lose; } if($response eq "axe" && $correctryhm eq "wax") { ++$win; } elsif($response ne "axe" && $correctryhm eq "wax") { ++$lose; } if($response eq "shield" && $correctryhm eq "field") { ++$win; } elsif($response ne "shield" && $correctryhm eq "field") { ++$lose; } if($response eq "spear" && $correctryhm eq "tear") { ++$win; } elsif($response ne "spear" && $correctryhm eq "tear") { ++$lose; } if ($win <= 9 && $lose <= 10) { # Removed for brevity ... } }

    And if you want to generalize further, you could create a hash (see perldata for more on hashes) containing the expected, correct rhyme for each word:

    $response = param('response'); $win = param('win'); $lose = param('lose'); my %correct_rhyme = ( 'arrow' => 'sparrow', 'axe' => 'wax', 'shield' => 'field', 'spear' => 'tear', ); if($game eq "Fight") { my $correct = $correct_rhyme{$response}; if ($response eq $correct) { ++$win; } else { ++$lose; } if ($win <= 9 && $lose <= 10) { # Removed for brevity ... } }

    Now, when you want to add a new word/rhyme pair, it's a straightforward matter of just adding the key/value pair to the %correct_rhyme hash.


    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/

      I tried altering code and adding the version with the hash, but that made it so no matter what the $response was I always got +1 to # of wins

        It seems to be storing the previous $response...For example if $theword equals wax and $response equals axe, I get +1 #number of losses, and if the next $theword equals wax again and $response equals arrow I get +1 to # of wins, because it is reading the previous $response, it's some kind of logic error, I've been playing around with it for a while but I still can't get it, i'm relatively new to perl, so if you notice any logic problems in the code can someone let me know thank you

        #!/usr/bin/perl use strict; use warnings; use Template; print "Content-type: text/html\n\n"; use CGI qw(:standard); my @words=("sparrow","wax","field","tear"); my $theword=$words[rand @words]; my $startwin=0; my $startlose=0; my $game=param('submit'); my $win=param('win'); my $lose=param('lose'); my $response=param('response'); my $picselect=param('picselect'); my $arrow=param('arrow'); my $axe=param('axe'); my $shield=param('shield'); my $spear=param('spear'); my $correctryhm=param('correctryhm'); #another way of setting up a hash #needs to know where the templates are my $config={ INCLUDE_PATH =>'../../projectTemplate', #or list ref INTERPOLATE => 1, #expand '$var' in plain text POST_CHOMP => 1, #cleans up whitespace EVAL_PERL =>1, #evaluate Perl code blocks }; #<body background="../../projectTemplate/indianbackground"> my $output=<<_html_; <html> <body> </body> </html> _html_ print $output; #create a template object #-> means 'send to' my $template=Template->new($config); $response = param('response'); $win = param('win'); $lose = param('lose'); if($game eq "Fight") { if($response eq "arrow" && $correctryhm eq "sparrow") { ++$win; } elsif($response ne "arrow" && $correctryhm eq "sparrow") { ++$lose; } if($response eq "axe" && $correctryhm eq "wax") { ++$win; } elsif($response ne "axe" && $correctryhm eq "wax") { ++$lose; } if($response eq "shield" && $correctryhm eq "field") { ++$win; } elsif($response ne "shield" && $correctryhm eq "field") { ++$lose; } if($response eq "spear" && $correctryhm eq "tear") { ++$win; } elsif($response ne "spear" && $correctryhm eq "tear") { ++$lose; } if ($win <= 9 && $lose <= 10) { print "<h3>Ryhm the words to win the battle!</h3>"; print "<form method=\"post\" action=\"ryhm.cgi\">"; print "<input type=\"hidden\" name=\"win\" value=\"$win\">"; print "<input type=\"hidden\" name=\"lose\" value=\"$lose\">"; print "<input type=\"hidden\" name=\"response\" value=\"$picselect +\">"; print "<input type=\"hidden\" name=\"correctryhm\" value=\"$thewor +d\">"; print "<input name=\"submit\" type=\"submit\" value=\"Fight\" /> < +br>"; print "Wins: $win <br>"; print "Losses: $lose <br>"; #goes into var1 from projectTemplate and creates image map my $var1=<<_html_; <html> <body> <IMG src="../../projectTemplate/arrow.jpg\" align="left" /><input name +="picselect" type="radio" value="arrow" /> <input type="hidden" name="response" value="$arrow"> <input type="hidden" name="response" value="$picselect"> </body> </html> _html_ #goes into var2 from projectTemplate and creates image map my $var2=<<_html_; <html> <body> <IMG src="../../projectTemplate/axe.jpg\" /><input name="picselect" ty +pe="radio" value="axe" /> <input type="hidden" name="response" value="$axe"> <input type="hidden" name="response" value="$picselect"> </body> </html> _html_ #goes into var3 and writes the random word in the box my $var3="<h3>$theword</h3> <input type=\"hidden\" name=\"correctryhm\ +" value=\"$theword\">"; #goes into var4 from projectTemplate and creates image map my $var4="<IMG src=\"../../projectTemplate/shield.jpg\" align=\"left\" +> <input name=\"picselect\" type=\"radio\" value=\"shield\" />"; #goes i +nto var5 from projectTemplate print "<input type=\"hidden\" name=\"response\" value=\"$shield\"> <input type=\"hidden\" name=\"response\" value=\"$picselect\">"; #goes into var5 from projectTemplate my $var5="<IMG src=\"../../projectTemplate/spear.jpg\"> <input name=\"picselect\" type=\"radio\" value=\"spear\" />"; print "<input type=\"hidden\" name=\"response\" value=\"$spear\"> <input type=\"hidden\" name=\"response\" value=\"$picselect\">"; #set up another hash with var1, var2, var3, var4, var5 my $vars={ var1=>$var1, var2=>$var2, var3=>$var3, var4=>$var4, var5=>$var5, }; #gets from template file my $inputfile='ryhmgame.tpl'; $template->process($inputfile,$vars) || die print "not done"; print "</form>"; } if ($win == 10) { print "<h2>You won the battle!</h2>"; print "Congradualations you held off the enemy!"; } if ($lose > 10) { print "<h2>You failed to kill enough people</h2>"; print "Your family is dead <br>"; print "Game Over <br>"; $output=<<_html_; <a href="http://esprit.champlain.edu/~ggrillone32001/gamestart.htm">Re +-start game</a> _html_ print $output; } } else{ print "<h1>You are about to enter a battle!</h1>"; print "<form action=\"ryhm.cgi\" method=\"post\">"; print "<input name=\"submit\" type=\"submit\" value=\"Fight\" />"; print "<input type=\"hidden\" name=\"win\" value=\"$startwin\">"; print "<input type=\"hidden\" name=\"lose\" value=\"$startlose\">" +; print"</form>"; }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://875306]
Approved by McDarren
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (13)
As of 2014-10-02 16:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (65 votes), past polls