2016-08-09 13 views
-1

このエラーが発生しましたが、どこに終了する必要があるのか​​わかりません。構文エラー、予期しない入力の終了、expecting keyword_end(help me)!

test/test1:54: class definition in method body 
test/test1:79: syntax error, unexpected end-of-input, expecting keyword_end 



class Code 
    attr_reader :pegs 

    def initialize(pegs) 
    @@pegs_key = { "R" => :red,"G" => :green,"B" => :blue,"Y" => :yellow, 
     "O" => :orange,"P" => :purple } 
    @pegs = pegs 
    end 

    class << self 
    def random 
     rand_pegs_set = [] 
     4.times { rand_pegs_set << @@pegs_Key.values.sample } 
     Code.new(rand_pegs_set) 
    end 

    def parse(str) 
     str = string.split("") 
     pegs_set = [] 
     str.each do |letter| 
     if @@pegs_key.has_key?(letter.upcase) 
      pegs_set << @@pegs_key[letter.upcase] 
     else 
      raise "Incorrect colors" 
     end 
     end 
     Code.new(pegs_set) 
    end 
    end 

    def exact_matches(guess) 
    matches = 0 
    pegs_set.each_with_index { |color,i| matches += 1 if self[i] == color } 
    matches 
    end 

    def color_counter 
    counter = Hash.new(0) 
    @pegs.each { |color| counter[color] += 1 } 
    counter 
    end 

    def near_matches(guess) 
     matches = 0 
     code_counter = self.color_counter 
     guess.color_counter.each do |color,count| 
     if code_counter.has_key?(color) 
      if code_counter[color] > count ? matches += count : matches += code_counter[color] 
     end 
     matches - self.exact_matches(guess) 
    end 
end 

class Game 
    attr_reader :secret_code 

    def initialize(secret_code = Code.random) 
    @secret_code = secret_code 
    end 

    def play 
    10.times do 
     puts "Enter your guess" 
     guess = Code.parse(gets.chomp) 
     if guess == @secret_code 
     puts "Great Job! you got it" 
     break 
     end 
     show_matches(guess) 
    end 
    end 

    def show_matches(guess) 
    exact_matches = @secret_code.exact_matches(guess) 
    near_matches = @secret_code.near_matches(guess) 
    puts "#{exact_matches} were exactly right" 
    puts "#{near_matches} were nearly a match" 
    end 
end 
+1

は 'guess.color_counter.each do'が終了していないように見えます。また、もし 'code_counter [color]> count?あなたが思っていることはおそらくしません。 – Amadan

+0

"matches - self.exact_matches(guess)"の後に終わりを追加して、最初のエラーはなくなりましたが、test/test1 :79:構文エラー、予期しない入力の終わり、まだそこにあるkeyword_endを期待しています。それ以外のライナーの場合は、どちらか低い方の値を取って「マッチ」に追加したい –

答えて

0

if code_counter[color] > count ? matches += count : matches += code_counter[color] 

を作ることはとても

matches += (code_counter[color] > count ? count : code_counter[color]) 
関連する問題