I'm supposed to create a method in ruby that will take in a structured,multi-dimensioned array, such as:
my_arr = [
[ [['Armando', 'P'], ['Dave', 'S']], [['Richard', 'R'], ['Michael', 'S']] ],
[ [['Allen', 'S'], ['Omer', 'P']], [['David E.', 'R'], ['Richard X.', 'P']] ]
]
This array is supposed to represent a tournament of Rock, paper & scissors, the number of players will always be 2^n and no repetitions (of players) are made.
The code I wrote is as follows:
class WrongNumberOfPlayersError < StandardError ; end
class NoSuchStrategyError < StandardError ; end
def rps_game_winner(game)
raise WrongNumberOfPlayersError unless game.length == 2
valid = ["r","p","s"]
a1=[(game[0][1]).downcase]
a2=[(game[1][1]).downcase]
raise NoSuchStrategyError unless (valid & a1) && (valid & a2)
return (game[0]) if a1 === a2
case (a1[0])
when "r"
case (a2[0])
when "p"
return (game[1])
else
return (game[0])
end
when "p"
case (a2[0])
when "s"
return (game[1])
else
return (game[0])
end
when "s"
case (a2[0])
when "r"
return (game[1])
else
return (game[0])
end
end
end
def rps_tournament_winner(tournament)
if tournament[0][0].is_a?(Array)
rps_tournament_winner(tournament[0])
elsif tournament[1][0].is_a?(Array)
rps_tournament_winner(tournament[1])
else
rps_game_winner(tournament)
end
end
So my problem is that given the use of array i mentioned earlier being passed to rps_tournament_winner Dave always wins instead of Richard and i haven't been able to figure out where i went wrong.
Ty for reading the wall of text/code :)
:paper,:rock,:scissorinstead of strings. It's easier to read the code and faster for the interpreter.