-
Notifications
You must be signed in to change notification settings - Fork 1
/
rockpaperscissors.rb
43 lines (40 loc) · 1.5 KB
/
rockpaperscissors.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
def who_wins(user_weapon, computer_weapon)
# A hash of all the weapon combinations as keys, winners as values
results = {
"rockscissors" => "user",
"paperrock" => "user",
"scissorspaper" => "user",
"rockpaper" => "computer",
"paperscissors" => "computer",
"scissorsrock" => "computer",
}
# the method returns the value in the hash, based on the weapon combo
return results[user_weapon+computer_weapon]
end
# Rock, Paper Scissors
weapons = ["rock", "paper", "scissors"]
computer_score = 0
user_score = 0
# get input, test for the tie case, then if not a tie get who wins from the hash
while computer_score <5 && user_score <5
# get some input and check if is valid
user_weapon = " "
while !weapons.include?(user_weapon) # loop until the user chooses a valid weapon
puts "please choose your weapon (enter 'rock', 'paper' or 'scissors')"
user_weapon = gets.chomp.downcase
end
# choose a weapon for the computer
computer_weapon = weapons.sample
# rumble!
puts "you chose #{user_weapon}, computer chose #{computer_weapon}:"
if user_weapon == computer_weapon
puts " tie! Score remains you=#{user_score}, computer=#{computer_score}"
elsif who_wins(user_weapon, computer_weapon) == "user"
puts " you win! Score is you=#{user_score+1}, computer=#{computer_score}"
user_score +=1
else
puts " computer wins! Score is you=#{user_score}, computer=#{computer_score +1}"
computer_score +=1
end
end
puts "Thanks for playing! Final score is you=#{user_score}, computer=#{computer_score}"