Naweed Chougle
Naweed Chougle

Reputation: 520

Implementing Rock, Paper, Scissors tournament in Ruby

I'm new to Ruby and am trying to implement a rock-paper-scissors tournament using the language, as an exercise.

The rps_game_winner method that's called within the code below returns the winner in a pair of competitors.

Here is my attempt so far:

def rps_tournament_winner(tournament)
  qualifying_round_winners = []
  for i in 0..0
  tournament.each_with_index do |x, xi|
    x.each_with_index do |y, yi|
      winner = rps_game_winner(y)
      qualifying_round_winners.push(winner) 
      qualifying_round_winners = qualifying_round_winners.each_slice(2).to_a
    end
  end
  tournament = qualifying_round_winners.each_slice(2).to_a
  end
  return tournament 
end

This is the input I provide:

puts rps_tournament_winner([
 [
   [ ["Armando", "P"], ["David", "S"] ],
   [ ["Richard", "R"],  ["Michael", "S"] ]
 ]

])

The output I get is:

David
S
Richard
R

This is where I get stuck. I'm unable to get these two compete against each other, and declare the final winner.

As you can see in the code above, the terminating condition of the for loop needs attention.

Any help?

Upvotes: 1

Views: 2401

Answers (1)

Sree Raj
Sree Raj

Reputation: 69

I have done some modifications in your code.

Note: If I give [ ["Armando", "P"], ["David", "S"] ] as input to my rps_game_winner() function it will give result like ["David", "S"].

def rps_tournament_winner(tournament)
  qualifying_round_winners = []
  for i in 0..0
  tournament.each_with_index do |x, xi|
    x.each_with_index do |y, yi|
      winner = rps_game_winner(y)
      qualifying_round_winners.push(winner) 
    end
  end
  tournament = rps_game_winner(qualifying_round_winners)
  end
  return tournament 
end

So for your input

[[ [["Armando", "P"], ["David", "S"]], [["Richard", "R"], ["Michael", "S"]] ]]

it will also return a result like ["Richard", "R"].

Upvotes: 2

Related Questions