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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.