<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <id>tag:www.refactormycode.com,2007:users1913</id>
  <link type="application/atom+xml" href="http://www.refactormycode.com/users/1913" rel="self"/>
  <title>websymphony.blogspot.com</title>
  <updated>Tue Feb 02 21:42:28 -0800 2010</updated>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code1165</id>
    <published>2010-02-02T21:42:28-08:00</published>
    <updated>2010-02-08T07:37:41-08:00</updated>
    <title>[Ruby] Looking for other/better solutions to this Greed Dice game</title>
    <content type="html">&lt;p&gt;# Greed is a dice game where you roll up to five dice to accumulate
&lt;br /&gt;# points.  The following &amp;quot;score&amp;quot; function will be used calculate the
&lt;br /&gt;# score of a single roll of the dice.
&lt;br /&gt;#
&lt;br /&gt;# A greed roll is scored as follows:
&lt;br /&gt;#
&lt;br /&gt;# * A set of three ones is 1000 points
&lt;br /&gt;#   
&lt;br /&gt;# * A set of three numbers (other than ones) is worth 100 times the
&lt;br /&gt;#   number. (e.g. three fives is 500 points).
&lt;br /&gt;#
&lt;br /&gt;# * A one (that is not part of a set of three) is worth 100 points.
&lt;br /&gt;#
&lt;br /&gt;# * A five (that is not part of a set of three) is worth 50 points.
&lt;br /&gt;#
&lt;br /&gt;# * Everything else is worth 0 points.
&lt;br /&gt;#
&lt;br /&gt;#
&lt;br /&gt;# Examples:
&lt;br /&gt;#
&lt;br /&gt;# score([1,1,1,5,1]) =&amp;gt; 1150 points
&lt;br /&gt;# score([2,3,4,6,2]) =&amp;gt; 0 points
&lt;br /&gt;# score([3,4,5,3,3]) =&amp;gt; 350 points
&lt;br /&gt;# score([1,5,1,2,4]) =&amp;gt; 250 points&lt;/p&gt;

&lt;pre&gt;def score(dice)
  
  count = [0,0,0,0,0,0]
  
  dice.each { |i|
    count[0] += 1 if i == 1
    count[1] += 1 if i == 2
    count[2] += 1 if i == 3
    count[3] += 1 if i == 4
    count[4] += 1 if i == 5
    count[5] += 1 if i == 6
  }

  sum = 0

  unless dice  == []

    if count[0] &amp;lt; 3
      sum += count[0] * 100
    elsif count[0] == 3
      sum += 1000
    elsif count[0] &amp;gt; 3
      sum  = 1000 + (count[0] -3) * 100
    end

    (1..5).each { |i|
     if count[i] == 3
      sum +=  (i+1) * 100
     end
    }

    if count[4] &amp;lt; 3
      sum += count[4] * 50
    elsif count[4] &amp;gt; 3
      sum  = 500 + (count[4] -3) * 50
    end
    
  end

  return sum

end&lt;/pre&gt;</content>
    <author>
      <name>websymphony.blogspot.com</name>
      <email>amitgaur84@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1165-looking-for-other-better-solutions-to-this-greed-dice-game" rel="alternate"/>
  </entry>
</feed>

