Module: Yahtzee::Scoring::LowerCard

Included in:
Yahtzee::Scoring
Defined in:
lib/scoring/lower_card.rb

Class Method Summary collapse

Class Method Details

.dup_counts_as_hash(dice) ⇒ Object



53
54
55
# File 'lib/scoring/lower_card.rb', line 53

def dup_counts_as_hash(dice)
  dice.each_with_object(Hash.new(0)) {|o, h| h[o] += 1 }
end

.max_dupe_count_as_hash(dice) ⇒ Object



57
58
59
# File 'lib/scoring/lower_card.rb', line 57

def max_dupe_count_as_hash(dice)
  dup_counts_as_hash(dice).max_by {|_,v| v }
end

.score_bonus_yahtzee(dice) ⇒ Object Also known as: score_bonus_yahtzee_1, score_bonus_yahtzee_2, score_bonus_yahtzee_3



41
42
43
# File 'lib/scoring/lower_card.rb', line 41

def score_bonus_yahtzee(dice)
  dice.uniq.count == 1 ? 100 : 0
end

.score_chance(dice) ⇒ Object



7
8
9
# File 'lib/scoring/lower_card.rb', line 7

def score_chance(dice)
  dice.reduce(:+)
end

.score_four_of_a_kind(dice) ⇒ Object



33
34
35
# File 'lib/scoring/lower_card.rb', line 33

def score_four_of_a_kind(dice)
  score_min_of_a_kind(dice, 4)
end

.score_full_house(dice) ⇒ Object



11
12
13
14
15
# File 'lib/scoring/lower_card.rb', line 11

def score_full_house(dice)
  sorted = dice.sort
  (sorted.count(sorted.first) + 
   sorted.count(sorted.last) == 5) ? 25 : 0
end

.score_large_straight(dice) ⇒ Object



23
24
25
26
27
# File 'lib/scoring/lower_card.rb', line 23

def score_large_straight(dice)
  dice.each_cons(5).any? do |a,b,c,d,e| 
    a+1==b && b+1==c && c+1==d && d+1==e
  end ? 40 : 0
end

.score_min_of_a_kind(dice, min) ⇒ Object



48
49
50
51
# File 'lib/scoring/lower_card.rb', line 48

def score_min_of_a_kind(dice, min)
  value = max_dupe_count_as_hash(dice)
  value[1] >= min ? value.reduce(:*) : 0
end

.score_small_straight(dice) ⇒ Object



17
18
19
20
21
# File 'lib/scoring/lower_card.rb', line 17

def score_small_straight(dice)
  dice.each_cons(4).any? do |a,b,c,d| 
    a+1==b && b+1==c && c+1==d
  end ? 30 : 0
end

.score_three_of_a_kind(dice) ⇒ Object



29
30
31
# File 'lib/scoring/lower_card.rb', line 29

def score_three_of_a_kind(dice)
  score_min_of_a_kind(dice, 3)
end

.score_yahtzee(dice) ⇒ Object



37
38
39
# File 'lib/scoring/lower_card.rb', line 37

def score_yahtzee(dice)
  dice.uniq.count == 1 ? 50 : 0
end