New to ruby - Need help with simple guessing program

E

Evan Riley

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.
 
B

Brian Candler

Evan said:
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
 
J

Jesús Gabriel y Galán

Apart from other suggestions, here some are some notes:

Here is my current code:

random_number =3D rand(10)

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

players_number =3D 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 =3D gets.chomp.to_i
while players_number.to_i !=3D random_number.to_i

random_number is already an integer.
=A0while players_number.to_i < random_number.to_i

As has been said, this loop is not needed, just the if
=A0if players_number.to_i < random_number.to_i
=A0 =A0puts 'Sorry your to low'
=A0 =A0puts '_________________'
=A0 =A0random_number =3D rand(10)

=A0 =A0puts 'Can you guess the number?'
=A0 =A0puts 'Select a number from 1-10 please'

And this should be moved to a common place once.
=A0 =A0players_number =3D gets.chomp
=A0end
=A0end

=A0while players_number.to_i > random_number.to_i
=A0if players_number.to_i > random_number.to_i
=A0 =A0puts 'Sorry your to high'
=A0 =A0puts '__________________'
=A0 =A0random_number =3D rand(10)

=A0 =A0puts 'Can you guess the number?'
=A0 =A0puts 'Select a number from 1-10 please'

=A0 =A0players_number =3D gets.chomp
=A0end
=A0end

=A0while players_number.to_i =3D=3D random_number.to_i
=A0if players_number.to_i =3D=3D random_number.to_i
=A0 =A0puts 'Congratluations! You Win!'
=A0 =A0puts '_________________________'
=A0 =A0random_number =3D rand(10)

=A0 =A0puts 'Can you guess the number?'
=A0 =A0puts 'Select a number from 1-10 please'

=A0 =A0players_number =3D gets.chomp
=A0else

=A0end
=A0end
end

Can anyone help me with tracking the amount of entries he makes? Thank
you.

Regards,

Jesus.
 
B

brabuhr

2010/9/2 Jes=FAs Gabriel y Gal=E1n said:
Apart from other suggestions, here some are some notes:


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 =3D gets.chomp.to_i

Or, simply: players_number =3D gets.to_i
 
P

Paul Mckibbin

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top