mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-10-16 19:05:05 +00:00
New Refactoring Kata - Yahtzee - by Jon Jagger
This commit is contained in:
1 parent
32ce66e211
commit
4945165df2
7 files changed
+1157
No files matched your search
@@ -0,0 +1,94 @@
|
||||
from yahtzee import Yahtzee
|
||||
|
||||
# These unit tests can be run using the py.test framework
|
||||
# available from http://pytest.org/
|
||||
|
||||
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)
|
||||
|
||||
|
||||
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])
|
||||
|
||||
|
||||
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)
|
||||
|
||||
|
||||
def test_2s():
|
||||
assert 4 == Yahtzee.twos(1,2,3,2,6)
|
||||
assert 10 == Yahtzee.twos(2,2,2,2,2)
|
||||
|
||||
|
||||
def test_threes():
|
||||
assert 6 == Yahtzee.threes(1,2,3,2,3)
|
||||
assert 12 == Yahtzee.threes(2,3,3,3,3)
|
||||
|
||||
|
||||
def test_fours_test():
|
||||
assert 12 == Yahtzee(4,4,4,5,5).fours()
|
||||
assert 8 == Yahtzee(4,4,5,5,5).fours()
|
||||
assert 4 == Yahtzee(4,5,5,5,5).fours()
|
||||
|
||||
|
||||
def test_fives():
|
||||
assert 10 == Yahtzee(4,4,4,5,5).fives()
|
||||
assert 15 == Yahtzee(4,4,5,5,5).fives()
|
||||
assert 20 == Yahtzee(4,5,5,5,5).fives()
|
||||
|
||||
|
||||
def test_sixes_test():
|
||||
assert 0 == Yahtzee(4,4,4,5,5).sixes()
|
||||
assert 6 == Yahtzee(4,4,6,5,5).sixes()
|
||||
assert 18 == Yahtzee(6,5,6,6,5).sixes()
|
||||
|
||||
|
||||
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)
|
||||
|
||||
|
||||
def test_two_Pair():
|
||||
assert 16 == Yahtzee.two_pair(3,3,5,4,5)
|
||||
assert 0 == Yahtzee.two_pair(3,3,5,5,5)
|
||||
|
||||
|
||||
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)
|
||||
|
||||
|
||||
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)
|
||||
|
||||
|
||||
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)
|
||||
|
||||
|
||||
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)
|
||||
|
||||
|
||||
def test_fullHouse():
|
||||
assert 18 == Yahtzee.fullHouse(6,2,2,2,6)
|
||||
assert 0 == Yahtzee.fullHouse(2,3,4,5,6)
|
||||
|
||||
@@ -0,0 +1,230 @@
|
||||
class Yahtzee:
|
||||
|
||||
@staticmethod
|
||||
def chance(d1, d2, d3, d4, d5):
|
||||
total = 0
|
||||
total += d1
|
||||
total += d2
|
||||
total += d3
|
||||
total += d4
|
||||
total += d5
|
||||
return total
|
||||
|
||||
@staticmethod
|
||||
def yahtzee(dice):
|
||||
counts = [0]*(len(dice)+1)
|
||||
for die in dice:
|
||||
counts[die-1] += 1
|
||||
for i in range(len(counts)):
|
||||
if counts[i] == 5:
|
||||
return 50
|
||||
return 0
|
||||
|
||||
@staticmethod
|
||||
def ones( d1, d2, d3, d4, d5):
|
||||
sum = 0
|
||||
if (d1 == 1):
|
||||
sum += 1
|
||||
if (d2 == 1):
|
||||
sum += 1
|
||||
if (d3 == 1):
|
||||
sum += 1
|
||||
if (d4 == 1):
|
||||
sum += 1
|
||||
if (d5 == 1):
|
||||
sum += 1
|
||||
|
||||
return sum
|
||||
|
||||
|
||||
@staticmethod
|
||||
def twos( d1, d2, d3, d4, d5):
|
||||
sum = 0
|
||||
if (d1 == 2):
|
||||
sum += 2
|
||||
if (d2 == 2):
|
||||
sum += 2
|
||||
if (d3 == 2):
|
||||
sum += 2
|
||||
if (d4 == 2):
|
||||
sum += 2
|
||||
if (d5 == 2):
|
||||
sum += 2
|
||||
return sum
|
||||
|
||||
@staticmethod
|
||||
def threes( d1, d2, d3, d4, d5):
|
||||
s = 0
|
||||
if (d1 == 3):
|
||||
s += 3
|
||||
if (d2 == 3):
|
||||
s += 3
|
||||
if (d3 == 3):
|
||||
s += 3
|
||||
if (d4 == 3):
|
||||
s += 3
|
||||
if (d5 == 3):
|
||||
s += 3
|
||||
return s
|
||||
|
||||
|
||||
def __init__(self, d1, d2, d3, d4, _5):
|
||||
self.dice = [0]*5
|
||||
self.dice[0] = d1
|
||||
self.dice[1] = d2
|
||||
self.dice[2] = d3
|
||||
self.dice[3] = d4
|
||||
self.dice[4] = _5
|
||||
|
||||
def fours(self):
|
||||
sum = 0
|
||||
for at in range(5):
|
||||
if (self.dice[at] == 4):
|
||||
sum += 4
|
||||
return sum
|
||||
|
||||
|
||||
def fives(self):
|
||||
s = 0
|
||||
i = 0
|
||||
for i in range(len(self.dice)):
|
||||
if (self.dice[i] == 5):
|
||||
s = s + 5
|
||||
return s
|
||||
|
||||
|
||||
def sixes(self):
|
||||
sum = 0
|
||||
for at in range(len(self.dice)):
|
||||
if (self.dice[at] == 6):
|
||||
sum = sum + 6
|
||||
return sum
|
||||
|
||||
@staticmethod
|
||||
def 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
|
||||
for at in range(6):
|
||||
if (counts[6-at-1] == 2):
|
||||
return (6-at)*2
|
||||
return 0
|
||||
|
||||
@staticmethod
|
||||
def 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 range(6):
|
||||
if (counts[6-i-1] == 2):
|
||||
n = n+1
|
||||
score += (6-i)
|
||||
|
||||
if (n == 2):
|
||||
return score * 2
|
||||
else:
|
||||
return 0
|
||||
|
||||
@staticmethod
|
||||
def 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 range(6):
|
||||
if (tallies[i] == 4):
|
||||
return (i+1) * 4
|
||||
return 0
|
||||
|
||||
|
||||
@staticmethod
|
||||
def 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 range(6):
|
||||
if (t[i] == 3):
|
||||
return (i+1) * 3
|
||||
return 0
|
||||
|
||||
|
||||
@staticmethod
|
||||
def 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
|
||||
if (tallies[0] == 1 and
|
||||
tallies[1] == 1 and
|
||||
tallies[2] == 1 and
|
||||
tallies[3] == 1 and
|
||||
tallies[4] == 1):
|
||||
return 15
|
||||
return 0
|
||||
|
||||
|
||||
@staticmethod
|
||||
def 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
|
||||
return 0
|
||||
|
||||
|
||||
@staticmethod
|
||||
def 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 range(6):
|
||||
if (tallies[i] == 2):
|
||||
_2 = True
|
||||
_2_at = i+1
|
||||
|
||||
|
||||
for i in range(6):
|
||||
if (tallies[i] == 3):
|
||||
_3 = True
|
||||
_3_at = i+1
|
||||
|
||||
|
||||
if (_2 and _3):
|
||||
return _2_at * 2 + _3_at * 3
|
||||
else:
|
||||
return 0
|
||||
Reference in new issue
Block a user