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()
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.
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()
-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
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.