mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2025-12-12 12:22:12 +00:00
Add Ruby version of the Yahtzee kata.
Signed-off-by: Lennart Fridén <lennart@devl.se>
This commit is contained in:
parent
e95f2ee409
commit
24d532ed94
@ -6,6 +6,8 @@ The other language translations have been contributed by:
|
|||||||
|
|
||||||
Python: Emily Bache
|
Python: Emily Bache
|
||||||
|
|
||||||
|
Ruby: Kim Persson and Lennart Fridén
|
||||||
|
|
||||||
## Kata: Yahtzee rules
|
## Kata: Yahtzee rules
|
||||||
|
|
||||||
The game of yahtzee is a simple dice game. Each player
|
The game of yahtzee is a simple dice game. Each player
|
||||||
@ -28,7 +30,7 @@ with the category, the player scores zero for the roll.
|
|||||||
For example, suppose a player scores 5,6,5,5,2 in the fives
|
For example, suppose a player scores 5,6,5,5,2 in the fives
|
||||||
category they would score 15 (three fives). The score for
|
category they would score 15 (three fives). The score for
|
||||||
that go is then added to their total and the category cannot
|
that go is then added to their total and the category cannot
|
||||||
be used again in the remaining goes for that game.
|
be used again in the remaining goes for that game.
|
||||||
A full game consists of one go for each category. Thus, for
|
A full game consists of one go for each category. Thus, for
|
||||||
their last go in a game, a player must choose their only
|
their last go in a game, a player must choose their only
|
||||||
remaining category.
|
remaining category.
|
||||||
@ -37,78 +39,78 @@ Your task is to score a GIVEN roll in a GIVEN category.
|
|||||||
You do NOT have to program the random dice rolling.
|
You do NOT have to program the random dice rolling.
|
||||||
The game is NOT played by letting the computer choose the
|
The game is NOT played by letting the computer choose the
|
||||||
highest scoring category for a given roll.
|
highest scoring category for a given roll.
|
||||||
|
|
||||||
|
|
||||||
## Kata: Yahzee Categories and Scoring Rules
|
## Kata: Yahzee Categories and Scoring Rules
|
||||||
|
|
||||||
_Note these rules differ from the original (copyrighted) rules_
|
_Note these rules differ from the original (copyrighted) rules_
|
||||||
|
|
||||||
Chance:
|
Chance:
|
||||||
The player scores the sum of all dice,
|
The player scores the sum of all dice,
|
||||||
no matter what they read.
|
no matter what they read.
|
||||||
For example,
|
For example,
|
||||||
1,1,3,3,6 placed on "chance" scores 14 (1+1+3+3+6)
|
1,1,3,3,6 placed on "chance" scores 14 (1+1+3+3+6)
|
||||||
4,5,5,6,1 placed on "chance" scores 21 (4+5+5+6+1)
|
4,5,5,6,1 placed on "chance" scores 21 (4+5+5+6+1)
|
||||||
|
|
||||||
Yahtzee:
|
Yahtzee:
|
||||||
If all dice have the same number,
|
If all dice have the same number,
|
||||||
the player scores 50 points.
|
the player scores 50 points.
|
||||||
For example,
|
For example,
|
||||||
1,1,1,1,1 placed on "yahtzee" scores 50
|
1,1,1,1,1 placed on "yahtzee" scores 50
|
||||||
1,1,1,2,1 placed on "yahtzee" scores 0
|
1,1,1,2,1 placed on "yahtzee" scores 0
|
||||||
|
|
||||||
Ones, Twos, Threes, Fours, Fives, Sixes:
|
Ones, Twos, Threes, Fours, Fives, Sixes:
|
||||||
The player scores the sum of the dice that reads one,
|
The player scores the sum of the dice that reads one,
|
||||||
two, three, four, five or six, respectively.
|
two, three, four, five or six, respectively.
|
||||||
For example,
|
For example,
|
||||||
1,1,2,4,4 placed on "fours" scores 8 (4+4)
|
1,1,2,4,4 placed on "fours" scores 8 (4+4)
|
||||||
2,3,2,5,1 placed on "twos" scores 4 (2+2)
|
2,3,2,5,1 placed on "twos" scores 4 (2+2)
|
||||||
3,3,3,4,5 placed on "ones" scores 0
|
3,3,3,4,5 placed on "ones" scores 0
|
||||||
|
|
||||||
Pair:
|
Pair:
|
||||||
The player scores the sum of the two highest matching dice.
|
The player scores the sum of the two highest matching dice.
|
||||||
For example, when placed on "pair"
|
For example, when placed on "pair"
|
||||||
3,3,3,4,4 scores 8 (4+4)
|
3,3,3,4,4 scores 8 (4+4)
|
||||||
1,1,6,2,6 scores 12 (6+6)
|
1,1,6,2,6 scores 12 (6+6)
|
||||||
3,3,3,4,1 scores 0
|
3,3,3,4,1 scores 0
|
||||||
3,3,3,3,1 scores 0
|
3,3,3,3,1 scores 0
|
||||||
|
|
||||||
Two pairs:
|
Two pairs:
|
||||||
If there are two pairs of dice with the same number, the
|
If there are two pairs of dice with the same number, the
|
||||||
player scores the sum of these dice.
|
player scores the sum of these dice.
|
||||||
For example, when placed on "two pairs"
|
For example, when placed on "two pairs"
|
||||||
1,1,2,3,3 scores 8 (1+1+3+3)
|
1,1,2,3,3 scores 8 (1+1+3+3)
|
||||||
1,1,2,3,4 scores 0
|
1,1,2,3,4 scores 0
|
||||||
1,1,2,2,2 scores 0
|
1,1,2,2,2 scores 0
|
||||||
|
|
||||||
Three of a kind:
|
Three of a kind:
|
||||||
If there are three dice with the same number, the player
|
If there are three dice with the same number, the player
|
||||||
scores the sum of these dice.
|
scores the sum of these dice.
|
||||||
For example, when placed on "three of a kind"
|
For example, when placed on "three of a kind"
|
||||||
3,3,3,4,5 scores 9 (3+3+3)
|
3,3,3,4,5 scores 9 (3+3+3)
|
||||||
3,3,4,5,6 scores 0
|
3,3,4,5,6 scores 0
|
||||||
3,3,3,3,1 scores 0
|
3,3,3,3,1 scores 0
|
||||||
|
|
||||||
Four of a kind:
|
Four of a kind:
|
||||||
If there are four dice with the same number, the player
|
If there are four dice with the same number, the player
|
||||||
scores the sum of these dice.
|
scores the sum of these dice.
|
||||||
For example, when placed on "four of a kind"
|
For example, when placed on "four of a kind"
|
||||||
2,2,2,2,5 scores 8 (2+2+2+2)
|
2,2,2,2,5 scores 8 (2+2+2+2)
|
||||||
2,2,2,5,5 scores 0
|
2,2,2,5,5 scores 0
|
||||||
2,2,2,2,2 scores 0
|
2,2,2,2,2 scores 0
|
||||||
|
|
||||||
Small straight:
|
Small straight:
|
||||||
When placed on "small straight", if the dice read
|
When placed on "small straight", if the dice read
|
||||||
1,2,3,4,5, the player scores 15 (the sum of all the dice.
|
1,2,3,4,5, the player scores 15 (the sum of all the dice.
|
||||||
|
|
||||||
Large straight:
|
Large straight:
|
||||||
When placed on "large straight", if the dice read
|
When placed on "large straight", if the dice read
|
||||||
2,3,4,5,6, the player scores 20 (the sum of all the dice).
|
2,3,4,5,6, the player scores 20 (the sum of all the dice).
|
||||||
|
|
||||||
Full house:
|
Full house:
|
||||||
If the dice are two of a kind and three of a kind, the
|
If the dice are two of a kind and three of a kind, the
|
||||||
player scores the sum of all the dice.
|
player scores the sum of all the dice.
|
||||||
For example, when placed on "full house"
|
For example, when placed on "full house"
|
||||||
1,1,2,2,2 scores 8 (1+1+2+2+2)
|
1,1,2,2,2 scores 8 (1+1+2+2+2)
|
||||||
2,2,3,3,4 scores 0
|
2,2,3,3,4 scores 0
|
||||||
4,4,4,4,4 scores 0
|
4,4,4,4,4 scores 0
|
||||||
|
|||||||
94
Yahtzee/ruby/test_yahtzee.rb
Normal file
94
Yahtzee/ruby/test_yahtzee.rb
Normal file
@ -0,0 +1,94 @@
|
|||||||
|
require_relative 'yahtzee'
|
||||||
|
require 'test/unit'
|
||||||
|
|
||||||
|
class YahtzeeTest < Test::Unit::TestCase
|
||||||
|
def test_chance_scores_sum_of_all_dice
|
||||||
|
expected = 15
|
||||||
|
actual = Yahtzee.chance(2,3,4,5,1)
|
||||||
|
assert expected == actual
|
||||||
|
assert 16 == Yahtzee.chance(3,3,4,5,1)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_yahtzee_scores_50
|
||||||
|
expected = 50
|
||||||
|
actual = Yahtzee.yahtzee([4,4,4,4,4])
|
||||||
|
assert expected == actual
|
||||||
|
assert 50 == Yahtzee.yahtzee([6,6,6,6,6])
|
||||||
|
assert 0 == Yahtzee.yahtzee([6,6,6,6,3])
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_1s
|
||||||
|
assert Yahtzee.ones(1,2,3,4,5) == 1
|
||||||
|
assert 2 == Yahtzee.ones(1,2,1,4,5)
|
||||||
|
assert 0 == Yahtzee.ones(6,2,2,4,5)
|
||||||
|
assert 4 == Yahtzee.ones(1,2,1,1,1)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_2s
|
||||||
|
assert Yahtzee.twos(1,2,3,2,6) == 4
|
||||||
|
assert Yahtzee.twos(2,2,2,2,2) == 10
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_threes
|
||||||
|
assert 6 == Yahtzee.threes(1,2,3,2,3)
|
||||||
|
assert 12 == Yahtzee.threes(2,3,3,3,3)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_fours_test
|
||||||
|
assert 12 == Yahtzee.new(4,4,4,5,5).fours
|
||||||
|
assert 8 == Yahtzee.new(4,4,5,5,5).fours
|
||||||
|
assert 4 == Yahtzee.new(4,5,5,5,5).fours
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_fives()
|
||||||
|
assert 10 == Yahtzee.new(4,4,4,5,5).fives()
|
||||||
|
assert 15 == Yahtzee.new(4,4,5,5,5).fives()
|
||||||
|
assert 20 == Yahtzee.new(4,5,5,5,5).fives()
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_sixes_test
|
||||||
|
assert 0 == Yahtzee.new(4,4,4,5,5).sixes()
|
||||||
|
assert 6 == Yahtzee.new(4,4,6,5,5).sixes()
|
||||||
|
assert 18 == Yahtzee.new(6,5,6,6,5).sixes()
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_one_pair
|
||||||
|
assert 6 == Yahtzee.score_pair(3,4,3,5,6)
|
||||||
|
assert 10 == Yahtzee.score_pair(5,3,3,3,5)
|
||||||
|
assert 12 == Yahtzee.score_pair(5,3,6,6,5)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_two_Pair
|
||||||
|
assert_equal 16, Yahtzee.two_pair(3,3,5,4,5)
|
||||||
|
assert_equal 0, Yahtzee.two_pair(3,3,5,5,5)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_three_of_a_kind()
|
||||||
|
assert 9 == Yahtzee.three_of_a_kind(3,3,3,4,5)
|
||||||
|
assert 15 == Yahtzee.three_of_a_kind(5,3,5,4,5)
|
||||||
|
assert 0 == Yahtzee.three_of_a_kind(3,3,3,3,5)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_four_of_a_knd
|
||||||
|
assert 12 == Yahtzee.four_of_a_kind(3,3,3,3,5)
|
||||||
|
assert 20 == Yahtzee.four_of_a_kind(5,5,5,4,5)
|
||||||
|
assert 0 == Yahtzee.three_of_a_kind(3,3,3,3,3)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_smallStraight()
|
||||||
|
assert 15 == Yahtzee.smallStraight(1,2,3,4,5)
|
||||||
|
assert 15 == Yahtzee.smallStraight(2,3,4,5,1)
|
||||||
|
assert 0 == Yahtzee.smallStraight(1,2,2,4,5)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_largeStraight
|
||||||
|
assert 20 == Yahtzee.largeStraight(6,2,3,4,5)
|
||||||
|
assert 20 == Yahtzee.largeStraight(2,3,4,5,6)
|
||||||
|
assert 0 == Yahtzee.largeStraight(1,2,2,4,5)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_fullHouse()
|
||||||
|
assert 18 == Yahtzee.fullHouse(6,2,2,2,6)
|
||||||
|
assert 0 == Yahtzee.fullHouse(2,3,4,5,6)
|
||||||
|
end
|
||||||
|
end
|
||||||
256
Yahtzee/ruby/yahtzee.rb
Normal file
256
Yahtzee/ruby/yahtzee.rb
Normal file
@ -0,0 +1,256 @@
|
|||||||
|
class Yahtzee
|
||||||
|
def self.chance(d1, d2, d3, d4, d5)
|
||||||
|
total = 0
|
||||||
|
total += d1
|
||||||
|
total += d2
|
||||||
|
total += d3
|
||||||
|
total += d4
|
||||||
|
total += d5
|
||||||
|
return total
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.yahtzee(dice)
|
||||||
|
counts = [0]*(dice.length+1)
|
||||||
|
for die in dice do
|
||||||
|
counts[die-1] += 1
|
||||||
|
end
|
||||||
|
for i in 0..counts.size do
|
||||||
|
if counts[i] == 5
|
||||||
|
return 50
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return 0
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.ones( d1, d2, d3, d4, d5)
|
||||||
|
sum = 0
|
||||||
|
if (d1 == 1)
|
||||||
|
sum += 1
|
||||||
|
end
|
||||||
|
if (d2 == 1)
|
||||||
|
sum += 1
|
||||||
|
end
|
||||||
|
if (d3 == 1)
|
||||||
|
sum += 1
|
||||||
|
end
|
||||||
|
if (d4 == 1)
|
||||||
|
sum += 1
|
||||||
|
end
|
||||||
|
if (d5 == 1)
|
||||||
|
sum += 1
|
||||||
|
end
|
||||||
|
|
||||||
|
sum
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.twos( d1, d2, d3, d4, d5)
|
||||||
|
sum = 0
|
||||||
|
if (d1 == 2)
|
||||||
|
sum += 2
|
||||||
|
end
|
||||||
|
if (d2 == 2)
|
||||||
|
sum += 2
|
||||||
|
end
|
||||||
|
if (d3 == 2)
|
||||||
|
sum += 2
|
||||||
|
end
|
||||||
|
if (d4 == 2)
|
||||||
|
sum += 2
|
||||||
|
end
|
||||||
|
if (d5 == 2)
|
||||||
|
sum += 2
|
||||||
|
end
|
||||||
|
return sum
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.threes( d1, d2, d3, d4, d5)
|
||||||
|
s = 0
|
||||||
|
if (d1 == 3)
|
||||||
|
s += 3
|
||||||
|
end
|
||||||
|
if (d2 == 3)
|
||||||
|
s += 3
|
||||||
|
end
|
||||||
|
if (d3 == 3)
|
||||||
|
s += 3
|
||||||
|
end
|
||||||
|
if (d4 == 3)
|
||||||
|
s += 3
|
||||||
|
end
|
||||||
|
if (d5 == 3)
|
||||||
|
s += 3
|
||||||
|
end
|
||||||
|
return s
|
||||||
|
end
|
||||||
|
|
||||||
|
def initialize(d1, d2, d3, d4, _5)
|
||||||
|
@dice = [0]*5
|
||||||
|
@dice[0] = d1
|
||||||
|
@dice[1] = d2
|
||||||
|
@dice[2] = d3
|
||||||
|
@dice[3] = d4
|
||||||
|
@dice[4] = _5
|
||||||
|
end
|
||||||
|
|
||||||
|
def fours
|
||||||
|
sum = 0
|
||||||
|
for at in Array 0..4
|
||||||
|
if (@dice[at] == 4)
|
||||||
|
sum += 4
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return sum
|
||||||
|
end
|
||||||
|
|
||||||
|
def fives()
|
||||||
|
s = 0
|
||||||
|
i = 0
|
||||||
|
for i in (Range.new(0, @dice.size))
|
||||||
|
if (@dice[i] == 5)
|
||||||
|
s = s + 5
|
||||||
|
end
|
||||||
|
end
|
||||||
|
s
|
||||||
|
end
|
||||||
|
|
||||||
|
def sixes
|
||||||
|
sum = 0
|
||||||
|
for at in 0..@dice.length
|
||||||
|
if (@dice[at] == 6)
|
||||||
|
sum = sum + 6
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return sum
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.score_pair( d1, d2, d3, d4, d5)
|
||||||
|
counts = [0]*6
|
||||||
|
counts[d1-1] += 1
|
||||||
|
counts[d2-1] += 1
|
||||||
|
counts[d3-1] += 1
|
||||||
|
counts[d4-1] += 1
|
||||||
|
counts[d5-1] += 1
|
||||||
|
at = 0
|
||||||
|
(0...6).each do |at|
|
||||||
|
if (counts[6-at-1] == 2)
|
||||||
|
return (6-at)*2
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return 0
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.two_pair( d1, d2, d3, d4, d5)
|
||||||
|
counts = [0]*6
|
||||||
|
counts[d1-1] += 1
|
||||||
|
counts[d2-1] += 1
|
||||||
|
counts[d3-1] += 1
|
||||||
|
counts[d4-1] += 1
|
||||||
|
counts[d5-1] += 1
|
||||||
|
n = 0
|
||||||
|
score = 0
|
||||||
|
for i in Array 0..5
|
||||||
|
if (counts[6-i-1] == 2)
|
||||||
|
n = n+1
|
||||||
|
score += (6-i)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
if (n == 2)
|
||||||
|
return score * 2
|
||||||
|
else
|
||||||
|
return 0
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.four_of_a_kind( _1, _2, d3, d4, d5)
|
||||||
|
tallies = [0]*6
|
||||||
|
tallies[_1-1] += 1
|
||||||
|
tallies[_2-1] += 1
|
||||||
|
tallies[d3-1] += 1
|
||||||
|
tallies[d4-1] += 1
|
||||||
|
tallies[d5-1] += 1
|
||||||
|
for i in (0..6)
|
||||||
|
if (tallies[i] == 4)
|
||||||
|
return (i+1) * 4
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return 0
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.three_of_a_kind( d1, d2, d3, d4, d5)
|
||||||
|
t = [0]*6
|
||||||
|
t[d1-1] += 1
|
||||||
|
t[d2-1] += 1
|
||||||
|
t[d3-1] += 1
|
||||||
|
t[d4-1] += 1
|
||||||
|
t[d5-1] += 1
|
||||||
|
for i in [0,1,2,3,4,5]
|
||||||
|
if (t[i] == 3)
|
||||||
|
return (i+1) * 3
|
||||||
|
end
|
||||||
|
end
|
||||||
|
0
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.smallStraight( d1, d2, d3, d4, d5)
|
||||||
|
tallies = [0]*6
|
||||||
|
tallies[d1-1] += 1
|
||||||
|
tallies[d2-1] += 1
|
||||||
|
tallies[d3-1] += 1
|
||||||
|
tallies[d4-1] += 1
|
||||||
|
tallies[d5-1] += 1
|
||||||
|
(tallies[0] == 1 and
|
||||||
|
tallies[1] == 1 and
|
||||||
|
tallies[2] == 1 and
|
||||||
|
tallies[3] == 1 and
|
||||||
|
tallies[4] == 1) ? 15 : 0
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.largeStraight( d1, d2, d3, d4, d5)
|
||||||
|
tallies = [0]*6
|
||||||
|
tallies[d1-1] += 1
|
||||||
|
tallies[d2-1] += 1
|
||||||
|
tallies[d3-1] += 1
|
||||||
|
tallies[d4-1] += 1
|
||||||
|
tallies[d5-1] += 1
|
||||||
|
if (tallies[1] == 1 and tallies[2] == 1 and tallies[3] == 1 and tallies[4] == 1 and tallies[5] == 1)
|
||||||
|
return 20
|
||||||
|
end
|
||||||
|
return 0
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.fullHouse( d1, d2, d3, d4, d5)
|
||||||
|
tallies = []
|
||||||
|
_2 = false
|
||||||
|
i = 0
|
||||||
|
_2_at = 0
|
||||||
|
_3 = false
|
||||||
|
_3_at = 0
|
||||||
|
|
||||||
|
tallies = [0]*6
|
||||||
|
tallies[d1-1] += 1
|
||||||
|
tallies[d2-1] += 1
|
||||||
|
tallies[d3-1] += 1
|
||||||
|
tallies[d4-1] += 1
|
||||||
|
tallies[d5-1] += 1
|
||||||
|
|
||||||
|
for i in Array 0..5
|
||||||
|
if (tallies[i] == 2)
|
||||||
|
_2 = true
|
||||||
|
_2_at = i+1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
for i in Array 0..5
|
||||||
|
if (tallies[i] == 3)
|
||||||
|
_3 = true
|
||||||
|
_3_at = i+1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if (_2 and _3)
|
||||||
|
return _2_at * 2 + _3_at * 3
|
||||||
|
else
|
||||||
|
return 0
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
Loading…
Reference in New Issue
Block a user