Showing posts with label kata. Show all posts
Showing posts with label kata. Show all posts

Sunday, August 7, 2011

Ruby Koan for Dice Scoring (Greed) - Solution #2

It's interesting what some sleep and exercise can do.  The next day while approaching the "big hill" part of my run I came up with a new solution to the Greed Ruby Koan.  This solution is shorter and is more Rubyesque (I think). It also includes a reasonable amount of error checking (nil parameter and valid dice values).  Again, I'd love to hear from Ruby enthusiasts to comment on style and other approaches.

def score(dice)

  return 0 if dice == nil

  score  = 0
  counts = [0, 0, 0, 0, 0, 0]
  dice.each { |roll| counts[roll-1] += 1 if (1..6) === roll } # Count valid rolls

  (1..6).each { |i| score += (i == 1) ? 1000 : i * 100 if counts[i-1] >= 3 } # Score triples

  # Score the rest
  score += counts[0] % 3 * 100
  score += counts[4] % 3 * 50

  return score
end

Friday, August 5, 2011

Ruby Koan for Dice Scoring (Greed) - Solution #1

Recently I've started teaching myself Ruby and as part of my quest I discovered the amazing Ruby Koan site by EdgeCase.  The concept of a koan dates back to the foundations of Zen Buddhism.  You might recognize the question "what is the sound of one hand clapping".  This is actually part of a Zen koan.  A koan is a question or story that serves as a fundamental teaching tool.  A traditional Zen koan cannot be understood via rational thought but rather through intuition leading to an "Aha!" moment.

Ruby Koans, as developed by EdgeCase, consist of 274 test cases and incomplete Ruby code.  Your task is to implement the code in order to make all the tests pass in classic TDD style.  For those unfamiliar with the term, TDD means Test Driven Development.  TDD follows the pattern of red, green, refactor.  When implementing a new section of code you start with its tests which will all fail (be 'red') initially.  Then you implement the code turning all of the tests green.  Finally, and over time, you go forth with confidence and refactor the code to higher and higher levels of quality (keeping it green).

Many of the Ruby Koan tests are fairly simple to make pass.  Some of the more esoteric Ruby features were more difficult to master (I thought Array.inject and blocks/yield would drive me to murder).  However, one of the most interesting koan related to scoring dice throws in a fictional game called Greed.  This koan is quite different than the others and is more like a Kata (a martial arts term referring to a repetitive exercise meant to hone skill -- I'll save Kata for another post).  Briefly, the koan asks you to implement a class that conforms to the following requirements:

# A greed roll is scored as follows [for a roll of up to 5 dice]:
#
# * 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

This particular koan consists of 13 (initially failing) tests.  Your implementation of the scoring method must make them all pass.  Ruby, by its nature, provides a multitude of ways (more than any other language I think) to solve this problem.  What follows is my first attempt.  It should be noted that error checking was excluded from the requirements nor did any of the tests include input error tests (like dice rolls outside 1..6, strings, etc.).

As a student of Ruby I am keen for feedback (particularly from the Ruby masters) as this solution seems a little... wordy.

def score(dice)
  score = 0
  sorted_dice = dice.sort # Sort the rolls

  while sorted_dice.length > 0
    val = sorted_dice.shift
    if sorted_dice.length >= 2 && val == sorted_dice[0] && val == sorted_dice[1]  # a triple!
      score += val == 1 ? 1000 : val * 100
      sorted_dice.shift(2)
    else
      score += 100 if val == 1
      score += 50  if val == 5
    end
  end

  return score
end