我正在尝试创建一个石头剪刀布程序,到目前为止,此代码有效,但没有抛出参数,我该如何解决这个问题?
class RockPaperScissors
# Exceptions this class can raise:
class NoSuchStrategyError < StandardError ; end
def self.winner(player1, player2)
p1c = player1.last.downcase
p2c = player2.last.downcase
p1wins = false
case p1c
when "p"
if (p2c == "p" or p2c == "r")
p1wins = true
end
when "s"
if (p2c == "p" or p2c == "s")
p1wins = true
end
when "r"
if (p2c == "r" or p2c == "s")
p1wins = true
end
else
raise NoSuchStrategyError, "Strategy must be one of R,P,S"
end
end
为什么错误不抛出?编辑*
这用于使用 rspec 测试代码,因为您可以看到它是一个带有名称的数组,后跟他们在大写字母中选择的内容
before(:each) do
@rock = ['Armando','R'] ; @paper = ['Dave','P'] ; @scissors = ['Sam','S']
end
describe 'game' do
it 'rock breaks scissors' do
RockPaperScissors.winner(@rock, @scissors).should == @rock
end
it 'scissors cut paper' do
RockPaperScissors.winner(@paper, @scissors).should == @scissors
end
it 'paper covers rock' do
RockPaperScissors.winner(@rock, @paper).should == @paper
end
it 'first player wins if both use same strategy' do
RockPaperScissors.winner(@scissors, ['Dave','S']).should == @scissors
end
end
it "should raise NoSuchStrategyError if strategy isn't R, P, or S" do
lambda { RockPaperScissors.winner(@rock, ['Dave', 'w']) }.
should raise_error(RockPaperScissors::NoSuchStrategyError,
"Strategy must be one of R,P,S")
end