Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Lets play a guessing game. (how to code this better?)

Reply
Thread Tools

Lets play a guessing game. (how to code this better?)

 
 
Super Goat
Guest
Posts: n/a
 
      05-05-2011
I am a new Rubyist. I told my friend that I was learning Ruby. He asked
me how that was going and then gave me a little challenge. His
challenge, "Write a text game that guesses numbers 0-100". My reply,
"you mean it picks a number at random, and you guess the number, it
tells you higher or lower until you get it?". Up to this point I had not
coded anything in Ruby on my own (aside from the examples in the book)
and I saw this as a great first challenge. So here is what I got. It
took me some time. I ran into trouble because I forgot to take into
consideration the Class of the variables and couldn't figure out why the
loops weren't working.
My question to you all... how could I have done this better or do you
seeing anything that is wrong. Attached is the code. Thanks for the
feedback.

Attachments:
http://www.ruby-forum.com/attachment/6168/100guess.rb


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

 
Reply With Quote
 
 
 
 
Stu
Guest
Posts: n/a
 
      05-05-2011
Looks good. You don't need the extra call to_i where it is already an integ=
er:

random =3D rand(100)
choice =3D=3D random

Both expressions are fine without any castiing.

small nit pick. ruby is on my machine in /usr/local/bin ... to make
this portable use:

#!/usr/bin/env ruby

I suggest your next task is to break everything down to methods. When
you get comfortable with the concept of methods make your user input
more strict as to only acquire a number. You did a very good job,
Keep it going.

~Stu

On Thu, May 5, 2011 at 1:45 AM, Super Goat <ruby-forum@sgoat.33mail.com> wr=
ote:
> I am a new Rubyist. I told my friend that I was learning Ruby. He asked
> me how that was going and then gave me a little challenge. His
> challenge, "Write a text game that guesses numbers 0-100". My reply,
> "you mean it picks a number at random, and you guess the number, it
> tells you higher or lower until you get it?". Up to this point I had not
> coded anything in Ruby on my own (aside from the examples in the book)
> and I saw this as a great first challenge. So here is what I got. It
> took me some time. I ran into trouble because I forgot to take into
> consideration the Class of the variables and couldn't figure out why the
> loops weren't working.
> My question to you all... how could I have done this better or do you
> seeing anything that is wrong. =A0Attached is the code. Thanks for the
> feedback.
>
> Attachments:
> http://www.ruby-forum.com/attachment/6168/100guess.rb
>
>
> --
> Posted via http://www.ruby-forum.com/.
>
>


 
Reply With Quote
 
 
 
 
Josh Cheek
Guest
Posts: n/a
 
      05-05-2011
[Note: parts of this message were removed to make it a legal post.]

On Thu, May 5, 2011 at 1:45 AM, Super Goat <ruby-forum@sgoat.33mail.com>wrote:

> I am a new Rubyist. I told my friend that I was learning Ruby. He asked
> me how that was going and then gave me a little challenge. His
> challenge, "Write a text game that guesses numbers 0-100". My reply,
> "you mean it picks a number at random, and you guess the number, it
> tells you higher or lower until you get it?". Up to this point I had not
> coded anything in Ruby on my own (aside from the examples in the book)
> and I saw this as a great first challenge. So here is what I got. It
> took me some time. I ran into trouble because I forgot to take into
> consideration the Class of the variables and couldn't figure out why the
> loops weren't working.
> My question to you all... how could I have done this better or do you
> seeing anything that is wrong. Attached is the code. Thanks for the
> feedback.
>
> Attachments:
> http://www.ruby-forum.com/attachment/6168/100guess.rb
>
>

Hi, Super Goat

Congratulations on your first program! Hope you're enjoying Ruby.
Here are some thoughts, that might be helpful:

* On line1 your shebang is `#!/usr/bin/ruby`, but the best shebang is
`#!/usr/bin/env ruby` because Ruby can be located in lots of different
places, but env is always (I think) located in /usr/bin/env, and it is smart
enough to figure out where your real Ruby is and then invoke it.

* On line 4, you have `rand(100).to_i`, rand already returns an integer when
you pass it a parameter, so you don't need to use to_i on it (
http://www.ruby-doc.org/core/classes...l.html#M001388)

* For that variable, I would have probably named it "target" rather than
"random", I think this clearer.

* On line 11, `if choice > random.to_i` again you are converting it to an
integer, but it is already an integer. (not harmful, but redundant and thus
confusing as it makes reader say "wait, I thought it was an integer already,
what did I miss?")

* Lines 13-14 look like this:
if choice > random.to_i
puts "\nIncorrect, please choose a lower number."
else puts "\nIncorrect, please choose a higher number."
end

Your indenting is off, the if/else/end should be at the same level, and for
multiline conditionals, the code would go between them, so it should look
like this:

if choice > target
puts "\nIncorrect, please choose a lower number."
else
puts "\nIncorrect, please choose a higher number."
end

I would personally probably save space by putting the conditional into the
string like this:
puts "\nIncorrect, please choose a #{if choice > target then 'higher' else
'lower' end} number"

I'm not sure whether many other Rubyists would do that or not, but I like it
much better.

* On line 16, you have gets.chomp.to_i The chomp here is unnecessary,
because to_i will disregard anything after the digit.

---

Anyway, nice job. Keep it up, after you get comfortable with the language,
you can play with gems, which is where all the fun stuff is!

 
Reply With Quote
 
John Feminella
Guest
Posts: n/a
 
      05-05-2011
There's a small bug:

=3D=3D=3D=3D
puts "Choose a number between 0 and 100"
random =3D rand(100).to_i
=3D=3D=3D=3D

This actually produces numbers between 0 and 99 inclusive, not 0 and
100. Also the name "random" doesn't describe the point of the variable
in the program, which is to serve as the answer. So it might be better
to call it "answer", for instance. Finally, `rand(100)` already
produces an integer, so you don't need the .to_i method.

Also, for this condition,

=3D=3D=3D=3D
until choice <=3D 100 and choice >=3D 0
=3D=3D=3D=3D

a more succinct way of writing this is to check if it's included in
the range 0..100:

=3D=3D=3D=3D
until (0..100).include? choice
=3D=3D=3D=3D

Next, notice that "100" appears a lot, so it might be better to
describe what that number is -- it's the maximum value that your
random number can take on. So, w emight write:

=3D=3D=3D=3D
max =3D 100
=3D=3D=3D=3D

and then use `max` everywhere instead of the `100` literal.

Otherwise, looks good overall for your first attempt at this, minus a
few rough spots. For another challenge, you might try writing a
program that can guess a number between 0 and 100 in as few guesses as
possible (hint: it should take around 7 guesses).

~ jf
--
John Feminella
Principal Consultant, BitsBuilder
LI: http://www.linkedin.com/in/johnxf
SO: http://stackoverflow.com/users/75170/



On Thu, May 5, 2011 at 02:45, Super Goat <ruby-forum@sgoat.33mail.com> wrot=
e:
> I am a new Rubyist. I told my friend that I was learning Ruby. He asked
> me how that was going and then gave me a little challenge. His
> challenge, "Write a text game that guesses numbers 0-100". My reply,
> "you mean it picks a number at random, and you guess the number, it
> tells you higher or lower until you get it?". Up to this point I had not
> coded anything in Ruby on my own (aside from the examples in the book)
> and I saw this as a great first challenge. So here is what I got. It
> took me some time. I ran into trouble because I forgot to take into
> consideration the Class of the variables and couldn't figure out why the
> loops weren't working.
> My question to you all... how could I have done this better or do you
> seeing anything that is wrong. =C2=A0Attached is the code. Thanks for the
> feedback.
>
> Attachments:
> http://www.ruby-forum.com/attachment/6168/100guess.rb
>
>
> --
> Posted via http://www.ruby-forum.com/.
>
>


 
Reply With Quote
 
Jesús Gabriel y Galán
Guest
Posts: n/a
 
      05-05-2011
On Thu, May 5, 2011 at 8:45 AM, Super Goat <ruby-forum@sgoat.33mail.com> wr=
ote:
> I am a new Rubyist. I told my friend that I was learning Ruby. He asked
> me how that was going and then gave me a little challenge. His
> challenge, "Write a text game that guesses numbers 0-100". My reply,
> "you mean it picks a number at random, and you guess the number, it
> tells you higher or lower until you get it?". Up to this point I had not
> coded anything in Ruby on my own (aside from the examples in the book)
> and I saw this as a great first challenge. So here is what I got. It
> took me some time. I ran into trouble because I forgot to take into
> consideration the Class of the variables and couldn't figure out why the
> loops weren't working.
> My question to you all... how could I have done this better or do you
> seeing anything that is wrong. =A0Attached is the code. Thanks for the
> feedback.
>
> Attachments:
> http://www.ruby-forum.com/attachment/6168/100guess.rb


Apart from the other comments, I would like to point out that the
check about the choice being a valid number (between 0 and 100) is
made only for the first choice. After the first incorrect choice, you
already inside the second loop, and don't check again.
My advice is to create a method that contains that logic: outputting a
prompt, getting the input from the user and validating it, the code
would get much cleaner:

def get_valid_input
# the prompt and validation logic here
end

target =3D rand(100)
until (choice =3D get_valid_input) =3D=3D target
puts "Wrong, my number is #{choice < target ? 'higher' : 'lower'}"
end

Or something like that.

Jesus.

 
Reply With Quote
 
Martin Boese
Guest
Posts: n/a
 
      05-05-2011
On Thu, 5 May 2011 15:45:52 +0900
Super Goat <ruby-forum@sgoat.33mail.com> wrote:

> I am a new Rubyist. I told my friend that I was learning Ruby. He
> asked me how that was going and then gave me a little challenge. His
> challenge, "Write a text game that guesses numbers 0-100". My reply,
> "you mean it picks a number at random, and you guess the number, it
> tells you higher or lower until you get it?". Up to this point I had
> not coded anything in Ruby on my own (aside from the examples in the
> book) and I saw this as a great first challenge. So here is what I
> got. It took me some time. I ran into trouble because I forgot to
> take into consideration the Class of the variables and couldn't
> figure out why the loops weren't working.
> My question to you all... how could I have done this better or do you
> seeing anything that is wrong. Attached is the code. Thanks for the
> feedback.


Hey, ruby-quiz is back?!
This is how I would have done it... One loop, break-out once you got the
number. Maybe it helps.. Cheers, Martin


magic = rand(101).to_i
puts "Guess the magic number!"

loop do
print "Choose 0-100> "
n = gets.to_i
if !(0..100).include?(n)
puts "0-100!"
next
end
puts "Higher!" if n<magic
puts "Lower!" if n>magic
break if n==magic
end

puts "You won: #{magic} is the number!"

>
> Attachments:
> http://www.ruby-forum.com/attachment/6168/100guess.rb
>
>




 
Reply With Quote
 
Super Goat
Guest
Posts: n/a
 
      05-06-2011
Thanks everyone for taking the time to reply!

The reason I had 'rand(100).to_i' and 'random.to_i' is because in IRB it
told me it was a Fixnum. Also, I had just figured out why my operators
weren't working so I wanted everything to be an integer. Obviously, in
retrospect, I see now that I didn't have to do it that way.

After I submitted to Ruby-Forum I did notice that when I was checking to
see if the input was within range that it would only do this at the
beginning. Putting that code in was an afterthought. I also thought
that I should really check to see if it was a number but I didn't know
how to do that.

I was unaware that I didn't need the chomp in 'gets.chomp.to_i'.

I am still trying to wrap my head around Methods. I get it, but don't
know how to put down the code. Even when I look at Jesus's example I am
a bit confused but I have a feeling that using Methods is a very Rubyist
thing and the way Ruby was intended to be written.

I really like Martin's example.

I am going to try to use a Method and make this work and see if I can
figure out how to reject any input that isn't a number (probably isn't
hard just don't know how yet).

I want to thank everyone again for their help. I definitely feel
welcomed and will be back.

Goat

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

 
Reply With Quote
 
Jesús Gabriel y Galán
Guest
Posts: n/a
 
      05-06-2011
On Fri, May 6, 2011 at 8:18 AM, Super Goat <ruby-forum@sgoat.33mail.com> wr=
ote:
> I am still trying to wrap my head around Methods. =A0I get it, but don't
> know how to put down the code. =A0Even when I look at Jesus's example I a=

m
> a bit confused but I have a feeling that using Methods is a very Rubyist
> thing and the way Ruby was intended to be written.


A method is just a block of code with a name that can be invoked from
other points in the code.
It can receive parameters, which are like local variables within the method=
 
Reply With Quote
 
Josh Cheek
Guest
Posts: n/a
 
      05-06-2011
[Note: parts of this message were removed to make it a legal post.]

On Fri, May 6, 2011 at 1:18 AM, Super Goat <ruby-forum@sgoat.33mail.com>wrote:

> Thanks everyone for taking the time to reply!
>
> The reason I had 'rand(100).to_i' and 'random.to_i' is because in IRB it
> told me it was a Fixnum. Also, I had just figured out why my operators
> weren't working so I wanted everything to be an integer. Obviously, in
> retrospect, I see now that I didn't have to do it that way.
>
>

In OOP, classes can inherit functionality from other classes. There are
several kinds of Integers, Fixnum is one of them, Bignum is another (okay,
actually, those are the only two).

1.class # => Fixnum
1.kind_of? Integer # => true
Fixnum.ancestors # => [Fixnum, Integer, Numeric, Comparable, Object,
Kernel, BasicObject]

n = 10**1000
n.class # => Bignum
n.kind_of? Integer # => true
Bignum.ancestors # => [Bignum, Integer, Numeric, Comparable, Object,
Kernel, BasicObject]

 
Reply With Quote
 
Johannes Held
Guest
Posts: n/a
 
      05-06-2011
On 05.05.2011 10:10, John Feminella wrote:
> For another challenge, you might try writing a program that can guess
> a number between 0 and 100 in as few guesses as possible (hint: it
> should take around 7 guesses).

Ah, that's a nice challenge.
You could even try to solve it with the functional pradigm. In this
case: recursion!

http://pastie.org/1870939

--
Johannes
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
code for number guessing by computer pramodmc4u@gmail.com Python 1 09-30-2007 06:22 PM
Guessing and destructors Narf the Mouse C++ 15 10-02-2006 08:40 PM
Finding the square root and guessing the number sathyashrayan C Programming 4 04-22-2006 08:08 PM
Guessing _charset for MIMEText ? Robert Python 0 01-18-2006 04:55 PM
An attempt at guessing the encoding of a (non-unicode) string Christos TZOTZIOY Georgiou Python 12 04-07-2004 09:20 AM



Advertisments