Class: ScoreSheet

Inherits:
Object
  • Object
show all
Includes:
Scoring
Defined in:
lib/scoresheet.rb

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

Instance Method Summary collapse

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.

Parameters:

  • custom_dice (Array<Fixnum>) (defaults to: Array.new(5) {Dice.new.instance_eval "new_dice"})

    custom dice for testing



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

#diceDice (readonly)

Returns:



12
13
14
# File 'lib/scoresheet.rb', line 12

def dice
  @dice
end

#num_yahtzeesFixnum (readonly)

Returns counter for number of yahtzees scored in the game.

Returns:

  • (Fixnum)

    counter for number of yahtzees scored in the game



13
14
15
# File 'lib/scoresheet.rb', line 13

def num_yahtzees
  @num_yahtzees
end

#sheetHash (readonly)

Returns table of two element arrays where the first value is the score and the second is whether the field has been played.

Returns:

  • (Hash)

    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.

Parameters:

  • field (Symbol)

Raises:

  • ArgumentError



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

Returns:

  • (true)

    if the score sheet is completely filled and no legal moves remain

  • (false)

    if the score sheet is not completely filled and there are still legal moves to be made



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_totalInteger

Returns The total score of the lower part of the ScoreSheet.

Returns:

  • (Integer)

    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_upperFixnum

Returns:

  • (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_sObject 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

#totalInteger

Returns the grand total.

Returns:

  • (Integer)

    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_bonusFixnum

Checks if upper score bonus can be awarded

Returns:

  • (Fixnum)

    0 if raw_upper < 63

  • (Fixnum)

    35 if raw_upper >= 63



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_totalFixnum

Returns the total score of the upper part of the ScoreSheet, including bonuses.

Returns:

  • (Fixnum)

    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

#yahtzeeFixnum

Checks to see if you have all the of the same dice

Returns:

  • (Fixnum)


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