-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrps.rb
61 lines (48 loc) · 1.45 KB
/
rps.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# user picks r/p/s
# computer randomly generates r/p/s
# user wins if: **put winning cases**
# else, computer wins
# display winning message, ask to play again
# 1. player picks either rock, paper, or scissors
# 2. compare paper > rock, rock > scissors, scissors > paper, or tie if same
CHOICES = { "p" => "paper", "s" => "scissors", "r" => "rock" }
puts "Welcome to rock, paper, scissors!"
def display_winning_msg(choice)
case choice
when "r"
puts "Rock smashes scissors."
when "p"
puts "Paper covers rock."
when "s"
puts "Scissors shred paper."
end
end
loop do
puts "Pick one: rock (r), paper (p), or scissors (s)"
player_choice = nil
loop do
player_choice = gets.chomp.downcase
if CHOICES.keys.include?(player_choice)
puts "You chose #{CHOICES[player_choice]}."
break
else
puts "That is not a valid selection!"
end
end
computer_choice = CHOICES.keys.sample
puts "The computer chose #{CHOICES[computer_choice]}."
if player_choice == computer_choice
puts "It's a tie!"
elsif (player_choice == "r" && computer_choice == "s") ||
(player_choice == "p" && computer_choice == "r") ||
(player_choice == "s" && computer_choice == "p")
display_winning_msg(player_choice)
puts "You win!"
else
display_winning_msg(computer_choice)
puts "Computer wins!"
end
puts "Want to play again? (put y/n)"
break if gets.chomp.downcase != "y"
end
puts "Thanks for playing! Goodbye."