My application doesn't work well

Hi everybody,

My application isn’t working well. Let me try to explain what’s wrong.
When I run my program appears something like:

ruby pedrapapeltesoura.rb
Digite pedra, papel ou tesoura: pedra
1
tesoura
pedra
alguem ganhou

This 1 that appears above is the value of resultSorteio variable, the
content of this variable come from a method which random a number
between 1 to 3. tesoura is the value of the variable maquina, however if
the variable resultSorteio is 1 the variable maquina should be pedra and
not be tesoura, and sometimes the variable is empty…if you try to play
many times you can see where is the word tesoura above will appears
nothing. Does somebody help me please. Here is my code:

class Jogo

    def sorteia
            opcoes = ['pedra','papel','tesoura']
            numero = 1 + rand(3)
            return numero
    end

    def joga(usuario)

            result = sorteia()
            resultSorteio = result.to_i

            puts(resultSorteio)
            puts (('11' * 2).to_i/2)
            if resultSorteio == 1 then
                    maquina = "pedra"
                    if resultSorteio == 2 then
                            maquina = "papel"
                    else
                            maquina = "tesoura"
                    end

            end

            puts(maquina)
            puts(usuario)
            usuario.chop
            if usuario == maquina then
                    puts("empatou")
            else
                    puts("alguem ganhou")
            end

    end

end

j = Jogo.new()

print "Digite pedra, papel ou tesoura: "
opcaoUsuario = gets()

j.joga(opcaoUsuario)

On Mon, Apr 12, 2010 at 3:16 PM, Bruno S.
<[email protected]

wrote:

alguem ganhou

           end
   end

Your if was nested funny, this should do what you want, I think:

class Jogo

   def sorteia
           opcoes = ['pedra','papel','tesoura']
           numero = 1 + rand(3)
           return numero
   end

   def joga(usuario)

           result = sorteia()
           resultSorteio = result.to_i

           puts(resultSorteio)
           puts (('11' * 2).to_i/2)
           if resultSorteio == 1 then
               maquina = "pedra"
           elsif resultSorteio == 2 then
               maquina = "papel"
           else
            maquina = "tesoura"
           end

           puts(maquina)
           puts(usuario)
           usuario.chop
           if usuario == maquina then
                   puts("empatou")
           else
                   puts("alguem ganhou")
           end

   end

end

j = Jogo.new()

print "Digite pedra, papel ou tesoura: "
opcaoUsuario = gets()

j.joga(opcaoUsuario)


“Hey brother Christian with your high and mighty errand, Your actions
speak
so loud, I can’t hear a word you’re saying.”

-Greg Graffin (Bad Religion)

it seems your suggestion worked, I’ll check it better and if I have some
more ploblems I’ll put here. Thanks a lot. :slight_smile:

Try this:

def sorteia
return [‘pedra’,‘papel’,‘tesoura’][rand(3)]
end

def joga(usario)
macina = sorteia

puts usario.chop == macina ? empatou" : “alguem ganhou”
end

I’m don’t read enough Portugese (?) to fully understand the intent of
your code, but the above should do pretty much the same as what you’ve
shown below, but should never give you randomly blank results.

Hope this helps

You young fellows are distorting the beloved noise to boredom ratio
here, and seem to me to most surely be TROLLING!

It is only not clear for what YET.

Show us all of your code, show it now, or BUGGER OFF!

davvis rieyan, sheriff

I’m not so good in English but I understood that I shoud show you my
whole code. I put a different if like what the guy above suggested and I
worked. Thanks:

class Jogo

    def sorteia
            opcoes = ['pedra','papel','tesoura']
            numero = 1 + rand(3)
            return numero
    end

    def joga(usuario)

            result = sorteia()
            resultSorteio = result.to_i

            puts(resultSorteio)
            if resultSorteio == 1 then
                    maquina = "pedra"
            elsif resultSorteio == 2 then
                    maquina = "papel"
            else
                    maquina = "tesoura"
            end


            puts(maquina)
            puts(usuario)
            usuario.chop
            if usuario == maquina then
                    puts("empatou")
            else
                    puts("alguem ganhou")
            end

    end

end

j = Jogo.new()

print "Digite pedra, papel ou tesoura: "
opcaoUsuario = gets()

j.joga(opcaoUsuario)

I ran the application and appears that:

-bash-4.0$ ruby pedrapapeltesoura.rb
Digite pedra, papel ou tesoura: pedra
1
pedra
pedra
alguem ganhou

the number 1 above is the content of the variable resultSorteio, the
first word “pedra” is the content of the variable maquina and the second
“pedra” is the content of the variable usuario. According to the code
below when both variables contain the value “pedra” it should be shown
“empatou”, however appears “alguem ganhou”. am I comparing the strings
properly?

if usuario == maquina then
puts(“empatou”)
else
puts(“alguem ganhou”)
end

The whole code is in my previous post. Thanks.

I’m not so good in English but I understood that I shoud show you my
whole code. I put a different if like what the guy above suggested and I
worked. Thanks:

Let me apologize (unofficially) on behalf of the list/newsgroup/forum
for that… your question and code provided was just fine. Please
feel welcome to ask questions here.

-Jonathan N.

Hi Rick,

I did as you said and it worked. I descovered that there was a newline
character, look at it:

-bash-4.0$ ruby pedrapapeltesoura.rb
Digite pedra, papel ou tesoura: pedra
1
“pedra”
“pedra\n”
alguem ganhou

I solved it usind the method chomp:
usuario.chomp!
p(maquina)
p(usuario)

Now it’s working:

-bash-4.0$ ruby pedrapapeltesoura.rb
Digite pedra, papel ou tesoura: pedra
1
“pedra”
“pedra”
empatou

Thank you and thank all of you. If you have any problem I’ll come back
here.

On Mon, Apr 12, 2010 at 8:35 PM, Bruno S.
[email protected] wrote:

first word “pedra” is the content of the variable maquina and the second

The whole code is in my previous post. Thanks.

Well, it looks like the two values really aren’t the same. Two
observations.

If there is trailing whitespace in one of the values the puts
statements won’t show this.

You’re printing the values before you alter one of them.

To get a better debugging perspective try changing

          puts(maquina)
          puts(usuario)
          usuario.chop

to

          usuario.chop
          p(maquina)
          p(usuario)

Which will show the ‘inspect’ output of the two values right before
you compare them.


Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Github: rubyredrick (Rick DeNatale) · GitHub
Twitter: @RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale