2016-06-13 11 views
0

私はCiao Ciaoというボードゲームを再作成しようとしています。私はどこでも終わったわけではありませんが、私は立ち往生し続け、本当に助けに感謝します。Ruby初心者:クラス、インスタンス変数、アクセッサ、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の:これまでのところ私は、次の3つのクラスと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 

障害:@gameを:

1)ゲームは1と4の間の数が 失敗/エラーを圧延する場合はプレイヤーを進め01​​

は、私はRSpecのファイルを実行すると、次のエラーメッセージが出続けます。 NoMethodErrorを果たし: 未定義のメソッド-' for nil:NilClass # ./player.rb:19:in嘘 #play' # ./game_spec.rb:17:in ./game.rb:16:inブロック(2つのレベル) 'はそれぞれblock in play' # ./game.rb:16:in ./game.rb:24:in #'

2)ゲーム "に〜するgame.play NoMethodError:@:5と6の間の数が 失敗/エラーを圧延する場合に電子プレーヤー嘘 未定義のメソッド+' for nil:NilClass # ./player.rb:15:in事前 #1 ./game 'block in play' # ./game.rb:16:in各./game.rb:21:in #'。 rb:16:play' # ./game_spec.rb:23:inのブロック(2レベル) '

エラーメッセージはPlayerクラスのadvance/lieメソッドを指していますが、間違ったことは何もわかりません。また、他の大失敗を指摘してください。事前にありがとうございます。

+1

設定例を見てください。 "@ initial_token"を初期化する代わりに、それを4と比較しています。初期化されていないインスタンス変数を参照すると、それは常に 'nil'を返します - インスタンス変数を設定する代わりに、 nilはある価値と同じで動きます。 – dodecaphonic

答えて

0

これは問題ではありませんが、の問題です。

あなたの最初のエラーは私に少し気を付けました。 player.lieは、ロールが5または6の場合にのみ呼び出されるはずですが、ロールメソッドをスタブして3を返します。

rollDieクラスのメソッドですが、Gameクラスのインスタンスである@gameをスタブしました。