0

我正在尝试创建一个石头剪刀布程序,到目前为止,此代码有效,但没有抛出参数,我该如何解决这个问题?

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
4

1 回答 1

1

编辑:根据您新提供的测试代码,问题是您只测试代码中第一个玩家的策略。然而,在测试代码中,第一个玩家 ( @rock) 有一个有效的策略;这是第二个有无效策略的玩家。请参阅我的代码以了解测试两者的一种方法。


当我将缺少的内容添加end到您的代码中时,它对我有用,并且:

Player = Struct.new(:last)
RockPaperScissors.winner(
  Player.new("cats"),
  Player.new("dogs")
)
#=> /Users/phrogz/Desktop/tmp.rb:24:in `winner': Strategy must be one of R,P,S (RockPaperScissors::NoSuchStrategyError)

请注意,我会像这样重写您的方法:

class RockPaperScissors
  class NoSuchStrategyError < StandardError ; end
  LEGAL_MOVES = %w[r p s]
  def self.winner(player1, player2)
    p1c = player1.last.downcase
    p2c = player2.last.downcase
    unless LEGAL_MOVES.include?(p1c) && LEGAL_MOVES.include?(p2c)
      raise NoSuchStrategyError, "Strategy must be one of R,P,S"
    end
    if p1c!=p2c then
      case p1c
        when "r" then p2c=="s" ? player1 : player2
        when "p" then p2c=="r" ? player1 : player2
        when "s" then p2c=="p" ? player1 : player2
      end
    end
  end
end

这会在无效移动中引发错误,nil如果两个玩家打成平手则返回,否则返回其中一个玩家。您可以通过像这样重写内部部分来使其更简洁,但可以说不太清楚:

if p1c!=p2c then
  case p1c
    when "r" then p2c=="s"
    when "p" then p2c=="r"
    when "s" then p2c=="p"
  end ? player1 : player2
end
于 2013-10-23T05:12:54.470 回答