Skip to content

Instantly share code, notes, and snippets.

@Seralto
Last active May 9, 2016 04:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Seralto/0dc71826f727486a758f2dc6366082da to your computer and use it in GitHub Desktop.
Save Seralto/0dc71826f727486a758f2dc6366082da to your computer and use it in GitHub Desktop.
Jogo Jokenpo em Ruby
def calcula_vencedor(sua_escolha, pc_escolha)
resultado = (sua_escolha - pc_escolha) % 3
if resultado == 1
'Você ganhou!'
elsif resultado == 2
'PC Ganhou!'
else
'Deu empate!'
end
end
opcoes = {
1 => 'Pedra',
2 => 'Papel',
3 => 'Tesoura'
}
novo_jogo = 's'
while novo_jogo == 's'
opcoes.each do |k, v|
puts "#{k} - #{v}"
end
print 'Escolha uma opção acima: '
sua_escolha = gets.to_i
while opcoes[sua_escolha].nil?
print 'Opção invalida! Escolha novamente: '
sua_escolha = gets.to_i
end
pc_escolha = Random.rand(3) + 1
puts "\nVocê escolheu #{opcoes[sua_escolha]}"
puts "O PC escolheu #{opcoes[pc_escolha]}\n\n"
print calcula_vencedor(sua_escolha, pc_escolha)
print "\n\nDeseja jogar novamente? (s/n): "
novo_jogo = gets.chomp
end
puts 'Obrigado por jogar!'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment