Module: Scoring

Included in:
ScoreSheet
Defined in:
lib/scoring.rb

Overview

methods for calculating score

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 Method Summary collapse

Instance Method Details

#chance(dice) ⇒ Fixnum

Returns the sum of all the dice.

Parameters:

  • dice (Array<Fixnum>)

    the dice to be evaluated

Returns:

  • (Fixnum)

    the sum of all the dice



73
74
75
# File 'lib/scoring.rb', line 73

def chance(dice)
	dice.reduce :+
end

#fives(d) ⇒ Object

See Also:

  • Scoring.((#ones)


51
52
53
# File 'lib/scoring.rb', line 51

def fives(d)	# @see (#ones)
	single_face d, 5
end

#four_of_a_kind(dice) ⇒ Fixnum

Checks to see if you have 4 of the same dice

Returns:

  • (Fixnum)

    0 if <= 4 indices have the same value

  • (Fixnum)

    dice.reduce(:+) if >= 4 indices have the same value

See Also:

  • Scoring.((#three_of_a_kind)


27
28
29
# File 'lib/scoring.rb', line 27

def four_of_a_kind(dice)
	of_a_kind dice, 4
end

#fours(d) ⇒ Object

See Also:

  • Scoring.((#ones)


47
48
49
# File 'lib/scoring.rb', line 47

def fours(d)	# @see (#ones)
	single_face d, 4
end

#full_house(dice) ⇒ Fixnum

Checks to see if you have 3 of one kind of dice and 2 of another

Returns:

  • (Fixnum)

    25 if @dice.dice contains 3 of one Fixnum and 2 of another

  • (Fixnum)

    0 if @dice.dice does not contain 3 of one Fixnum and 2 of another



14
15
16
17
18
19
# File 'lib/scoring.rb', line 14

def full_house(dice)
	f_table = freq dice
	if (f_table.length == 2 && f_table.has_value?(3)) || f_table.length == 1 then return 25			
	else; return 0
	end
end

#large_straight(dice) ⇒ Fixnum Also known as: LS

Parameters:

  • dice (Array<Fixnum>)

    the dice to be tested

Returns:

  • (Fixnum)

    40 if there are 4 consecutive Fixnums in dice

  • (Fixnum)

    0 if there are not three conscecutive Fixnums in dice

See Also:

  • Scoring.((#small_straight)


94
95
96
# File 'lib/scoring.rb', line 94

def large_straight(dice)
	straight dice, 5, 40
end

#ones(d) ⇒ Fixnum

Returns the score.

Parameters:

  • d (Array<Fixnum>)

    the dice to be tested

Returns:

  • (Fixnum)

    the score



35
36
37
# File 'lib/scoring.rb', line 35

def ones(d)
	single_face d, 1
end

#sixes(d) ⇒ Object

See Also:

  • Scoring.((#ones)


55
56
57
# File 'lib/scoring.rb', line 55

def sixes(d)	# @see (#ones)
	single_face d, 6
end

#small_straight(dice) ⇒ Fixnum Also known as: SS

Parameters:

  • dice (Array<Fixnum>)

    the dice to be tested

Returns:

  • (Fixnum)

    30 if there are 3 consecutive Fixnums in dice

  • (Fixnum)

    0 if there are not three conscecutive Fixnums in dice

See Also:

  • Scoring.((#large_straight)


83
84
85
# File 'lib/scoring.rb', line 83

def small_straight(dice)
	straight dice, 4, 30
end

#three_of_a_kind(dice) ⇒ Fixnum

Checks to see if you have 3 of the same dice

Returns:

  • (Fixnum)

    dice.reduce(:+) if there is <= 3 of the same value

  • (Fixnum)

    0 if you do not have a three of a kind

See Also:

  • Scoring.((#four_of_a_kind)


65
66
67
# File 'lib/scoring.rb', line 65

def three_of_a_kind(dice)
	of_a_kind dice, 3
end

#threes(d) ⇒ Object

See Also:

  • Scoring.((#ones)


43
44
45
# File 'lib/scoring.rb', line 43

def threes(d)	# @see (#ones)
	single_face d, 3
end

#twos(d) ⇒ Object

See Also:

  • Scoring.((#ones)


39
40
41
# File 'lib/scoring.rb', line 39

def twos(d)	# @see (#ones)
	single_face d, 2
end