Hi Panda,

Very nice job! In addition to the things that GrandFather mentioned I'd like to encourage you to think about using hashes and references (see perlref and perldsc), because you are duplicating the core of the code for each option - you use essentially the same code for 'good' as you do for 'bad', the only thing that changes is the name of the array. By using the technique shown below you can add other moods without having to copy and paste the code block for each new mood.

use warnings; use strict; my %games = ( good => [ "nfs", "nfsII", "With the PerlMonks", "Morrowind", "TES Constructor Set" ], bad => [ "RedFactionII", "CCG", "RavenShield", "AGG" +], ); # another way to loop my $mood = ''; while( ! exists $games{$mood} ) { print "How has your day been?\n"; $mood = <STDIN>; chomp $mood; $mood = lc $mood; } my $game_aref = $games{$mood}; my $randgame = ${ $game_aref }[ rand @{ $game_aref } ]; print "You should play: $randgame\n";

To add a new mood, all you have to do is add it to the hash:

my %games = ( good => [ "nfs", "nfsII", "With the PerlMonks", "Morrowind", "TES Constructor Set" ], bad => [ "RedFactionII", "CCG", "RavenShield", "AGG" +], fantastic => [ "With the PerlMonks" ], );
Feel free to ask if you have any questions about the code.

Now for some homework:

  1. If you forget what the valid moods are, how could you have the program display a list of options?
  2. How could you add the "Error, please re-type answer" statement if the user entered an invalid option?
  3. How could you combine the solutions to problems #1 and #2 so the user gets both an error message and a list of valid moods when an invalid option is entered?
  4. Bonus question: How could you solve problem #3 without duplicating code? Hint: perlsub

I hope this helps, and that it gives you some things to think about and play with. Have fun! :-)


In reply to Re: Decisiveness for Gamers by bobf
in thread Decisiveness for Gamers by Panda

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.