#Guessing Game
#You only get 10 tries!
#Gives the option of starting over.
#This version should count your turns for you, down from #ten.
TOP:
my $tries = 10;
#This is how many guesses you get.
my $value = int(rand(200));
#This is the number they have to guess: a random integer.
print "Pick a number between 0 and 200.\n";
print "You have ";
print $tries;
print " guesses left.\n";
$tries--;
my $guess = <STDIN>;
until ($tries==0){
if($guess>$value){
print "You need to guess a lower number.\n";
print "Guesses left: ";
print $tries;
print "\n";
$tries--;
$guess = <STDIN>;
}elsif($guess<$value){
print "You need to guess a higher number.\n";
print "Guesses left: ";
print $tries;
print "\n";
$tries--;
$guess = <STDIN>;
}else{
print "You guessed my number in ";
print (10-$tries);
print " guesses. Thanks for playing. \n";
$tries=0}
}
if ($guess != $value){
print "Sorry, you ran out of guesses. My number was ";
print $value;
print ". \n";
}
print "Play Again? (y/n) \n";
my $again = <STDIN>;
chomp ($again);
if($again eq "y"){
goto TOP;
}else{
print "Goodbye!";
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|