Ruby skips if statements -
i have ruby program here w/c consists of hashes. when user enters l or p or sc check if computer generated key matches criteria on first variable w/c player or user. if matches criteria return won. if it's not return loose. otherwise if it's same print tie.
my_choices = { 'l' => 'light', 'p' => 'paper', 'sc' => 'scan', } def test?(first, second) (first == 'p' && second == my_choices ['l']) || (first == 'l' && second == my_choices ['p']) || (first == 'sc' && second == my_choices ['sc']) || (first == 'l' && second == my_choices ['l']) end def print_results(player, computer) if test?(player, computer) puts("you won!") elsif test?(computer, player) puts("computer won! loose!") else puts("it's tie!") end end puts "enter key: " choice = gets().chomp() computer_choice = my_choices.values.sample print_results(choice, computer_choice) puts("you chose: #{choice}, computer choose: #{computer_choice}"
with codes above, code skipping codes , prints "it's tie" don't missing here. idea?
you're getting user's input , testing against 'rock', 'paper' , 'scissors', you've stated user entering 'l', 'p', , 'sc' test never evaluate true
as example, saving rps.rb , running:
> ruby rps.rb enter key: rock won! chose: rock, computer choose: light
Comments
Post a Comment