Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I remember doing this game when I was first learning in BASIC. And then the corrolary - you pick the number, the computer guesses it. I learned binary searches that day, without realising it. ;-)

Comments:

  • use strict; use warnings; Always. Unless you know what you're doing.
  • Formatting. There are two major styles of braces. I'd suggest picking one. Although there can be religious wars on the topic of which one, and I have my preference as well, I'm not going to offer more advice than "just pick one".
  • Indentation. Whitespace is your friend. Make blocks of code easier to follow by lining up the left indent approprately.
  • Next is the use of goto. Although it has some uses, most of them are for advanced users only. As in, only once you've mastered not using goto can you give a reasonable justification for using it. At least, that's my opinion on using goto. In this case, you're using it to somewhat emulate a while loop. So just use a while loop, and people (like me) won't complain. To this end, I'd put the game itself into a "game" subroutine, and then be able to just wrap the call to the game sub in a small while loop.
  • Generally speaking, a single line of output is represented by a single print statement. Exceptions to this, unlike the exceptions to goto, are commonplace, however, so don't take this as a hard-and-fast rule or anything. For example, instead of
    print "Sorry, you ran out of guesses. My number was "; print $value; print ". \n";
    use
    print "Sorry, you ran out of guesses. My number was ", $value, ". +\n";
    Some people will use perl's interpolation to do this as:
    print "Sorry, you ran out of guesses. My number was $value.\n";
    The former may be faster, but in reality the deciding factor should be what you find to be more readable.
  • Comments ... should go before what they're commenting, not after. Code is not a suspense novel - you shouldn't need to wait to see what really is happening. Code should be extremely boring where the plot is revealed before anything happens.
With all that in mind, here's your code with all of these changes. If you have any questions, don't hesitate to ask.

#Guessing Game #You only get 10 tries! #Gives the option of starting over. #This version should count your turns for you, down from ten. use strict; use warnings; #This is how many guesses you get. my $max_guesses = 10; sub game { #This is the number they have to guess: a random integer. my $value = int(rand(200)); print "Pick a number between 0 and 200.\n"; for (my $tries = $max_guesses; $tries; --$tries) { print "You have ", $tries, " guesses left.\n"; my $guess = <STDIN>; if($guess>$value) { print "You need to guess a lower number.\n"; print "Guesses left: ", $tries, "\n"; } elsif($guess<$value) { print "You need to guess a higher number.\n"; print "Guesses left: ", $tries, "\n"; } else { print "You guessed my number in ", $max_guesses - $tries, " guesses. Thanks for playing. \n"; return; } } print "Sorry, you ran out of guesses. My number was ", $value, ". +\n"; } my $again = 'y'; while ($again eq 'y') { game(); print "Play Again? (y/n) \n"; $again = <STDIN>; chomp ($again); } print "Goodbye!\n";

In reply to Re: Number Guess by Tanktalus
in thread Number Guess by cryptoquip

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-04-20 01:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found