-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpart2.rb
69 lines (58 loc) · 1.78 KB
/
part2.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
61
62
63
64
65
66
67
68
69
#john meehan
class WrongNumberOfPlayersError < StandardError ; end
class NoSuchStrategyError < StandardError ; end
def rps_result(m1, m2)
# YOUR CODE HERE
#see which move wins
return (m1 + m2) =~ /rs|sp|pr|rr|ss|pp/i #check if combo is a winner returns true if it is
#what if it is a draw?
end
def rps_game_winner(game)
# YOUR CODE HERE
raise WrongNumberOfPlayersError unless game.length == 2
strategy = ["r","p","s"] # array of possible strategys or moves
raise NoSuchStrategyError unless strategy.include?(game[0][1].downcase) && strategy.include?(game[1][1].downcase)#raise and error if move is not part of the strategy
#takes played moves and check if it is a winning combo
if rps_result(game[0][1], game[1][1])
game[0]
else
game[1]
end
end
def rps_tournament_winner(tournament)
# takes in a tournament as an array and returns a winner
# YOUR CODE HERE
#if tournament.length <= 1
# if 1 player do nothing?
#elsif tournament.length >=2
# 2 player play normal
#puts tlength
#puts tournament
#tournament.each do |round|
# puts round.length
# puts rps_game_winner(round)
#end
if tournament[0][1].class==String
rps_game_winner(tournament)
else
round1 = rps_tournament_winner(tournament[0])
round2 = rps_tournament_winner(tournament[1])
rps_tournament_winner([round1,round2])
end
end
#test dave should win
puts "**** Test Game **** Dave should win with S"
puts rps_game_winner([ ["Armando", "P"], ["Dave", "S"] ])
#test part2
puts "**** Test Tounrament ****Richard should Win With R"
puts rps_tournament_winner([
[
[ ["Armando", "P"], ["Dave", "S"] ],
[ ["Richard", "R"], ["Michael", "S"] ],
],
[
[ ["Allen", "S"], ["Omer", "P"] ],
[ ["David E.", "R"], ["Richard X.", "P"] ]
]
])
#Richard should win with R