-
Notifications
You must be signed in to change notification settings - Fork 68
/
RockPaperScissors.rb
57 lines (47 loc) · 1.19 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
=begin
Let's play! You have to return which player won! In case of a draw return Draw!.
Examples:
rps('scissors','paper') // Player 1 won!
rps('scissors','rock') // Player 2 won!
rps('paper','paper') // Draw!
=end
# My Solution
def rps(p1, p2)
result = 0
case p1
when "rock"
puts "HERE"
result = 0 if p2 == "rock"
result = 1 if p2 == "scissors"
result = 2 if p2 == "paper"
when "paper"
result = 0 if p2 == "paper"
result = 1 if p2 == "rock"
result = 2 if p2 == "scissors"
when "scissors"
result = 0 if p2 == "scissors"
result = 1 if p2 == "paper"
result = 2 if p2 == "rock"
end
result == 0 ? "Draw!" : result == 1 ? "Player 1 won!" : "Player 2 won!"
end
# Another Solution
def rps(p1, p2)
return 'Draw!' if p1 == p2
result = win_matrix[p1.to_sym][p2.to_sym]
"Player #{result} won!"
end
def win_matrix
{
rock: { paper: 2, scissors: 1 },
paper: { scissors: 2, rock: 1 },
scissors: { rock: 2, paper: 1 }
}
end
# Another Solution
winning_moves = {
'rock' => 'scissors', 'scissors' => 'paper', 'paper' => 'rock'
}
return 'Draw!' if p1 == p2
winning_moves[p1] == p2 ? 'Player 1 won!' : 'Player 2 won!'
end