0

I'm doing the edgecase koans to learn ruby and I got stuck with the greed koan (182-183) getting a mysterious error. The rules are outlined HERE

I know my code is..unimpressive, I figured I'd refactor it once my logic was sound (which it probably is not).

I appreciate any help.

def score(dice)
  score = 0
  if dice == []
    return score
  end

  dice = dice.sort
  dice = [1,1,4,5,6]
  count = [0,0,0,0,0,0]
  score = 0
  dice.each do |face|
    if    face == 1
        count[0]++
    elsif face == 2 # this is line 45 with reported error
        count[1]++
    elsif face == 3
        count[2]++
    elsif face == 4
        count[3]++
    elsif face == 5
        count[4]++
    elsif face == 6
        count[5]++
    end
  end
  if count[0] >= 3
    score+= 1000
    count[0] = count[0] - 3
  elsif count[4] >= 3
    score+= 500
    count[4] = count[4] - 3
  end
  score+= count[0] * 100
  count [0] = 0
  score+= count[4] * 50
  count [4] = 0

  if count[1] >= 3
    score+= 200
  elsif count[2] >= 3
    score+= 300
  elsif count[3] >= 3
    score+= 400
  elsif count[5] >= 3
    score+= 600
  end

  #check if there are three 1 at the beginning
  #if not, check if we have three 2
  # You need to write this method
end

For info, I am getting this error:

 /Users/gozulin/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': /Users/gozulin/Sites/ruby_koans/koans/about_scoring_project.rb:45: syntax error, unexpected keyword_elsif (SyntaxError)
    elsif face == 2
         ^
/Users/gozulin/Sites/ruby_koans/koans/about_scoring_project.rb:47: syntax error, unexpected keyword_elsif
    elsif face == 3
         ^
/Users/gozulin/Sites/ruby_koans/koans/about_scoring_project.rb:49: syntax error, unexpected keyword_elsif
    elsif face == 4
         ^
/Users/gozulin/Sites/ruby_koans/koans/about_scoring_project.rb:51: syntax error, unexpected keyword_elsif
    elsif face == 5
         ^
/Users/gozulin/Sites/ruby_koans/koans/about_scoring_project.rb:53: syntax error, unexpected keyword_elsif
    elsif face == 6
         ^
/Users/gozulin/Sites/ruby_koans/koans/about_scoring_project.rb:55: syntax error, unexpected keyword_end
/Users/gozulin/Sites/ruby_koans/koans/about_scoring_project.rb:84: class definition in method body
/Users/gozulin/Sites/ruby_koans/koans/about_scoring_project.rb:122: syntax error, unexpected $end, expecting keyword_end
    from /Users/gozulin/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
    from path_to_enlightenment.rb:24:in `<main>'
rake aborted!
Command failed with status (1): [/Users/gozulin/.rvm/rubies/ruby-1.9.2-p290...]
Community
  • 1
  • 1
gozu
  • 159
  • 5

2 Answers2

5

Ruby doesn't support C style incrementation: ++.

Use count[0] += 1

If you have some kind of "mysterious" error, you should also look not where interpreter pointed you, but also one line above.

Ernest
  • 8,701
  • 5
  • 40
  • 51
0

The issue is not with elif although the error message sounds like that but with the token before:

count[0]++

is no allowed syntax in ruby.

Howard
  • 38,639
  • 9
  • 64
  • 83