funny game

Discussion in 'Perl Misc' started by robin, Aug 15, 2005.

  1. robin

    robin Guest

    this is some basic stuff, but I think some of you might have fun with
    it,
    --


    #!/perl
    use strict;
    my @compresponses=('Yes.', ' No.', 'Maybe.', 'Nothing doing.', 'You
    might be
    crazy to ask that.', 'You are so cool.', 'The magic 8 box likes you',
    'Cool!', 'Hmmm.',
    '2$#@(&*#&*(*&#$$&(*', 'No way in heck.',, 'Good luck.', 'I like what
    you\'re doing with yourself', 'This game sucks.', 'Better luck next
    time.',
    'Try again.', 'Maybe so.', 'Forever.', 'Difficult to tell.', 'You
    really
    crack me up.',
    'Good, but no cigar.', 'Have a cold beer.', 'Having fun?', 'Good
    thinking.',
    'Whatever.','Are you implying I am fat?','Go and put the kettle on.','I
    want
    you to think about what you just asked.','Fourty Two.','It will all end
    in
    tears.',
    'A long time ago in a galaxy far far away...','I\'m late for a very
    important date.','That\'s hard to say - I\'ll have to get back to
    you.','Such things are not for me to decide.','I have a
    dream!','Vanity,
    Vanity, All is vanity!',
    'Are you always this tenecious?','I could tell you, but then I\'d have
    to
    kill you.','What do I look like - a fortune teller!?');

    my $in;
    my $answer;
    while ()
    {
    srand;
    print "Ask the magic 8 ball a question: (\"q\" to quit)\n";
    $in = <STDIN>;
    chomp ($in);
    print "\n\n--Mail me at webmaster\@infusedlight.net!--\n\n" and
    exit
    if $in eq "q";
    next if ! $in or $in =~ /^\s*$/;
    $answer = $compresponses[int rand @compresponses];
    print "ANSWER: $answer\n\n";
    sleep 2;
    }


    --
    Robin
    --

    --
    robin, Aug 15, 2005
    #1
    1. Advertising

  2. robin

    Paul Lalli Guest

    robin wrote:
    > this is some basic stuff, but I think some of you might have fun with
    > it,
    > --
    >
    >
    > #!/perl
    > use strict;


    After $deity knows how many years, you *still* refuse to use warnings?!

    > my @compresponses=('Yes.', ' No.', 'Maybe.', 'Nothing doing.', 'You
    > might be
    > crazy to ask that.', 'You are so cool.', 'The magic 8 box likes you',
    > 'Cool!', 'Hmmm.',
    > '2$#@(&*#&*(*&#$$&(*', 'No way in heck.',, 'Good luck.', 'I like what
    > you\'re doing with yourself', 'This game sucks.', 'Better luck next
    > time.',
    > 'Try again.', 'Maybe so.', 'Forever.', 'Difficult to tell.', 'You
    > really
    > crack me up.',
    > 'Good, but no cigar.', 'Have a cold beer.', 'Having fun?', 'Good
    > thinking.',
    > 'Whatever.','Are you implying I am fat?','Go and put the kettle on.','I
    > want
    > you to think about what you just asked.','Fourty Two.','It will all end
    > in
    > tears.',
    > 'A long time ago in a galaxy far far away...','I\'m late for a very
    > important date.','That\'s hard to say - I\'ll have to get back to
    > you.','Such things are not for me to decide.','I have a
    > dream!','Vanity,
    > Vanity, All is vanity!',
    > 'Are you always this tenecious?','I could tell you, but then I\'d have
    > to
    > kill you.','What do I look like - a fortune teller!?');
    >


    Gaahhhhh! What do you have against legible code? And *why* are you
    mixing logic with data?!

    > my $in;
    > my $answer;


    Neither of these are used outside the following block. They should be
    declared in the smallest scope possible.

    > while ()


    Personal preference - that looks yucky. Like you forgot something.
    while (1) at least makes it explicit you're writing an infinite loop.

    > {
    > srand;


    Wholly unnecessary. Please read:
    perldoc -f rand

    > print "Ask the magic 8 ball a question: (\"q\" to quit)\n";
    > $in = <STDIN>;
    > chomp ($in);


    chomp (my $in = <STDIN>);

    > print "\n\n--Mail me at webmaster\@infusedlight.net!--\n\n" and
    > exit
    > if $in eq "q";


    but not if $in is 'Q'?

    > next if ! $in or $in =~ /^\s*$/;


    So that the question can't consist of a single 0? Because an empty
    string is already taken care of by the regexp.

    > $answer = $compresponses[int rand @compresponses];
    > print "ANSWER: $answer\n\n";
    > sleep 2;
    > }


    IMHO, this looks rather cleaner:
    #!/usr/bin/perl
    use strict;
    use warnings;
    chomp (my @compresponses=<DATA>);

    my $in;
    do {
    print qq/Ask the magic 8 ball a question: ("q" to quit)\n/;
    chomp ($in = <STDIN>);
    unless ($in =~ /^\s*$/ or lc $in eq 'q'){
    my $answer = $compresponses[int rand @compresponses];
    print "ANSWER: $answer\n\n";
    sleep 2;
    }
    } until lc $in eq 'q';

    print "\n\n--Mail me at webmaster\@infusedlight.net!--\n\n";

    __DATA__
    Yes.
    No.
    Maybe.
    Nothing doing.
    You might be crazy to ask that.
    You are so cool.
    The magic 8 box likes you
    Cool!
    Hmmm.
    2$#@(&*#&*(*&#$$&(*
    No way in heck.
    Good luck.
    I like what you're doing with yourself
    This game sucks.
    Better luck next time.
    Try again.
    <etc...>
    Paul Lalli, Aug 15, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. BlackHawke
    Replies:
    12
    Views:
    1,493
    Andrew Thompson
    Jan 26, 2004
  2. judith
    Replies:
    0
    Views:
    1,602
    judith
    Nov 1, 2006
  3. Max Kubierschky
    Replies:
    10
    Views:
    1,813
    pabloreda
    Mar 31, 2007
  4. Advertiser for `2D Games Development Central`

    {Game Development} 2D Game Development Central

    Advertiser for `2D Games Development Central`, May 7, 2008, in forum: Java
    Replies:
    2
    Views:
    395
    RedGrittyBrick
    May 8, 2008
  5. Advertiser for `2D Games Development Central`

    {Game Development} 2D Game Development Central

    Advertiser for `2D Games Development Central`, May 7, 2008, in forum: C++
    Replies:
    0
    Views:
    376
    Advertiser for `2D Games Development Central`
    May 7, 2008
Loading...

Share This Page