This post is an extension of my last post on the Ruby Koans. Today, I tackled the next Ruby Koan on the block, which was to score dice throws on a simplified version of the game Greed. The first thing I noticed was that the Koans that I got from EdgeCase had a typo in it. It suggested that the following was true:

def test_score_of_mixed_is_sum
    assert_equal 50, score([2,5,2,2,3])
    assert_equal 550, score([5,5,5,5])
  end

That first assert should actually total 250, as the 3 2′s are worth 200 and the 5 is worth 50. I searched the web to make sure that I wasn’t crazy and the version of this file checked in to Google Code by Tim Wingfield agreed with me, so I forged on.

The hardest part of this one for me was figuring out the best way to even work this out in English. I play the Farkle game on Facebook quite a bit, so I was really comfortable with scoring the dice, but I just couldn’t figure out a good step-by-step way to do this. I eventually decided on a Hash to tally up the number of each dice present. This choice was made even easier when I found that Hash had an each_pair method that enumerated over the collection.

After that, I tried my best to implement the logic (this code passes the tests, I don’t promise it is bug-free) and have now put this up for criticism. Last time, I learned some great lessons from Nathan Kelley who shared his code in the comments of my last post. If you have any questions, comments, criticisms, or rebukes of this code, let me know and I’d love to hear them.

I definitely am guilty of writing C# in Ruby and had to undo a lot of code where I started to go down that path, so I welcome comments from Ruby enthusiasts on my code.

require 'edgecase'

# Greed is a dice game where you roll up to five dice to accumulate
# points.  The following "score" function will be used calculate the
# score of a single roll of the dice.
#
# A greed roll is scored as follows:
#
# * A set of three ones is 1000 points
#
# * A set of three numbers (other than ones) is worth 100 times the
#   number. (e.g. three fives is 500 points).
#
# * A one (that is not part of a set of three) is worth 100 points.
#
# * A five (that is not part of a set of three) is worth 50 points.
#
# * Everything else is worth 0 points.
#
#
# Examples:
#
# score([1,1,1,5,1]) => 1150 points
# score([2,3,4,6,2]) => 0 points
# score([3,4,5,3,3]) => 350 points
# score([1,5,1,2,4]) => 250 points
#
# More scoing examples are given in the tests below:
#
# Your goal is to write the score method.

STANDARD_TRIPLET_MULTIPLIER = 100
ONES_TRIPLET_MULTIPLIER = 1000
ONES_VALUE = 100
FIVES_VALUE = 50

def score(dice)
  # You need to write this method
  result = 0
  return result if invalid_dice?(dice)

  dice_sort = {1=>0, 2=>0, 3=>0, 4=>0, 5=>0, 6=>0}

  # Increment the tally, using the die face as the hash key
  # by iterating over the passed in array of dice
  dice.each do | die |
      dice_sort[die] += 1
    end

  # Iterate over the pairs and score them up
  dice_sort.each_pair do |key, value|
      if non_single_score?(key)
        result += key*STANDARD_TRIPLET_MULTIPLIER if value>= 3
      else
        # I LOVE Multiple assignments
        groups_of_three, remainder = value/3, value%3

        result += (ONES_TRIPLET_MULTIPLIER * groups_of_three) + (ONES_VALUE * remainder) if key == 1
        result += (key * STANDARD_TRIPLET_MULTIPLIER * groups_of_three) + (FIVES_VALUE * remainder) if key == 5
      end
  end 

  result
end

def non_single_score?(die)
  return true if die != 1 and die != 5
end

def invalid_dice?(dice)
  return true if dice.length == 0
end

class AboutScoringAssignment < EdgeCase::Koan
  def test_score_of_an_empty_list_is_zero
    assert_equal 0, score([])
  end

  def test_score_of_a_single_roll_of_5_is_50
    assert_equal 50, score([5])
  end

  def test_score_of_a_single_roll_of_1_is_100
    assert_equal 100, score([1])
  end

  def test_score_of_mulitple_1s_and_5s_is_the_sum
    assert_equal 300, score([1,5,5,1])
  end

  def test_score_of_single_2s_3s_4s_and_6s_are_zero
    assert_equal 0, score([2,3,4,6])
  end

  def test_score_of_a_triple_1_is_1000
    assert_equal 1000, score([1,1,1])
  end

  def test_score_of_other_triples_is_100x
    assert_equal 200, score([2,2,2])
    assert_equal 300, score([3,3,3])
    assert_equal 400, score([4,4,4])
    assert_equal 500, score([5,5,5])
    assert_equal 600, score([6,6,6])
  end

  def test_score_of_mixed_is_sum
    assert_equal 250, score([2,5,2,2,3])
    assert_equal 550, score([5,5,5,5])
  end
end

Kick It on DotNetKicks.com

3 Comments

  • 1. Matt Nowack replies at 2nd October 2009 um 1:50 pm :

    I found the same error, here is how I went about solving the scoring exercise. These koans are nice for learning Ruby.

    I had the same idea of using a hash to count them up


    def score(dice)
    result = 0
    counter = {}
    (1..6).each { |x| counter[x] = 0 }
    dice.each { |d| counter[d] += 1 }
    if counter[1] >= 3
    result += 1000
    counter[1] -= 3
    end

    (2..6).each do |x|
    if counter[x] >= 3
    result += (100 * x)
    counter[x] -= 3
    end
    end

    result += (counter[1] * 100)
    result += (counter[5] * 50)
    end

  • 2. Jon Kruger replies at 13th May 2010 um 7:09 am :

    I implemented it in C#, but here is my solution.
    http://github.com/downloads/JonKruger/solid-principles/SWE101%20Greed%20-%20Completed.zip

    I took a different approach when writing the implementation code. I tried to make the code readable so that it would be really easy to figure out what it’s doing. For example, what happens when I get a change to the rules and I (or someone else) has to change my code? How easy is it to figure out what to change?

    I tried to avoid complicated math formulas for this reason. Sure, you can make it work that way in less lines of code, but at the expense of readability.

    I still have to try it in Ruby someday and see what I come up with! I’ll probably end up writing C# in Ruby like you said. :)

  • 3. Mike Murray replies at 19th July 2010 um 8:56 pm :

    Whoa, delete the last comment, horrible formatting…

    Instead of doing a hash with counts, I sorted the dice array and then took length-of-3 subarrays to see if I could find a three-some. On each subarray, I used the array.uniq trick that was suggested in the comments of your last post about the Triangles koan.

    Here’s my code: http://pastie.org/1051380

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <blockquote cite=""> <code> <em> <strong>