2016-06-13 3 views
0

그래서 Ciao Ciao라는 보드 게임을 다시 만들려고합니다. 나는 거의 끝나지 않았지만 계속 붙어서 정말로 도움이된다.Ruby beginner : 클래스, 인스턴스 변수, 접근 자, 접근 자

플레이어 클래스

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에 : 지금까지 나는 다음과 같은 세 클래스와 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 사이의 숫자 장애/오류 압연 경우 플레이어를 전진

나는 내가 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 사이의 숫자 실패/오류 압연 경우 E 플레이어 거짓말 정의 방법 +' for nil:NilClass # ./player.rb:15:in 미리 #의 ./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

예제 설정을 확인하십시오. 더 좋든 나쁘 든간에, 초기화되지 않은 인스턴스 변수를 참조 할 때는 항상'nil'을 반환합니다. 따라서 인스턴스 변수를 설정하는 대신에 nil은 어떤 가치와 동일하고 계속 나아 간다. – dodecaphonic

답변

0

이것은 전체적인 문제는 아니지만, 문제입니다.

첫 번째 오류는 나에게 조금 돋보였습니다. player.lie은 롤이 5 또는 6 인 경우에만 호출되도록되어 있지만 롤 메서드를 스텁하여 3을 반환합니다. 그렇지 않습니까?

rollDie 클래스의 방법입니다,하지만 당신은 Game 클래스의 인스턴스 인 @game을 스텁.