Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I want to create a very simple game and while plotting it out in my head I have a question on the general setup.

When they load game.pl it'll say "welcome to the game." or whatever the first time and a few lines down will present a question for them to answer.

If the question is wrong, it says it's wrong and counts up to X (after which if they reach X wrong guesses it'll tell them the answer and will post a new question).

If they get the answer right, it'll have a nice message and then will move on to the next question.

I have the questions and answers setup and I know how to count bad guesses. I don't know how to setup the rotation where once the program starts, it won't quit until they say "quit" and will continually move on from one question to the next.

Does anyone have psuedo code that could help me with the general setup of this structure?

Replies are listed 'Best First'.
Re: setup of a small perl game
by tlm (Prior) on Apr 11, 2005 at 22:56 UTC

    Sure, in pseudocode (or wishfulthinking code) it's a piece cake:

    print_greeting(); reset_score(); OUTER: while ( 1 ) { my $current_question = ask_next_question(); my $attempts = 0; while ( $attempts++ < $MAX_ATTEMPTS ) { my $answer = get_answer(); last OUTER if $answer eq 'q'; last if check_answer( $current_question, $answer ); print_sorry_try_again(); } if ($attempts < $MAX_ATTEMPTS ) { print_congratulations(); update_score( 1 ); } else { explain_correct_answer( $current_question ); update_score( 0 ); } } print_score(); print_good_bye();
    The devil is in the details :-).

    the lowliest monk

      Thanks for your help! I tried using your method and got the following. It sorta works as it either says you got it right or wrong. However I don't know how to make it go back to the beginning if they got it right. It just sort of sits there until you make 4 more wrong guesses after you make it right.

      What do I need to do?

      while (1) { ########### # Heart of the game- Choose and scramble a word ########### my $word = $words[rand @words]; my @scramble = split(//, $word); @scramble = sort { (-1,1)[rand 2] } @scramble; print "\n\nGuess the scrambled word: "; foreach (@scramble) {print $_}; print "\n\n"; my $attempts = 0; while ($attempts++ < $max_attempts) { ##### # Take their guess ##### my $guess = <STDIN>; chomp($guess); if ($guess ne "$word") { print "$guess is incorrect.\n"; $attempts++; } else { print "Congratulations! The scrambled word was $word.\n\n"; } } }

        The quick fix would be to insert a last after the "Congratulations" line, like this:

        { print "Congratulations! The scrambled word was $word.\n\n"; last; }
        but IMHO I would advice you to follow the structure I gave you more closely; I think you will find that your script is clearer, and more easily modified and debugged if you actually implement functions like check_answer(), etc., that I used in the "wishfulthinking code". The more your code looks like pseudocode, the clearer it will be.

        the lowliest monk

Re: setup of a small perl game
by nedals (Deacon) on Apr 12, 2005 at 01:07 UTC

    If this is a web application, you could simply add a couple of hidden fields to your question form, one to track current question and another to track number of attempts. Then increment 'current question' for correct answers and 'attempts' for wrong ones.

    Of course, users could cheat by altering the hidden fields. So you could do the same thing with cookies to make it a little more difficult. Or, if you want to get a little fancy, you could use a sessionID.

Re: setup of a small perl game
by Anonymous Monk on Apr 12, 2005 at 14:21 UTC
    #!/usr/bin/perl use strict; use warnings; sub ask_question; my $question_count = 0; 1 while ask_question ++$question_count; exit; sub ask_question { my $question_count = shift; my $wrongs = 3; local $| = 1; for (my $prompt = "This is question $question_count: "; $wrongs --; $prompt = "Incorrect, try again: ") { print $prompt; chomp(my $answer = <>); return 0 if $answer =~ /^q(?:uit)?$/i; return 1 if $answer =~ /^a(?:nswer)?\s+$question_count$/i; } print "Still incorrect. The answer is 'answer $question_count'\n"; } __END__
Re: setup of a small perl game
by TedPride (Priest) on Apr 12, 2005 at 09:04 UTC
    If you also include an MD5 hash generated from the contents of your hidden fields, people won't be able to mess with them.