Class: ScoreSheet
Overview
Keeps score throughout the game
Constant Summary collapse
- UpperScores =
The fields on the top section of the score sheet
:ones, :twos, :threes, :fours, :fives, :sixes
- LowerScores =
The fields on the bottom section of the score sheet
:three_of_a_kind, :four_of_a_kind, :full_house, :small_straight, :large_straight, :chance, :yahtzee
Instance Attribute Summary collapse
- #dice ⇒ Dice readonly
-
#num_yahtzees ⇒ Fixnum
readonly
Counter for number of yahtzees scored in the game.
-
#sheet ⇒ Hash
readonly
Table of two element arrays where the first value is the score and the second is whether the field has been played.
Instance Method Summary collapse
- #enter_score(field) ⇒ void
- #filled? ⇒ true, false
-
#initialize(custom_dice = Array.new(5) {Dice.new.instance_eval "new_dice"}) ⇒ ScoreSheet
constructor
A new instance of ScoreSheet.
-
#lower_score_total ⇒ Integer
The total score of the lower part of the ScoreSheet.
- #raw_upper ⇒ Fixnum
-
#to_s ⇒ Object
(also: #display)
@todo Find a less complex way to create final string @return [String].
-
#total ⇒ Integer
The grand total.
-
#upper_score_bonus ⇒ Fixnum
Checks if upper score bonus can be awarded.
-
#upper_score_total ⇒ Fixnum
The total score of the upper part of the ScoreSheet, including bonuses.
-
#yahtzee ⇒ Fixnum
Checks to see if you have all the of the same dice.
Methods included from Scoring
#chance, #fives, #four_of_a_kind, #fours, #full_house, #large_straight, #ones, #sixes, #small_straight, #three_of_a_kind, #threes, #twos
Constructor Details
#initialize(custom_dice = Array.new(5) {Dice.new.instance_eval "new_dice"}) ⇒ ScoreSheet
Returns a new instance of ScoreSheet.
17 18 19 20 |
# File 'lib/scoresheet.rb', line 17 def initialize(custom_dice=Array.new(5) {Dice.new.instance_eval "new_dice"}) @sheet, @dice, @num_yahtzees = Hash.new, Dice.new(custom_dice), 0 Array.new(UpperScores).concat(LowerScores).each {|s| @sheet[s] = [0, false]} end |
Instance Attribute Details
#num_yahtzees ⇒ Fixnum (readonly)
Returns counter for number of yahtzees scored in the game.
13 14 15 |
# File 'lib/scoresheet.rb', line 13 def num_yahtzees @num_yahtzees end |
#sheet ⇒ Hash (readonly)
Returns table of two element arrays where the first value is the score and the second is whether the field has been played.
11 12 13 |
# File 'lib/scoresheet.rb', line 11 def sheet @sheet end |
Instance Method Details
#enter_score(field) ⇒ void
This method returns an undefined value.
27 28 29 30 31 32 33 34 35 |
# File 'lib/scoresheet.rb', line 27 def enter_score(field) if field == :yahtzee && ((available :yahtzee) || @num_yahtzees > 0) @sheet[field] = yahtzee, true elsif available field @sheet[field] = send(field, @dice.values), true else raise ArgumentError, "Score already entered." end end |
#filled? ⇒ true, false
41 42 43 |
# File 'lib/scoresheet.rb', line 41 def filled? @sheet.collect{|k,v| v[1]}.reduce{|r,e| r && e} end |
#lower_score_total ⇒ Integer
Returns The total score of the lower part of the ScoreSheet.
52 53 54 |
# File 'lib/scoresheet.rb', line 52 def lower_score_total # @return [Integer] The total score of the lower part of the ScoreSheet @sheet.select{|x| LowerScores.include? x }.collect{|k,v| v[0]}.reduce :+ end |
#raw_upper ⇒ Fixnum
45 46 47 |
# File 'lib/scoresheet.rb', line 45 def raw_upper # @return [Fixnum] @sheet.select{|x| UpperScores.include? x }.collect{|k,v| v[0]}.reduce :+ end |
#to_s ⇒ Object Also known as: display
@todo Find a less complex way to create final string @return [String]
90 91 92 93 94 95 96 97 98 99 |
# File 'lib/scoresheet.rb', line 90 def to_s ss = String.new ss += %Q( S C O R E S H E E T ).center(80, ?=) + "\n\n" (0..(UpperScores.length - 1)).each { |i| ss += print_score_sheet_line(i) } ss += bonus_yahtzee_line ss += "\n\n" ss += "Total Score: #{total}".center(80) + ?\n ss += (?= * 80) + ?\n return ss end |
#total ⇒ Integer
Returns the grand total.
55 56 57 |
# File 'lib/scoresheet.rb', line 55 def total # @return [Integer] the grand total lower_score_total + upper_score_total end |
#upper_score_bonus ⇒ Fixnum
Checks if upper score bonus can be awarded
66 67 68 69 70 71 |
# File 'lib/scoresheet.rb', line 66 def upper_score_bonus if raw_upper >= 63 then 35 else 0 end end |
#upper_score_total ⇒ Fixnum
Returns the total score of the upper part of the ScoreSheet, including bonuses.
49 50 51 |
# File 'lib/scoresheet.rb', line 49 def upper_score_total # @return [Fixnum] the total score of the upper part of the ScoreSheet, including bonuses raw_upper + upper_score_bonus end |
#yahtzee ⇒ Fixnum
Checks to see if you have all the of the same dice
76 77 78 79 80 81 82 83 |
# File 'lib/scoresheet.rb', line 76 def yahtzee if dice.values.all? {|x| x == dice.values[0]} @num_yahtzees += 1 return sheet[:yahtzee][0] + 50 * 2 ** (@num_yahtzees - 1) else return 0 end end |