Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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$..$/

In reply to Re: logic problem with perl game by liverpole
in thread logic problem with perl game by mynameisG

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (None)
    As of 2024-04-25 01:55 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found