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



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

def chance(dice)
	dice.reduce :+
end

#fives(d) ⇒ Fixnum

Returns the score.

Parameters:

  • d (Array<Fixnum>)

    the dice to be tested

Returns:

  • (Fixnum)

    the score



69
70
71
# File 'lib/scoring.rb', line 69

def fives(d)
	single_face d, 5
end

#four_of_a_kind(dice) ⇒ Fixnum

Checks to see if you have 4 of the same dice

Parameters:

  • dice (Array<Fixnum>)

    The dice to be tested

Returns:

  • (Fixnum)

    0 if <= 4 indices have the same value

  • (Fixnum)

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

See Also:

  • (three_of_a_kind)


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

def four_of_a_kind(dice)
	of_a_kind dice, 4
end

#fours(d) ⇒ Fixnum

Returns the score.

Parameters:

  • d (Array<Fixnum>)

    the dice to be tested

Returns:

  • (Fixnum)

    the score



61
62
63
# File 'lib/scoring.rb', line 61

def fours(d)
	single_face d, 4
end

#full_house(dice) ⇒ Fixnum

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

Parameters:

  • dice (Array<Fixnum>)

    The dice to be tested

Returns:

  • (Fixnum)

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

  • (Fixnum)

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



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

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:

  • (small_straight)


116
117
118
# File 'lib/scoring.rb', line 116

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

#ones(d) ⇒ Fixnum

Returns the total of all the ones.

Parameters:

  • d (Array<Fixnum>)

    the dice to be tested

Returns:

  • (Fixnum)

    the total of all the ones



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

def ones(d)
	single_face d, 1
end

#sixes(d) ⇒ Fixnum

Returns the score.

Parameters:

  • d (Array<Fixnum>)

    the dice to be tested

Returns:

  • (Fixnum)

    the score



77
78
79
# File 'lib/scoring.rb', line 77

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:

  • (large_straight)


105
106
107
# File 'lib/scoring.rb', line 105

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)


87
88
89
# File 'lib/scoring.rb', line 87

def three_of_a_kind(dice)
	of_a_kind dice, 3
end

#threes(d) ⇒ Fixnum

Returns the score.

Parameters:

  • d (Array<Fixnum>)

    the dice to be tested

Returns:

  • (Fixnum)

    the score



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

def threes(d)
	single_face d, 3
end

#twos(d) ⇒ Fixnum

Returns the total of all the twos.

Parameters:

  • d (Array<Fixnum>)

    the dice to be tested

Returns:

  • (Fixnum)

    the total of all the twos



45
46
47
# File 'lib/scoring.rb', line 45

def twos(d)
	single_face d, 2
end