我必须实施Rock,Paper,Scissors游戏的一小部分作为作业。这是我的代码:
class RockPaperScissors
# Exceptions this class can raise:
class NoSuchStrategyError < StandardError ; end
def self.winner(player1, player2)
# YOUR CODE HERE
puts "player1 = " + player1[1]
if (player1[1] || player2[1] != 'R') || (player1[1] || player[2] != 'P') || (player1[1] || player2[1] != 'S')
raise NoSuchStrategyError.new("Strategy must be one of 'R','S','P'")
else
if player1[1] == player2[1]
return player1
elsif (player1[1] == 'R' && player2[1] == 'P') || (player1[1] == 'P' && player2[1] == 'S') || (player1[1] == 'S' && player2[1] == 'R')
return player2
else
return player1
end
end
end
def self.tournament_winner(tournament)
# YOUR CODE HERE
end
end
我正在使用rspec测试代码,它在以下两种情况下都会中断。我假设它与我检查的位置有关,以确保输入有效,即使输入有效,无论出于何种原因,我在行if (player1[1] || player2[1] != 'R') || (player1[1] || player[2] != 'P') || (player1[1] || player2[1] != 'S')
的方式使输入失败在每个案例上。如何修复该行,以便在输入有效时,它不会引发错误?
Failures:
1) RockPaperScissors game rock breaks scissors [10 points]
Failure/Error: raise NoSuchStrategyError.new("Strategy must be one of 'R','S','P'")
RockPaperScissors::NoSuchStrategyError:
Strategy must be one of 'R','S','P'
# ./lib/rock_paper_scissors.rb:11:in `winner'
# ./spec/rock_paper_scissors_spec.rb:10:in `block (3 levels) in <top (required)>'
编辑:player1和player2是格式为[“playername”,“move”]的数组,其中R,S或P为摇滚,剪刀或纸张