title: Ruby beginner: class, instance variable, accessor, rspec
body: <p>So I'm in the process of trying to recreate a board game called Ciao Ciao. I'm not anywhere near done but I keep getting stuck and would really appreciate some help. So far I've made the following 3 classes and rspec file:</p>
  
  <p>The Player class</p>
  
  <pre><code>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
  </code></pre>
  
  <p>The Game Class</p>
  
  <pre><code>require_relative 'player'
  require_relative 'die'
  
  class Game
  def initialize(title)
      @title = title
      @players = []
  end
  
  def join(player)
      @players &lt;&lt; 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 &gt;= 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
  </code></pre>
  
  <p>The Die Class</p>
  
  <pre><code>class Die
  attr_reader :number
  
  def initialize
  end
  
  def roll
      @number = rand(1..6)
  end
  end
  </code></pre>
  
  <p>The rspec file</p>
  
  <pre><code>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
  </code></pre>
  
  <p>I keep getting the following error message when I run the rspec file:</p>
  
  <p>Failures:</p>
  
  <p>1) Game advances the player if a number between 1 and 4 is rolled
       Failure/Error: @game.play
       NoMethodError:
         undefined method <code>-' for nil:NilClass
       # ./player.rb:19:in</code>lie'
       # ./game.rb:24:in <code>block in play'
       # ./game.rb:16:in</code>each'
       # ./game.rb:16:in <code>play'
       # ./game_spec.rb:17:in</code>block (2 levels) in '</p>
  
  <p>2) Game makes the player lie if a number between 5 and 6 is rolled
       Failure/Error: @game.play
       NoMethodError:
         undefined method <code>+' for nil:NilClass
       # ./player.rb:15:in</code>advance'
       # ./game.rb:21:in <code>block in play'
       # ./game.rb:16:in</code>each'
       # ./game.rb:16:in <code>play'
       # ./game_spec.rb:23:in</code>block (2 levels) in '</p>
  
  <p>So the error message points to the advance/lie methods under Player class but I have no idea what I've done wrong. Also please feel free to point out other blunders. Thanks so much in advance.</p>