in reply to setup of a small perl game

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

Replies are listed 'Best First'.
Re^2: setup of a small perl game
by Anonymous Monk on Apr 12, 2005 at 03:29 UTC
    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

        last; did fix it, thanks!

        You are probably right, functions might be easier to debug later. By functions do you mean sub routines? I could probably do that. I'm not too versed with self-made functions, however.