When all questions of space, time, matter and the nature of being have been resolved, only one question remains-"What game shall we play?"

The Perlmatic Automated Game Picker provides the ultimate gaming experience and for once there are no options to worry about.

This newly improved edition will pick a game for you to play based on your mood. So until PAGP 3.0 comes out, Have FUN!

use warnings; use strict; { my @HappyGamesAndThings = ("nfs", "nfsII", "With the PerlMonks", "Morr +owind", "TES Constructor Set"); my @SadGamesAndThings = qw(RedFactionII CCG RavenShield AGG); print "How has your day been? (Answer 'good or 'bad').\n"; my $mood = <STDIN>; chomp $mood; if ("bad" eq lc ($mood)) { my $randgame = $SadGamesAndThings [rand (@SadGamesAndThings)]; print "You should play:"; print $randgame; } if ("good" eq lc ($mood)) { my $randgame = $HappyGamesAndThings [rand (@HappyGamesAndThings)]; print "You should play: "; print $randgame; } if (lc ($mood) ne "bad" and lc ($mood) ne "good") { print "Error, please re-type answer. \n"; redo } } exit;
Please feel free to substitute your own games, use this wisely, and you will never have to make a decision again, well, almost.

Replies are listed 'Best First'.
Re: Decisiveness for Gamers
by bobf (Monsignor) on Jul 14, 2006 at 15:28 UTC

    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! :-)

      Thanks bobf!

      I thought that you had to have a key for every value in a hash - I would have never thought of making the key a reference to an anonymous array. Those questions got me thinking as well, if I ever do make version 3.0 I'll put the answers in that.

      The thing I don't understand is how you get it to return the value of the array:

      my $game_aref = $games{$mood}; my $randgame = ${ $game_aref }[ rand @{ $game_aref } ]; print "You should play: $randgame\n";

      Way over my head, any explanation would be welcome :)

      Combining our various suggestions and in the interests of TIMTOWTDI I offer the following:

      use warnings; use strict; my %moods = ( good => ["nfs", "nfsII", "With the PerlMonks", "Morrowind", "TES C +onstructor Set"], bad => ["RedFactionII", "CCG", "RavenShield", "AGG"], ); my $mood; while (1) { print "How has your day been?\n"; $mood = <STDIN>; chomp $mood; $mood = lc $mood; last if exists $moods{$mood}; } my $gamesRef = $moods{$mood}; my $randgame = $gamesRef->[rand @$gamesRef]; print "You should play: $randgame\n";

      which gives the hash an improved name and better facilitates a solution to homework problem 2.

      Panda should note the use of the if as a statement modifier in last if ... and the alternate syntax used for dereferencing the array ref ($gamesRef->). Note too that the {} brackets are not required for @$gamesRef.


      DWIM is Perl's answer to Gödel
Re: Decisiveness for Gamers
by GrandFather (Saint) on Jul 14, 2006 at 04:35 UTC

    When you want a "loop for ever" is is more conventional to:

    while (1) { ... last if $finished; ... }

    Long identifiers are easier to read if you break them up a little:

    my @happyGamesAndThings; # Camel case my @happy_games_and_things; # underscores

    Camel case takes a little time for your eye to grow accustomed to, but has the advantage os being somewhat more compact than the underscore using variant.

    The last if is redundant. You can only arrive there if the previous two tests have failed. If you use a while loop that whole block of code can be replaced by just the print statement.

    Those comments aside, a cool use for Perl by a learning monk.


    DWIM is Perl's answer to Gödel
Re: Decisiveness for Gamers
by Tanktalus (Canon) on Jul 17, 2006 at 15:58 UTC

    In addition to all the good advice above, I'd like to suggest that you find some new hobbies. ;-) Maybe even some that don't involve the 'puter ;-)

      Tanktalus,

      This may surprise you, but I play the tuba (carrying it around is -in itself- exercise), read lots of books and play badminton.

      I also have two baby brothers (see my home node for picture) whose are very good exercise.

      I also, don't watch tv. ;)

A reply falls below the community's threshold of quality. You may see it by logging in.