Last active
August 29, 2015 14:14
-
-
Save cromwellryan/9a0a76acdf8f7d468fb1 to your computer and use it in GitHub Desktop.
@kaseybon Greed Kata Ideas for Classes/Objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# My approach for the greed game was to take the values in a roll and sort them into seperate arrays. For example all the ones would be in an array, twos in an array, etc.. To keep these neat and tidy I will organize them into a hash. | |
# I could create a class called dice with two methods, roll and score: | |
#The roll method will randomly generate the numbers for the roll (I don't think this project included that but I'll add it anyways) and them store them into their arrays of similar numbers. | |
# The score method would be responsible for taking the sorted arrays and calculating the score then return the score. | |
class Dice | |
def initialize | |
@roll = {1 => [], 2 => [], 3 => [], 4 => [], 5 => [], 6 => []} | |
@points = 0 | |
end | |
def roll(count) | |
dice = [] | |
# Generate Values | |
dice = (1..count).map { |_| Random.rand(1..6) } | |
dice.each do |die| | |
@roll[die] << die | |
end | |
# Output roll to user | |
puts dice.join(", ") | |
end | |
def score | |
# Split the hash | |
remain = @roll.reject{ |k| k == 1 || k == 5 } | |
special = @roll.reject{ |k| k == 2 || k == 3 || k == 4 || k == 6 } | |
# Handle the specials | |
special.each do |key, array| | |
if key == 1 | |
calculate_specials(key, array, 1000, 100) | |
elsif key == 5 | |
calculate_specials(key, array, 500, 50) | |
end | |
end | |
# Handle the remaining | |
remain.each do |key, array| | |
@points = @points + (100 * key) if array.length >= 3 | |
end | |
puts "#{@points}" | |
end | |
def calculate_specials(value, nums = [], high_score, low_score) | |
if nums.length >= 4 | |
@points = @points + high_score | |
leftovers = nums.length - 3 | |
@points = @points + (low_score * leftovers) | |
elsif nums.length == 3 | |
@points = @points + high_score | |
else | |
@points = @points + (low_score * nums.length) | |
end | |
@points | |
end | |
end | |
# Just testing to make sure everything works... | |
test = Dice.new | |
testb = Dice.new | |
test.roll(5) | |
testb.roll(5) | |
test.score | |
testb.score |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is great stuff! Some things that stick out that I'm really excited to see:
reject
. So glad you're looking for and finding some of these idiomatic ruby functions.join
.calculate_specials
Some questions to consider:
calculate_special
only returned what should be added to the overall score?test.roll(5)
as line 74?