in reply to Re: setup of a small perl game
in thread setup of a small perl game

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"; } } }

Replies are listed 'Best First'.
Re^3: setup of a small perl game
by tlm (Prior) on Apr 12, 2005 at 03:53 UTC

    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.

        Yes, subroutines. Subs are one of the most powerful ways to tame code complexity; without them you quickly end up with huge, monolithic scripts that are impossible to read, modify, or debug. Read perlsub, and soon you'll be sub genius. :-)

        the lowliest monk