Create a PERL script for a number guessing game. Here are the requirements:

a. Your program generates a random number between 100 and 200 and keeps asking the user to guess the number until the user guesses it correctly.
b. If the user guesses a number that is lower or higher than the number to be guessed then indicate that information to the user as a ‘low guess’ or a ‘high guess’ so that the user can get to the correct guess quicker.
c. The program keeps track on number of guesses taken by the user to get to the correct guess. This is user’s score.
d. Use a parameter for number of allowed guesses (set it some value, say 7) and your program congratulates users if user guesses the number correctly in number of guesses less than the number of allowed guesses. The program doesn’t allow user to continue beyond the number of allowed guesses and tells user that the number wasn’t guessed correctly in allowed attempts. You can print the number of allowed guesses at the beginning of the game.
e. Write out user name and number of guesses taken to a game report file. Each attempt at the game will add a new line to the report file.

Respuesta :

Limosa

Answer:

Following are the program in the Perl Programming Language.

#set variable to 7

$gus = 7;

#set random number to the variable

$random = int(rand(100)) + 100;

#print message

print "Your name please: ";

#set variable to store name

my $name = <STDIN>;

chomp $name;

#print the random numbers with message ;

print "You Only Have Seven Guss to Find a Number\n";

print "Enter any number between 100 - 200\n";

#get number from the user

my $n = <STDIN>;

chomp $n;

#set the while loop

while( $gus > 0 )

{

#set if conditional statement

if( $n == $random)

{

print ("Congratulation, you Win the game in '$gus' gusses");

last;

}

elsif( $n < $random )

{

print ("Low Guss, Try Again \n");

}

else

{

print ("High Guss, Try Again \n");

}

#set variable to store value

my $n1 = <STDIN>;

chomp $n1;

#initialize the values

$n = $n1;

$gus = $gus-1;

}

#if guss is not found

if($gus == 0)

{

print "You have reached the maximum number of guss. Try Next Time\n";

}

#set variable to store file

open(my $f, '>', 'report.txt');

print $f " '$n' '$gus'\n";

#close file

close $f;

print "done\n";

Output:

Your name please: Shiva                                                                                                        

You Only Have Seven Guss to Find a Number                                                                                      

Enter any number between 100 - 200                                                                                              

150                                                                                                                            

High Guss, Try Again                                                                                                            

130                                                                                                                            

High Guss, Try Again                                                                                                            

110                                                                                                                            

High Guss, Try Again                                                                                                            

105                                                                                                                            

Congratulation, you Win the game in '4' gusses

done

Explanation:

Here, we set an integer type variable "gus" and assign value to 7 and then, we set variable "random" in which we store the random number from 100 to 200.

Then, we print message and get input string from the user in variable "name".

Then, we get number from user between 100 - 200 and set the while loop inside it we set the if-else statement to check the following input is equal to the ransom number or not.

Finally, we follow the following steps and set file.