Josh Cheek wrote in post #997327:
>> attempts = "0".to_i
>
> In the same way you have string literals (the "0"), you also have
> integer
> literals (just 0).
> 0 == "0".to_i # => true
>
> So just make it `attempts = 0`, same for flag.

Josh:  I originally had the variables like that way you described but I
changed it because I was getting an error when I tried to add 1
'attempts += 1'.  I just now changed it back and of course there was no 
error so I must have mistyped something.  Who knows now but thanks for 
pointing that out.

>
> On Sun, May 8, 2011 at 12:08 AM, Super Goat
> <ruby-forum / sgoat.33mail.com>wrote:
>
>>
>> I was thinking of asking if the player would like to play again and then
>> figuring out how to restart the whole thing but I wasn't sure how to do
>> this. How can I make prompt to "Play again?" and when answered "Yes"
>> restart, whether the person wins or loses (or if "No" just exits)?  Or
>> is the way my code is written make this impossible?
>>
>>
> Right now there is no mechanism to get back to the beginning where
> variables
> are set and such. What if you had a method that did everything involved
> with
> a game, then you could play a game by calling that method. That would
> allow
> you to just have a little piece of code that is responsible for handling
> whether you should play another game or not.
>
> def play_game
>   # ...
> end
>
> def play_again?
>   # prompt user, return true or false
> end
>
> play_game
> play_game while play_again?
> puts "goodbye!"

I like this idea and will give this a go on my next version.
Thanks again for your insight.

==============================

Stu wrote in post #997331:
> On Sun, May 8, 2011 at 12:08 AM, Super Goat
> <ruby-forum / sgoat.33mail.com> wrote:
>
>>
>> --
>> Posted via http://www.ruby-forum.com/.
>>
>>
>
> Every method closes with the end keyword.
>
> The last statement returns regardless in ruby.
>

Can you explain these two lines more thoroughly and when you say 'the
last statement returns', where does it return to.

Sorry for my ignorance, this is my first true attempt to learn a
language (I took 2 classes C++ 15 years ago in highschool)



Stu wrote in post #997333:
> My last piece of advice when you begin to figure out names for your
> variables and methods is to separate as much of the game logic as you
> can from the peripheral data. This will make it easier to change
> things in the long run if you need.
>
> puts "\nLets play a guessing game! You have 8 guesses before you lose."
> target = rand(101) #creates a random number from 0 - 100
> attempts = "0".to_i  #
> flag = "8".to_i
>
> can be rewritten like so
>
> max_user_attempts = 8
> @attempt_counter = 0
> directions = "\nLets play a guessing game! You have
> #{max_user_attempts.to_s} guesses before you lose."
> winning_response =  rand(101)
>
> def game_loop
>   print directions
>   #.. start loop and call many methods
> end

Stu, this make complete sense.  I probably would have read this or
sortof figured this out eventually but now I will really consider it as
I start to code each project.

> Last note. notice the @ in front of attempt_counter. This allows for
> methods to see the variable outside their own scope. it's optional if
> you want to use it but allows for direct manipulation:
>
> def increment_attempt
>   @attempt_counter += 1
> end
>

So can I just refer to increment_attempt each time i want the
@attempt_counter to increase by one?  for instance, replacing the way i
did it here.

until (choice = get_valid_input) == target
  puts "\nWrong, my number is #{choice < target ? 'higher' : 'lower'}"
  attempts += 1

with

until (choice = get_valid_input) == target
  puts "\nWrong, my number is #{choice < target ? 'higher' : 'lower'}"
  @attempt_counter

and if there were any other places where I would want to increase the
attempts just refer to @attempt_counter again?

> I hope I haven't confused you. I do not want to make it
> overcomplicated or obfuscate it so please ask many questions. Methods(
> and functions) are a very important concept to grok in every
> programming language in every paradigm.
>
> ~Stu

Not at all Stu.  I think you are really helping me establish some solid
Ruby coding processes and techniques. (I'm sure a lot of this applies to 
more than just Ruby) One thing I have learned, which may sound obvious 
but I never realized, is that there are so many ways to code something 
and get the same end result.  That coding can even be someones style. 
Thanks for taking your time to explain everything.


Goat

-- 
Posted via http://www.ruby-forum.com/.