My application doesn't work well

B

Bruno Santana

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)
 
G

Glen Holcomb

On Mon, Apr 12, 2010 at 3:16 PM, Bruno Santana <[email protected]=
r
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 =3D ['pedra','papel','tesoura']
numero =3D 1 + rand(3)
return numero
end

def joga(usuario)

result =3D sorteia()
resultSorteio =3D result.to_i

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

end

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

end
end

j =3D Jogo.new()

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

j.joga(opcaoUsuario)
Your if was nested funny, this should do what you want, I think:

class Jogo

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

def joga(usuario)

result =3D sorteia()
resultSorteio =3D result.to_i

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

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

end
end

j =3D Jogo.new()

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

j.joga(opcaoUsuario)

--=20
"Hey brother Christian with your high and mighty errand, Your actions speak
so loud, I can=92t hear a word you=92re saying."

-Greg Graffin (Bad Religion)
 
S

steve ross

Try this:

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

def joga(usario)
macina =3D sorteia

puts usario.chop =3D=3D 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
 
T

thunk

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
 
B

Bruno Santana

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

Bruno Santana

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

Bruno Santana

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

Jonathan Nielsen

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 Nielsen
 
R

Rick DeNatale

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?

=A0if usuario =3D=3D maquina then
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0puts("empatou")
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0else
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0puts("alguem ganhou")
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0end

The whole code is in my previous post. Thanks.

Well, it looks like the two values really aren't the same. Two observation=
s.

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.



--=20
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Github: http://github.com/rubyredrick
Twitter: @RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale
 
B

Bruno Santana

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.
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top