public
Created

Refactor ruby koan 182

  • Download Gist
refactor-ruby-koan-182.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
# http://stackoverflow.com/questions/6738715/ruby-koans-182-refactor-help
 
def old_score(dice)
rollGreedRoll = Hash.new
rollRollCount = Hash.new
(1..6).each do |roll|
rollGreedRoll[roll] = roll == 1 ? GreedRoll.new(1000, 100) :
GreedRoll.new( 100 * roll, roll == 5 ? 50 : 0)
rollRollCount[roll] = dice.count { |a| a == roll }
end
 
score =0
 
rollRollCount.each_pair do |roll, rollCount|
gr = rollGreedRoll[roll]
if rollCount < 3
score += rollCount * gr.individualPoints
else
score += gr.triplePoints + ((rollCount - 3) * gr.individualPoints)
 
end
end
 
return score
end
 
class GreedRoll
attr_accessor :triplePoints
attr_accessor :individualPoints
 
def initialize(triplePoints, individualPoints)
@triplePoints = triplePoints
@individualPoints = individualPoints
end
end
 
def refactor1(dice)
# This seems backwards but it makes things clearer later.
# Lets temporarily seperate the rollGreedRoll
# and rollRollCount init loops.
rollGreedRoll = Hash.new
(1..6).each do |roll|
rollGreedRoll[roll] = roll == 1 ? GreedRoll.new(1000, 100) :
GreedRoll.new( 100 * roll, roll == 5 ? 50 : 0)
end
 
rollRollCount = Hash.new
(1..6).each do |roll|
rollRollCount[roll] = dice.count { |a| a == roll }
end
 
score =0
 
rollRollCount.each_pair do |roll, rollCount|
gr = rollGreedRoll[roll]
if rollCount < 3
score += rollCount * gr.individualPoints
else
score += gr.triplePoints + ((rollCount - 3) * gr.individualPoints)
 
end
end
 
return score
end
 
def refactor2(dice)
# The rollRollCount map is being used to provide (1..6)
# and the corresponding count. We can just compute the count
# inside a (1..6).each so lets remove it.
# Also introduce dice.count(obj)
rollGreedRoll = Hash.new
(1..6).each do |roll|
rollGreedRoll[roll] = roll == 1 ? GreedRoll.new(1000, 100) :
GreedRoll.new( 100 * roll, roll == 5 ? 50 : 0)
end
 
score =0
 
(1..6).each do |roll|
rollCount = dice.count(roll)
gr = rollGreedRoll[roll]
if rollCount < 3
score += rollCount * gr.individualPoints
else
score += gr.triplePoints + ((rollCount - 3) * gr.individualPoints)
 
end
end
 
return score
end
 
def refactor3(dice)
# The rollGreedRoll map is only being used in one place
# within a similiar loop. Lets remove it.
score =0
 
(1..6).each do |roll|
rollCount = dice.count(roll)
gr = roll == 1 ? GreedRoll.new(1000, 100) :
GreedRoll.new( 100 * roll, roll == 5 ? 50 : 0)
if rollCount < 3
score += rollCount * gr.individualPoints
else
score += gr.triplePoints + ((rollCount - 3) * gr.individualPoints)
 
end
end
 
return score
end
 
def refactor4(dice)
# there are two places score gets added to
# we can reduce that to one useing % and /
score =0
 
(1..6).each do |roll|
rollCount = dice.count(roll)
gr = roll == 1 ? GreedRoll.new(1000, 100) :
GreedRoll.new( 100 * roll, roll == 5 ? 50 : 0)
score += gr.triplePoints * (rollCount / 3) + gr.individualPoints * (rollCount % 3)
end
 
return score
end
 
def refactor5(dice)
# notice a summation loop
# we can replace that with a collect + sum/reduce
(1..6).collect do |roll|
rollCount = dice.count(roll)
gr = roll == 1 ? GreedRoll.new(1000, 100) :
GreedRoll.new( 100 * roll, roll == 5 ? 50 : 0)
gr.triplePoints * (rollCount / 3) + gr.individualPoints * (rollCount % 3)
end.reduce(0) {|sum, score| sum + score}
end
 
def refactor6(dice)
# the GreedRoll cases seem complicated
# so lets break that apart
(1..6).collect do |roll|
rollCount = dice.count(roll)
gr = case roll
when 1 : GreedRoll.new(1000, 100)
when 5 : GreedRoll.new(500, 50)
else GreedRoll.new(100 * roll, 0)
end
gr.triplePoints * (rollCount / 3) + gr.individualPoints * (rollCount % 3)
end.reduce(0) {|sum, score| sum + score}
end
 
def refactor7(dice)
# the GreedRoll class is just providing two accessors
# so let get rid of it
(1..6).collect do |roll|
rollCount = dice.count(roll)
triplePoints, individualPoints = case roll
when 1 : [1000, 100]
when 5 : [500, 50]
else [100 * roll, 0]
end
triplePoints * (rollCount / 3) + individualPoints * (rollCount % 3)
end.reduce(0) {|sum, score| sum + score}
end
 
def score(dice)
# lets remove the triple and individual points
# as they are only used once
# and lets redo naming using _ vs camelCase
(1..6).collect do |roll|
roll_count = dice.count(roll)
case roll
when 1 : 1000 * (roll_count / 3) + 100 * (roll_count % 3)
when 5 : 500 * (roll_count / 3) + 50 * (roll_count % 3)
else 100 * roll * (roll_count / 3)
end
end.reduce(0) {|sum, n| sum + n}
end
 
# At this point you could try to recollapse the scoring function
# or collapse the 5 and default cases, but I think its clearer
# what is happening this way

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.