Forum: Ruby New to ruby - Need help with simple guessing program

Posted by Evan Riley (chichu)
on 2010-09-02 15:28
Ok,so as a little practice I've decided to come up with a Guessing Game
where the user is able to guess a number, and if it the same as the
randomly generated number by the computer, he gets a message that says a
little hooray message. That part is done, and completed, but now I need
help
with tracking the amount of entries he makes in gets

Here is my current code:

random_number = rand(10)

puts 'Can you guess the number?'
puts 'Select a number from 1-10 please'

players_number = gets.chomp

while players_number.to_i != random_number.to_i

  while players_number.to_i < random_number.to_i
  if players_number.to_i < random_number.to_i
    puts 'Sorry your to low'
    puts '_________________'
    random_number = rand(10)

    puts 'Can you guess the number?'
    puts 'Select a number from 1-10 please'

    players_number = gets.chomp
  end
  end

  while players_number.to_i > random_number.to_i
  if players_number.to_i > random_number.to_i
    puts 'Sorry your to high'
    puts '__________________'
    random_number = rand(10)

    puts 'Can you guess the number?'
    puts 'Select a number from 1-10 please'

    players_number = gets.chomp
  end
  end

  while players_number.to_i == random_number.to_i
  if players_number.to_i == random_number.to_i
    puts 'Congratluations! You Win!'
    puts '_________________________'
    random_number = rand(10)

    puts 'Can you guess the number?'
    puts 'Select a number from 1-10 please'

    players_number = gets.chomp
  else

  end
  end
end

Can anyone help me with tracking the amount of entries he makes? Thank
you.
Posted by Brian Candler (candlerb)
on 2010-09-02 15:57
Evan Riley wrote:
> Can anyone help me with tracking the amount of entries he makes? Thank
> you.

Put these lines in the appropriate places:

tries = 0

...

tries = tries + 1

...

puts "You took #{tries} attempt(s)"

BTW your code could do with some trimming, but if it works that's the 
first thing :-) Some suggestions:

* no need for inner while loops, the outer while loop handles it for 
you.

if condition1
  ...
elsif condition2
  ...
else
  ...
end

* Move the bits which are common (prompting for the next attempt) to 
outside of this section, since they're the same in all cases
Posted by Jesús Gabriel y Galán (Guest)
on 2010-09-02 16:19
(Received via mailing list)
Apart from other suggestions, here some are some notes:

On Thu, Sep 2, 2010 at 3:28 PM, Evan Riley <rileyc.evan@gmail.com> 
wrote:

> Here is my current code:
>
> random_number = rand(10)
>
> puts 'Can you guess the number?'
> puts 'Select a number from 1-10 please'
>
> players_number = gets.chomp

As you are always using players_number as an integer to, I suggest
doing the to_i after the gets, so you don't have to repeat it over and
over:

players_number = gets.chomp.to_i

>
> while players_number.to_i != random_number.to_i

random_number is already an integer.

>
>  while players_number.to_i < random_number.to_i

As has been said, this loop is not needed, just the if

>  if players_number.to_i < random_number.to_i
>    puts 'Sorry your to low'
>    puts '_________________'
>    random_number = rand(10)
>
>    puts 'Can you guess the number?'
>    puts 'Select a number from 1-10 please'
>

And this should be moved to a common place once.

>    puts 'Can you guess the number?'
>    random_number = rand(10)
>
> Can anyone help me with tracking the amount of entries he makes? Thank
> you.

Regards,

Jesus.
Posted by Savard, Joseph (Guest)
on 2010-09-02 17:19
(Received via mailing list)
Evan,

As you have players_numbers you need to setup players_guesses

Increasing the players_guesses every time  a player enters a number.

players_guesses += 1  // this increments players_guess by 1

Once you have this try creating a class and building out methods...  Try 
to DRY this example up. ;-)


________________________________
From: Evan Riley <rileyc.evan@gmail.com>
Reply-To: <ruby-talk@ruby-lang.org>
Date: Thu, 2 Sep 2010 08:28:42 -0500
To: ruby-talk ML <ruby-talk@ruby-lang.org>
Subject: New to ruby - Need help with simple guessing program

Ok,so as a little practice I've decided to come up with a Guessing Game
where the user is able to guess a number, and if it the same as the
randomly generated number by the computer, he gets a message that says a
little hooray message. That part is done, and completed, but now I need
help
with tracking the amount of entries he makes in gets

Here is my current code:

random_number = rand(10)

puts 'Can you guess the number?'
puts 'Select a number from 1-10 please'

players_number = gets.chomp

while players_number.to_i != random_number.to_i

  while players_number.to_i < random_number.to_i
  if players_number.to_i < random_number.to_i
    puts 'Sorry your to low'
    puts '_________________'
    random_number = rand(10)

    puts 'Can you guess the number?'
    puts 'Select a number from 1-10 please'

    players_number = gets.chomp
  end
  end

  while players_number.to_i > random_number.to_i
  if players_number.to_i > random_number.to_i
    puts 'Sorry your to high'
    puts '__________________'
    random_number = rand(10)

    puts 'Can you guess the number?'
    puts 'Select a number from 1-10 please'

    players_number = gets.chomp
  end
  end

  while players_number.to_i == random_number.to_i
  if players_number.to_i == random_number.to_i
    puts 'Congratluations! You Win!'
    puts '_________________________'
    random_number = rand(10)

    puts 'Can you guess the number?'
    puts 'Select a number from 1-10 please'

    players_number = gets.chomp
  else

  end
  end
end

Can anyone help me with tracking the amount of entries he makes? Thank
you.
Posted by unknown (Guest)
on 2010-09-02 17:25
(Received via mailing list)
2010/9/2 Jesús Gabriel y Galán <jgabrielygalan@gmail.com>:
>
> As you are always using players_number as an integer to, I suggest
> doing the to_i after the gets, so you don't have to repeat it over and
> over:
>
> players_number = gets.chomp.to_i

Or, simply: players_number = gets.to_i
Posted by Paul Mckibbin (blackrat)
on 2010-09-02 19:05
Hi Evan,

As others have said, you could have a single control loop, and run 
everything in that. Something like

won=false
while !won
  <logic for the game>
  if <win condition>
    won=true
  end
end

The random number in your code will change every time rand(10) is 
called, so for the user they won't know if their next guess should be 
higher or lower. Also having a single place for input will make it 
easier to do other things for that input. (e.g. count=0 outside the loop 
and count+=1 inside, rather than multiple count+=1 for every line 
requiring user input.)

For comparison, you can use the <=> operator which will return -1 if 
lower, 1 if higher and 0 if they are the same. Using this combination of 
things, it is possible to code the whole game in about 20 clear lines of 
ruby and then to add 3 lines to make it keep a count and display that 
count at the end. (16 was what my code example was, others might come up 
with even fewer.)

Since this is practice for you, I've held off on putting any real code 
here, but as a hint, take a look at using case...when...else if you try 
with <=>. One good place to go to for practice code is 
http://rubylearning.com/blog/. They set a coding challenge every month, 
aimed at new ruby developers.

Good luck and enjoy,
Paul

Evan Riley wrote:
> Ok,so as a little practice I've decided to come up with a Guessing Game
> where the user is able to guess a number, and if it the same as the
> randomly generated number by the computer, he gets a message that says a
> little hooray message. That part is done, and completed, but now I need
> help
> with tracking the amount of entries he makes in gets
> 
> Here is my current code:
> 
> random_number = rand(10)
> 
> puts 'Can you guess the number?'
> puts 'Select a number from 1-10 please'
> 
> players_number = gets.chomp
> 
> while players_number.to_i != random_number.to_i
> 
>   while players_number.to_i < random_number.to_i
>   if players_number.to_i < random_number.to_i
>     puts 'Sorry your to low'
>     puts '_________________'
>     random_number = rand(10)
> 
>     puts 'Can you guess the number?'
>     puts 'Select a number from 1-10 please'
> 
>     players_number = gets.chomp
>   end
>   end
> 
>   while players_number.to_i > random_number.to_i
>   if players_number.to_i > random_number.to_i
>     puts 'Sorry your to high'
>     puts '__________________'
>     random_number = rand(10)
> 
>     puts 'Can you guess the number?'
>     puts 'Select a number from 1-10 please'
> 
>     players_number = gets.chomp
>   end
>   end
> 
>   while players_number.to_i == random_number.to_i
>   if players_number.to_i == random_number.to_i
>     puts 'Congratluations! You Win!'
>     puts '_________________________'
>     random_number = rand(10)
> 
>     puts 'Can you guess the number?'
>     puts 'Select a number from 1-10 please'
> 
>     players_number = gets.chomp
>   else
> 
>   end
>   end
> end
> 
> Can anyone help me with tracking the amount of entries he makes? Thank
> you.
Posted by Evan Riley (chichu)
on 2010-09-07 15:19
Thank you all for the tips, I'm loving ruby more every minute.
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.