Skip to content

Commit

Permalink
Fix: Ensure only scoring dice are removed between turns
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael de Silva committed Oct 6, 2015
1 parent 289d59a commit 3544913
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 16 deletions.
38 changes: 22 additions & 16 deletions koans/greed/lib/greed.rb
@@ -1,6 +1,15 @@
require File.expand_path(File.dirname(__FILE__) + '/greed/dice_set')
require File.expand_path(File.dirname(__FILE__) + '/greed/scorable')

# Playing Greed
#
# * Each player takes a turn consisting of one or more rolls of the dice.
#
# * After a player rolls and the score is calculated, the scoring dice are
# removed and the player has the option of rolling again using only the
# non-scoring dice.
#
#
module Greed

class Game
Expand Down Expand Up @@ -72,29 +81,26 @@ def take_turn(round, dice, player, index)
set.roll(dice)

player_scoring = Scorable.new(set.values)
remaining_dice = dice - player_scoring.non_scoring.size

if player_scoring.score > 0
return {
round: round,
score: player_scoring.score,
roll: set.values,
non_scoring_dice: player_scoring.non_scoring,
remaining_dice: remaining_dice
}
end

false
remaining_dice = dice - player_scoring.scoring.size

{
round: round,
score: player_scoring.score,
roll: set.values,
scoring_dice: player_scoring.scoring,
non_scoring_dice: player_scoring.non_scoring,
remaining_dice: remaining_dice
}
end

def show_totals
report = ""

report << "\nRound\tPlayer\tScore\n"
report << "----\t------\t-----\n"
report << "\nRound\tPlayer\tScore\tRoll\n"
report << "----\t------\t-----\t----\n"

@turns.each do |turn|
report << "#{turn[1][:round]}\t#{get_player_name(turn[0])}\t#{turn[1][:score]}\n"
report << "#{turn[1][:round]}\t#{get_player_name(turn[0])}\t#{turn[1][:score]}\t#{turn[1][:roll]}\n"
end
report << "\n"

Expand Down
9 changes: 9 additions & 0 deletions koans/greed/lib/greed/scorable.rb
Expand Up @@ -5,12 +5,14 @@ class Scorable
def initialize(dice)
@dice = dice
@non_scoring = []
@scoring = []
end

attr_reader :dice

def score
@non_scoring = []
@scoring = []
tally = 0

counts = Hash.new(0)
Expand All @@ -20,12 +22,15 @@ def score
if counts[i] >= 3
i == 1 ? tally += 1000 : tally += 100 * i
counts[i] = [counts[i] - 3, 0].max
@scoring << i
end

if i == 1
tally += 100 * counts[i]
@scoring << i if counts[i] > 0
elsif i == 5
tally += 50 * counts[i]
@scoring << i if counts[i] > 0
end
end

Expand All @@ -39,6 +44,10 @@ def non_scoring
@non_scoring.flatten
end

def scoring
score if @scoring.empty?
@scoring.flatten.uniq
end
end

end
7 changes: 7 additions & 0 deletions koans/greed/spec/greed/scorable_spec.rb
Expand Up @@ -75,4 +75,11 @@ def test_non_scoring_of_mixed
assert_equal [1, 5], Greed::Scorable.new([1,5,5,1]).non_scoring
end

def test_scoring_of_mixed
assert_equal [2, 5], Greed::Scorable.new([2,5,2,2,3]).scoring
assert_equal [1, 5], Greed::Scorable.new([1,1,1,5,1]).scoring
assert_equal [], Greed::Scorable.new([2,3,4,6]).scoring
assert_equal [1, 5], Greed::Scorable.new([1,5,5,1]).scoring
end

end
1 change: 1 addition & 0 deletions koans/greed/spec/greed_spec.rb
Expand Up @@ -33,6 +33,7 @@ def test_running_a_game_records_turns
def test_running_a_game_and_printing_turn_totals
game = Greed::Game.new(Player.new("mike"), Player.new("bob"))
game.play
game.play

assert_match /Score/, game.turn_totals
end
Expand Down

0 comments on commit 3544913

Please sign in to comment.