0
votes

i am just starting my journey with ruby and am very new to programming in general as well. This is code for a homework assignment. I am getting syntax error as described in title. I understand that I have added some extra end while end of file was expected? right? but where is this extra "end".

The code:

class WrongNumberOfPlayersError < StandardError ; end
class NoSuchStrategyError < StandardError ; end

def rps_result(move1,move2)

  if (move1 == "r" && move2 == "s") || (move1 == "s" && move2 == p) || (move1 == "p" && move2 == "r")
    return true 
  else 
    return false
  end

end

  def rps_game_winner(player1, player2)    
    strategy = /[rps]/
    raise NoSuchStrategyError unless (move1 =~ strategy) & (move2 =~ strategy) 

    move1 = player1[1].downcase
    move2 = player2[2].downcase

    rps_result(move1, move2)? player1 : player2
  end
end

def rps_tournament_winner(game)
  round_winners = []

  if game.length < 2
    raise WrongNumberOfPlayersError
  else
    game.each_slice(2) do
      |l1 , l2|
      round_winners << rps_game_winner(l1, l2)
    end

    rps_tournament_winner(round_winners) 
  end
end

rps_tournament_winner([[[["Richard", "S"], ["Dave", "S"]], [["Richard", "R"], ["Michael", "S"]]], [[["Allen", "S"], ["Omer", "P"]], [["David E.", "R"], ["Richard X.", "P"]]]])
2
In the future, please format your question for clarity (there is a view below the edit pane that shows how the question will actually be displayed) - jwalk

2 Answers

0
votes

The rps_result method is never closed. Move the second end at the end of rps_game_winner to the last intended line of the rps_result method.

P.s. sorry for all the end's :)

0
votes

I have modified the second block of code. Please check if it works.

def rps_game_winner(player1, player2)    
    strategy = /[rps]/
    raise NoSuchStrategyError unless (move1 =~ strategy) && (move2 =~ strategy) 

    move1 = player1[1].downcase
    move2 = player2[2].downcase

    rps_result(move1, move2) ? player1 : player2
end    

def rps_tournament_winner(game)
  round_winners = []

  if game.length < 2
    raise WrongNumberOfPlayersError
  else
    game.each_slice(2) do |l1 , l2|
      round_winners << rps_game_winner(l1, l2)
    end

    rps_tournament_winner(round_winners) 
  end
end

rps_tournament_winner([[[["Richard", "S"], ["Dave", "S"]], [["Richard", "R"], ["Michael", "S"]]], [[["Allen", "S"], ["Omer", "P"]], [["David E.", "R"], ["Richard X.", "P"]]]])

Did 4 changes:

  1. Removed extra end in rps_game_winner method.

  2. Added space between tertiary operators in rps_game_winner method.

  3. rps_game_winner method, 2nd line, unless condition had only one &.

  4. In rps_tournament_winner method, moved the args |l1, l2| after do.