From: Bruno Santana on 12 Apr 2010 17:16 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) -- Posted via http://www.ruby-forum.com/.
From: Glen Holcomb on 12 Apr 2010 17:28 On Mon, Apr 12, 2010 at 3:16 PM, Bruno Santana <bruno_r_santana(a)yahoo.com.br > wrote: > 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) > -- > Posted via http://www.ruby-forum.com/. > > 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 cant hear a word youre saying." -Greg Graffin (Bad Religion)
From: steve ross on 12 Apr 2010 17:32 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 On Apr 12, 2010, at 2:16 PM, Bruno Santana wrote: > > 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)
From: Bruno Santana on 12 Apr 2010 18:16 it seems your suggestion worked, I'll check it better and if I have some more ploblems I'll put here. Thanks a lot. :) -- Posted via http://www.ruby-forum.com/.
From: Bruno Santana on 12 Apr 2010 20:00 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) -- Posted via http://www.ruby-forum.com/.
|
Next
|
Last
Pages: 1 2 Prev: error: www deprecation notice Next: Thread to handle an execute command and wait for output |