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>"; }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: logic problem with perl game
by PeterPeiGuo (Hermit) on Dec 04, 2010 at 01:34 UTC | |
by mynameisG (Novice) on Dec 04, 2010 at 01:47 UTC | |
by PeterPeiGuo (Hermit) on Dec 04, 2010 at 01:59 UTC | |
by Anonymous Monk on Dec 04, 2010 at 03:36 UTC | |
Re: logic problem with perl game
by McDarren (Abbot) on Dec 04, 2010 at 01:52 UTC | |
by mynameisG (Novice) on Dec 04, 2010 at 01:56 UTC | |
by PeterPeiGuo (Hermit) on Dec 04, 2010 at 02:06 UTC | |
by mynameisG (Novice) on Dec 04, 2010 at 02:57 UTC | |
by PeterPeiGuo (Hermit) on Dec 04, 2010 at 03:43 UTC | |
Re: logic problem with perl game
by apl (Monsignor) on Dec 04, 2010 at 14:38 UTC | |
by mynameisG (Novice) on Dec 04, 2010 at 23:16 UTC | |
Re: logic problem with perl game
by liverpole (Monsignor) on Dec 05, 2010 at 02:50 UTC | |
by mynameisG (Novice) on Dec 05, 2010 at 23:26 UTC | |
by mynameisG (Novice) on Dec 05, 2010 at 23:38 UTC |