in reply to Modifying loop structure and placement

I am just guessing again.
But I figure that this some guessing game based upon some random number between 0-100.

Here is one version.
The rand() function is not really that random but it is good enough for this game.

This shows how to start the game, validate input to the game and also how to restart a new session of the game.
Certainly some refinements are possible, but this a general framework.

#!/usr/bin/perl -w use strict; my $secret_number; my $total_guesses; sub init_game { $secret_number = int(rand 101); $total_guesses =0; } init_game(); #initial start of game parameters print "General Rules:\n"; print " I have a secret number from 0-100\n"; print " Your job is to guess this number!\n"; print " You will be given hints for each guess\n"; print " Enter quit if you give up!\n"; while ( (print "Enter a guess: "), (my $number = <STDIN>) !~ /^\s*q(uit)?\s*$/i ) { next if $number !~ /\S/; #skip blank lines $number =~ s/^\s*//; #no leading spaces $number =~ s/\s*$//; #no trailing spaces (also chomps) if ($number =~ /[-.]+/) { print "only unsigned integers are allowed!\n"; next; } if ($number =~ /\D/) { print "Invalid!! Only integer numbers >=0 are allowed!\n"; next; } if ($number >100) { print "Hey, you didn't pay attention, max number is 100!\n"; next; } if (game_finished($number)) { print "would you like to play again?:(Y|y|yes)\n"; if ( (my $answer =<STDIN>) =~ /^\s*Y(es)?\s*$/i) { init_game(); print "New Game Starting!!!\n\n"; } else { exit(0); } } } sub game_finished { my $number = shift; $total_guesses++; if ($number == $secret_number) { print "Horray! you got the secret number, $secret_number!\n"; print "But it took you $total_guesses total guesses to do it!!\ +n"; return(1); } if ($number < $secret_number) { print "You guessed too low! Dummy!\n"; return(0); } if ($number > $secret_number) { print "You guessed too high! Duh!\n"; return(0); } } Example Session: General Rules: I have a secret number from 0-100 Your job is to guess this number! You will be given hints for each guess Enter quit if you give up! Enter a guess: 50 You guessed too high! Duh! Enter a guess: 25 You guessed too high! Duh! Enter a guess: 12 You guessed too high! Duh! Enter a guess: 6 You guessed too low! Dummy! Enter a guess: 8 You guessed too high! Duh! Enter a guess: 7 Horray! you got the secret number, 7! But it took you 6 total guesses to do it!! would you like to play again?:(Y|y|yes) y New Game Starting!!! Enter a guess: q
Update: I guess I could have combined two statements:
if ($number =~ /[-.]+/) if ($number =~ /\D/) into just one regex: if( $number =~/[-.\D]/) { print "Invalid!! Only unsigned integer numbers >=0 are allowed! +\n"; next; } probably could add if( $number =~/[-.+\D]/) also to disallow the + sig +n. But I don't this minor issue detracts from the main point.

Replies are listed 'Best First'.
Re^2: Modifying loop structure and placement
by irvson (Sexton) on Dec 30, 2009 at 23:06 UTC
    Thanks for taking the time to create this extensive response. A lot for me to digest.
Re^2: Modifying loop structure and placement
by Marshall (Canon) on Jan 19, 2010 at 20:45 UTC
    A bit shorter version of the "guessing game"
    #!/usr/bin/perl -w use strict; my $magic = int (rand 100 ); my $guesses = 1; my $line; print "Guess a number between 0-99\n"; while ( (print "Enter an integer: "), $line = <STDIN>, $line !~ /^\s*q(uit)?\s*$/i ) { next if $line =~ /^\s*$/; #re-pompt on blank line if ( $line !~ /^\s*(\d{1,2})\s*$/ ) { print "Illegal entry! Try again!\n\n"; next; } print "Too high!\n\n" if $1 > $magic; print "Too low!\n\n" if $1 < $magic; last if $1 == $magic; $guesses++; } print "You got it right in $guesses guesses! \n" if ($line !~ /^\s*q(uit)?\s*$/i);