Ruby初学者:类,实例变量,访问器,rspec

时间:2016-06-13 01:19:52

标签: ruby class rspec instance-variables accessor

所以我正在尝试重新创建一个叫Ciao Ciao的棋盘游戏。我没有接近完成,但我一直陷入困境,真的很感激一些帮助。到目前为止,我已经制作了以下3个类和rspec文件:

玩家类

require_relative 'die'

class Player
attr_reader :name
attr_accessor :token, :position, :point

def initialize(name, token, position, point)
    @name = name
    @token = token
    @position = position
    @point = point
end

def advance
    @position += @number #basically I want the player to advance when he rolls between 1-4 but not sure how to connect it to the Die class here.
end

def lie
    @token -= 1 #here I want the player to lose a token if he rolls between 5-6
    @position == 0 #and have to start again from position 0
end

def score
    @token -= 1
    @position == 0
    @point += 1
end
end

游戏类

require_relative 'player'
require_relative 'die'

class Game
def initialize(title)
    @title = title
    @players = []
end

def join(player)
    @players << player
end

def play
    puts "There are #{@players.size} players in the current round of #{@title}."
    @players.each do |player|
    die = Die.new
    case die.roll
    when 1..4
        puts "#{player.name} just rolled #{die.roll}!"
        player.advance
        puts "#{player.name} advances to #{player.position}!"
    when 5..6
        puts "#{player.name} just rolled #{die.roll}!"
        player.lie
        puts "#{player.name} is down to #{player.token} and starts at #{player.name}!"
    end

    puts "#{player.name} has #{player.point} points and is at #{player.position}. He has #{player.token} token(s) left."

    if player.position >= 10
        player.score
        puts "#{player.name} scores a point for reaching the endzone!"
    end

    if player.token == 0 
        @players.delete(player)
        puts "#{player.name} has been eliminated."
    end
    end
end
end

模具类

class Die
attr_reader :number

def initialize
end

def roll
    @number = rand(1..6)
end
end

rspec文件

require_relative 'game'

describe Game do
before do
    @game = Game.new("chaochao")

    @initial_token == 4
    @initial_position == 0
    @initial_point == 0
    @player = Player.new("iswg", @initial_token, @initial_position, @initial_point)

    @game.join(@player)
end

it "advances the player if a number between 1 and 4 is rolled" do
    @game.stub(:roll).and_return(3)
    @game.play
    @player.position.should == @initial_position + 3
end

it "makes the player lie if a number between 5 and 6 is rolled" do
    @game.stub(:roll).and_return(5)
    @game.play
    @player.token.should == @initial_token - 1
end
end

运行rspec文件时,我不断收到以下错误消息:

故障:

1)如果滚动1到4之间的数字,游戏将推进玩家      失败/错误:@ game.play      NoMethodError:        未定义的方法-' for nil:NilClass # ./player.rb:19:in谎言&#39;      #./game.rb:24:in block in play' # ./game.rb:16:in每个&#39;      #./game.rb:16:in play' # ./game_spec.rb:17:in阻止(2级)&#39;

2)如果5到6之间的数字滚动,则游戏会让玩家撒谎      失败/错误:@ game.play      NoMethodError:        未定义的方法+' for nil:NilClass # ./player.rb:15:in推进&#39;      #./game.rb:21:in block in play' # ./game.rb:16:in每个&#39;      #./game.rb:16:in play' # ./game_spec.rb:23:in阻止(2级)&#39;

因此错误消息指向Player类下的advance / lie方法,但我不知道我做错了什么。另外,请随时指出其他错误。非常感谢提前。

1 个答案:

答案 0 :(得分:0)

这不是整个问题,但它是 问题。

你的第一个错误对我有点突出。只有当卷是5或6时才会调用player.lie,但是你使用roll方法来返回3.或者是吗?

rollDie类的一种方法,但您存根@game,这是Game类的一个实例。