#!/usr/bin/perl # This script picks a secret random number between 1 and 100 # and has the user try to guess what it is. # /acl 06/25/2009 # Draw the opening screen. print "\n\n\n\n\n"; print "\t.-----------------------.\n"; print "\t| Welcome to HiLow |\n"; print "\t.-----------------------.\n"; print "\t| Pick a number between |\n"; print "\t| 1 and 100! |\n"; print "\t.-----------------------."; print "\n\n\n\n\n"; # Set the range of the random number between 1 and 100. my $range = 100; # Pick a whole integer within the randon number range. my $random_number = int(rand($range)); # Set the initial guess number to 1, which we will increment with each guess. my $guess_number = "1"; # The guess routine is the real brains behind the game and is called by the last line # of this script to get started, and recalled within it's own conditions when applicable. sub guess { # Give the user a prompt for a guess, along with the current number of guesses. print "Guess [" . $guess_number . "]: "; # Set their current guess to a variable, chomping off the new line created at the end. chomp($my_guess = ); # Check to see if the guess is numeric by calling a function below. if (is_numeric($my_guess)) { # Makes sure that the guess is less than 100. if ($my_guess < 101) { # Compare the current guess to the random number. If their guess is too HIGH, # then tell them so, increment the variable for number of guesses, and restart # the guess sub. if ($my_guess > $random_number) { print "\tThe secret number is LOWER\n"; print "\tthan your guess of $my_guess.\n"; $guess_number++; &guess; } # Compare the current guess to the random number. If their guess is too LOW, # then tell them so, increment the variable for number of guesses, and restart # the guess sub. if ($my_guess < $random_number) { print "\tThe secret number is HIGHER\n"; print "\tthan your guess of $my_guess.\n"; $guess_number++; &guess; } # Compare the current guess to the random number. If their guess matches the, # random number, then draw a fancy screen telling them that they've won and show # the random number as well as the number of guesses that it took to find it. if ($my_guess = $random_number) { print "\n\n\n\n\n"; print "\t.-----------------------.\n"; print "\t| CONGRATUALTIONS! |\n"; print "\t.-----------------------.\n"; print "\t| YOU HAVE GUESSED |\n"; print "\t| THE |\n"; print "\t| SECRET NUMBER! |\n"; print "\t.-----------------------.\n\n"; print "\tSecret Number: $random_number\n"; print "\tNumber of Guesses: $guess_number\n"; print "\n\n\n\n\n"; } # This else is for the less than 100 check. If it fails that check, it calls the # error sub, below. } else { &error; } # This else is for the is numeric check. If it fails that check, it calls the # error, sub below. } else { &error; } } # This is the function called by the guess sub to see if the guess is numeric. sub is_numeric { defined $_[0] && $_[0] =~ /^[+-]?\d+$/; } # This catchall sub is referenced in the guess sub any time a check fails and # prints the rules of the game as a reminder. sub error { print "Whole numeric values between 1 and 100 only, please!\n"; &guess; } # This is to call the guess sub the first time. &guess;