Skip to content

Instantly share code, notes, and snippets.

Created February 2, 2012 20:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/1725633 to your computer and use it in GitHub Desktop.
Save anonymous/1725633 to your computer and use it in GitHub Desktop.
Unit tests for the Greed Ruby Koan
require 'test/unit'
# Greed is a dice game where you roll up to six 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.
#
# * A set of four numbers is worth the triple score multiply by 2.
#
# * A set of five numbers is worth the triple score multiply by 4.
#
# * A set of six numbers is worth the triple score multiply by 8.
#
# * A set of three pairs is worth 800 points
#
# * A straight is worth 1200 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
# score([2,2,2,2,6]) => 400 points
# score([4,4,4,4,4]) => 1600 points
# score([6,6,6,6,6,6]) => 4800 points
# score([1,2,3,4,5,6]) => 1200 points
# score([1,1,3,3,6,6]) => 800 points
#
# More scoring examples are given in the tests below:
#
# Your goal is to write the score method.
class AboutScoringProject < Test::Unit::TestCase
def test_score_of_a_single_roll_of_5_is_50
assert_equal 50, single_double_rule([5])
end
def test_score_of_a_single_roll_of_1_is_100
assert_equal 100, single_double_rule([1])
end
def test_score_of_multiple_1s_and_5s_is_the_sum_of_individual_scores
assert_equal 300, single_double_rule([1,5,5,1])
end
def test_score_of_single_2s_3s_4s_and_6s_are_zero
assert_equal 0, single_double_rule([2,3,4,6])
end
def test_score_of_a_triple_1_is_1000
assert_equal 1000, triple_quadruple_quintuple_sextuple_rule([1,1,1])
end
def test_score_of_other_triples_is_100x
assert_equal 100*2, triple_quadruple_quintuple_sextuple_rule([2,2,2])
assert_equal 100*3, triple_quadruple_quintuple_sextuple_rule([3,3,3])
assert_equal 100*4, triple_quadruple_quintuple_sextuple_rule([4,4,4])
assert_equal 100*5, triple_quadruple_quintuple_sextuple_rule([5,5,5])
assert_equal 100*6, triple_quadruple_quintuple_sextuple_rule([6,6,6])
end
def test_score_of_a_quadruple_1_is_2000
assert_equal 2000, triple_quadruple_quintuple_sextuple_rule([1,1,1,1])
end
def test_score_of_other_quadruple_is_200x
assert_equal 200*2, triple_quadruple_quintuple_sextuple_rule([2,2,2,2])
assert_equal 200*3, triple_quadruple_quintuple_sextuple_rule([3,3,3,3])
assert_equal 200*4, triple_quadruple_quintuple_sextuple_rule([4,4,4,4])
assert_equal 200*5, triple_quadruple_quintuple_sextuple_rule([5,5,5,5])
assert_equal 200*6, triple_quadruple_quintuple_sextuple_rule([6,6,6,6])
end
def test_score_of_a_quintuple_1_is_4000
assert_equal 4000, triple_quadruple_quintuple_sextuple_rule([1,1,1,1,1])
end
def test_score_of_other_quintuple_is_400x
assert_equal 400*2, triple_quadruple_quintuple_sextuple_rule([2,2,2,2,2])
assert_equal 400*3, triple_quadruple_quintuple_sextuple_rule([3,3,3,3,3])
assert_equal 400*4, triple_quadruple_quintuple_sextuple_rule([4,4,4,4,4])
assert_equal 400*5, triple_quadruple_quintuple_sextuple_rule([5,5,5,5,5])
assert_equal 400*6, triple_quadruple_quintuple_sextuple_rule([6,6,6,6,6])
end
def test_score_of_a_sextuple_1_is_8000
assert_equal 8000, triple_quadruple_quintuple_sextuple_rule([1,1,1,1,1,1])
end
def test_score_of_other_sextuple_is_800x
assert_equal 800*2, triple_quadruple_quintuple_sextuple_rule([2,2,2,2,2,2])
assert_equal 800*3, triple_quadruple_quintuple_sextuple_rule([3,3,3,3,3,3])
assert_equal 800*4, triple_quadruple_quintuple_sextuple_rule([4,4,4,4,4,4])
assert_equal 800*5, triple_quadruple_quintuple_sextuple_rule([5,5,5,5,5,5])
assert_equal 800*6, triple_quadruple_quintuple_sextuple_rule([6,6,6,6,6,6])
end
def test_score_of_three_pairs_is_800
assert_equal [800, true], three_pairs_rule([2,2,3,3,6,6])
assert_equal [800, true], three_pairs_rule([1,1,5,5,6,6])
end
def test_score_of_a_straight_is_1200
assert_equal [1200, true], straight_rule([1,2,3,4,5,6])
end
def test_score_of_an_empty_list_is_zero
assert_equal 0, score([])
end
def test_score_of_mixed_is_sum
assert_equal 0, score([2,3,3,4,6,6])
assert_equal 250, score([1,1,5,2,3,4])
assert_equal 500, score([2,2,2,3,3,3])
assert_equal 600, score([5,5,5,1])
assert_equal 1000, score([5,5,5,5,4])
assert_equal 2050, score([1,1,1,5,1])
assert_equal 2450, score([6,6,6,6,6,5])
assert_equal 3200, score([4,4,4,4,4,4])
assert_equal 1200, score([1,2,3,4,5,6])
assert_equal 800, score([1,1,5,5,6,6])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment