t = [
[
[["Armando", "P"],["Dave", "S"]],
[["Richard", "R"],["Michael", "S"]],
],
[
[["Allen", "S"],["Omer", "P"]],
[["David E.", "R"], ["Richard X.", "P"]]
]
]
def rps_game_winner(game)
raise WrongNumberOfPlayersError unless game.length == 2
if (game[0][1] =~ /[r]/i && game[1][1] =~ /[s]/i) || (game[0][1] =~ /[s]/i && game[1][1] =~ /[p]/i) || (game[0][1] =~ /[p]/i && game[1][1] =~ /[r]/i)
return game[0]
elsif (game[0][1] =~ /[r]/i && game[1][1] =~ /[p]/i) || (game[0][1] =~ /[s]/i && game[1][1] =~ /[r]/i) || (game[0][1] =~ /[p]/i && game[1][1] =~ /[s]/i)
return game[1]
elsif game[0][1] == game[1][1]
return game[0]
else
raise NoSuchStrategyError.new
end
end
def rps_tournament_winner(t)
t.each do |pair|
yield pair
end
end
rps_tournament_winner(t) { |x| rps_game_winner(x) }
So the error I am getting is: NoSuchStrategyError: NoSuchStrategyError – which means that the yield is passing a value to the block and that is being passed to my method rps_game_winner and it is evaluating something and giving that error – per the method.
But it’s not evaluating it properly….because it should be looking at Armando and Dave and returning a winner, then it should go back and continue look at the next pair and return the winner, etc.
P.S. When I do puts pair, I see the right values – and I have tested rps_game_winner method on one single pair and it works fine. It’s just iterating over multiple pairs is giving me an issue with the passing of control back and forth.
Are you sure your t is correct?
Seems to me like it should be something like this:
I assume Armando plays Dave in the first game.
Richard plays Michael etc.
In your code [[“Armando”, “P”],[“Dave”, “S”]] plays [[“Allen”, “S”],[“Omer”, “P”]]. So when your rps_game_winner checks for game[0][1] it actually returns [“Dave”, “S”]. Not P.
edit1:
if you want to do the set-logic you speak of you need to modify rps_tournament_winner like so:
edit2:
made my own implementation. take from it what you will. but it does what you want it to do.
https://github.com/SpoBo/rock-paper-scissors
you basicaly need to keep track of the winners and let the winners play against themselves. my implementation allows for any number of players to play against each other. the only problem my implementation has is that a player always plays the same hand and as soon as 2 players with the same hands meet the game will become unpredictable. so it needs some modifications to handle this.