New Refactoring Kata - Yahtzee - by Jon Jagger

This commit is contained in:
Emily Bache 2012-06-14 11:30:21 +02:00
parent 32ce66e211
commit 4945165df2
7 changed files with 1157 additions and 0 deletions

120
Yahtzee/README Normal file
View File

@ -0,0 +1,120 @@
========================
Yahtzee Refactoring Kata
========================
This Refactoring Kata was designed by Jon Jagger and is available in his Cyber-Dojo on these urls:
C#: http://cyber-dojo.com/diff/show/E4466E24B2?avatar=elephant&tag=26
Java: http://cyber-dojo.com/diff/show/3D5589AA49?avatar=panda&tag=1
The other language translations have been contributed by:
Python: Emily Bache
=============
Yahtzee rules
=============
The game of yahtzee is a simple dice game. Each player
rolls five six-sided dice. They can re-roll some or all
of the dice up to three times (including the original roll).
For example, suppose a players rolls
3,4,5,5,2
They hold (-,-,5,5,-) and re-roll (3,4,-,-,2)
5,1,5,5,3
They hold (5,-,5,5,-) and re-roll (-,1,-,-,3)
5,6,5,5,2
The player then places the roll in a category, such as ones,
twos, fives, pair, two pairs etc (see below). If the roll is
compatible with the category, the player gets a score for the
roll according to the rules. If the roll is not compatible
with the category, the player scores zero for the roll.
For example, suppose a player scores 5,6,5,5,2 in the fives
category they would score 15 (three fives). The score for
that go is then added to their total and the category cannot
be used again in the remaining goes for that game.
A full game consists of one go for each category. Thus, for
their last go in a game, a player must choose their only
remaining category.
Your task is to score a GIVEN roll in a GIVEN category.
You do NOT have to program the random dice rolling.
The game is NOT played by letting the computer choose the
highest scoring category for a given roll.
Yahzee Categories and Scoring Rules
===================================
Chance:
The player scores the sum of all dice,
no matter what they read.
For example,
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)
Yahtzee:
If all dice have the same number,
the player scores 50 points.
For example,
1,1,1,1,1 placed on "yahtzee" scores 50
1,1,1,2,1 placed on "yahtzee" scores 0
Ones, Twos, Threes, Fours, Fives, Sixes:
The player scores the sum of the dice that reads one,
two, three, four, five or six, respectively.
For example,
1,1,2,4,4 placed on "fours" scores 8 (4+4)
2,3,2,5,1 placed on "twos" scores 4 (2+2)
3,3,3,4,5 placed on "ones" scores 0
Pair:
The player scores the sum of the two highest matching dice.
For example, when placed on "pair"
3,3,3,4,4 scores 8 (4+4)
1,1,6,2,6 scores 12 (6+6)
3,3,3,4,1 scores 0
3,3,3,3,1 scores 0
Two pairs:
If there are two pairs of dice with the same number, the
player scores the sum of these dice.
For example, when placed on "two pairs"
1,1,2,3,3 scores 8 (1+1+3+3)
1,1,2,3,4 scores 0
1,1,2,2,2 scores 0
Three of a kind:
If there are three dice with the same number, the player
scores the sum of these dice.
For example, when placed on "three of a kind"
3,3,3,4,5 scores 9 (3+3+3)
3,3,4,5,6 scores 0
3,3,3,3,1 scores 0
Four of a kind:
If there are four dice with the same number, the player
scores the sum of these dice.
For example, when placed on "four of a kind"
2,2,2,2,5 scores 8 (2+2+2+2)
2,2,2,5,5 scores 0
2,2,2,2,2 scores 0
Small straight:
When placed on "small straight", if the dice read
1,2,3,4,5, the player scores 15 (the sum of all the dice.
Large straight:
When placed on "large straight", if the dice read
2,3,4,5,6, the player scores 20 (the sum of all the dice).
Full house:
If the dice are two of a kind and three of a kind, the
player scores the sum of all the dice.
For example, when placed on "full house"
1,1,2,2,2 scores 8 (1+1+2+2+2)
2,2,3,3,4 scores 0
4,4,4,4,4 scores 0

238
Yahtzee/csharp/Yahtzee.cs Normal file
View File

@ -0,0 +1,238 @@
public class Yahtzee {
public static int Chance(int d1, int d2, int d3, int d4, int d5)
{
int total = 0;
total += d1;
total += d2;
total += d3;
total += d4;
total += d5;
return total;
}
public static int yahtzee(params int[] dice)
{
int[] counts = new int[6];
foreach (int die in dice)
counts[die-1]++;
for (int i = 0; i != 6; i++)
if (counts[i] == 5)
return 50;
return 0;
}
public static int Ones(int d1, int d2, int d3, int d4, int d5) {
int sum = 0;
if (d1 == 1) sum++;
if (d2 == 1) sum++;
if (d3 == 1) sum++;
if (d4 == 1) sum++;
if (d5 == 1)
sum++;
return sum;
}
public static int Twos(int d1, int d2, int d3, int d4, int d5) {
int 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;
}
public static int Threes(int d1, int d2, int d3, int d4, int d5) {
int s;
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;
}
protected int[] dice;
public Yahtzee(int d1, int d2, int d3, int d4, int _5)
{
dice = new int[5];
dice[0] = d1;
dice[1] = d2;
dice[2] = d3;
dice[3] = d4;
dice[4] = _5;
}
public int Fours()
{
int sum;
sum = 0;
for (int at = 0; at != 5; at++) {
if (dice[at] == 4) {
sum += 4;
}
}
return sum;
}
public int Fives()
{
int s = 0;
int i;
for (i = 0; i < dice.Length; i++)
if (dice[i] == 5)
s = s + 5;
return s;
}
public int sixes()
{
int sum = 0;
for (int at = 0; at < dice.Length; at++)
if (dice[at] == 6)
sum = sum + 6;
return sum;
}
public static int ScorePair(int d1, int d2, int d3, int d4, int d5)
{
int[] counts = new int[6];
counts[d1-1]++;
counts[d2-1]++;
counts[d3-1]++;
counts[d4-1]++;
counts[d5-1]++;
int at;
for (at = 0; at != 6; at++)
if (counts[6-at-1] == 2)
return (6-at)*2;
return 0;
}
public static int TwoPair(int d1, int d2, int d3, int d4, int d5)
{
int[] counts = new int[6];
counts[d1-1]++;
counts[d2-1]++;
counts[d3-1]++;
counts[d4-1]++;
counts[d5-1]++;
int n = 0;
int score = 0;
for (int i = 0; i < 6; i += 1)
if (counts[6-i-1] == 2) {
n++;
score += (6-i);
}
if (n == 2)
return score * 2;
else
return 0;
}
public static int FourOfAKind(int _1, int _2, int d3, int d4, int d5)
{
int[] tallies;
tallies = new int[6];
tallies[_1-1]++;
tallies[_2-1]++;
tallies[d3-1]++;
tallies[d4-1]++;
tallies[d5-1]++;
for (int i = 0; i < 6; i++)
if (tallies[i] == 4)
return (i+1) * 4;
return 0;
}
public static int ThreeOfAKind(int d1, int d2, int d3, int d4, int d5)
{
int[] t;
t = new int[6];
t[d1-1]++;
t[d2-1]++;
t[d3-1]++;
t[d4-1]++;
t[d5-1]++;
for (int i = 0; i < 6; i++)
if (t[i] == 3)
return (i+1) * 3;
return 0;
}
public static int SmallStraight(int d1, int d2, int d3, int d4, int d5)
{
int[] tallies;
tallies = new int[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 &&
tallies[1] == 1 &&
tallies[2] == 1 &&
tallies[3] == 1 &&
tallies[4] == 1)
return 15;
return 0;
}
public static int LargeStraight(int d1, int d2, int d3, int d4, int d5)
{
int[] tallies;
tallies = new int[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 &&
tallies[2] == 1 &&
tallies[3] == 1 &&
tallies[4] == 1
&& tallies[5] == 1)
return 20;
return 0;
}
public static int FullHouse(int d1, int d2, int d3, int d4, int d5)
{
int[] tallies;
bool _2 = false;
int i;
int _2_at = 0;
bool _3 = false;
int _3_at = 0;
tallies = new int[6];
tallies[d1-1] += 1;
tallies[d2-1] += 1;
tallies[d3-1] += 1;
tallies[d4-1] += 1;
tallies[d5-1] += 1;
for (i = 0; i != 6; i += 1)
if (tallies[i] == 2) {
_2 = true;
_2_at = i+1;
}
for (i = 0; i != 6; i += 1)
if (tallies[i] == 3) {
_3 = true;
_3_at = i+1;
}
if (_2 && _3)
return _2_at * 2 + _3_at * 3;
else
return 0;
}
}

View File

@ -0,0 +1,124 @@
using NUnit.Framework;
[TestFixture]
public class UntitledTest
{
[Test]
public void Chance_scores_sum_of_all_dice()
{
int expected = 15;
int actual = Yahtzee.Chance(2,3,4,5,1);
Assert.AreEqual(expected, actual);
Assert.AreEqual(16, Yahtzee.Chance(3,3,4,5,1));
}
[Test]
public void Yahtzee_scores_50()
{
int expected = 50;
int actual = Yahtzee.yahtzee(4,4,4,4,4);
Assert.AreEqual(expected, actual);
Assert.AreEqual(50, Yahtzee.yahtzee(6,6,6,6,6));
Assert.AreEqual(0, Yahtzee.yahtzee(6,6,6,6,3));
}
[Test]
public void Test_1s() {
Assert.IsTrue(Yahtzee.Ones(1,2,3,4,5) == 1);
Assert.AreEqual(2, Yahtzee.Ones(1,2,1,4,5));
Assert.AreEqual(0, Yahtzee.Ones(6,2,2,4,5));
Assert.AreEqual(4, Yahtzee.Ones(1,2,1,1,1));
}
[Test]
public void test_2s()
{
Assert.AreEqual(4, Yahtzee.Twos(1,2,3,2,6));
Assert.AreEqual(10, Yahtzee.Twos(2,2,2,2,2));
}
[Test]
public void test_threes()
{
Assert.AreEqual(6, Yahtzee.Threes(1,2,3,2,3));
Assert.AreEqual(12, Yahtzee.Threes(2,3,3,3,3));
}
[Test]
public void fours_test()
{
Assert.AreEqual(12, new Yahtzee(4,4,4,5,5).Fours());
Assert.AreEqual(8, new Yahtzee(4,4,5,5,5).Fours());
Assert.AreEqual(4, new Yahtzee(4,5,5,5,5).Fours());
}
[Test]
public void fives() {
Assert.AreEqual(10, new Yahtzee(4,4,4,5,5).Fives());
Assert.AreEqual(15, new Yahtzee(4,4,5,5,5).Fives());
Assert.AreEqual(20, new Yahtzee(4,5,5,5,5).Fives());
}
[Test]
public void sixes_test()
{
Assert.AreEqual(0, new Yahtzee(4,4,4,5,5).sixes());
Assert.AreEqual(6, new Yahtzee(4,4,6,5,5).sixes());
Assert.AreEqual(18, new Yahtzee(6,5,6,6,5).sixes());
}
[Test]
public void one_pair()
{
Assert.AreEqual(6, Yahtzee.ScorePair(3,4,3,5,6));
Assert.AreEqual(10, Yahtzee.ScorePair(5,3,3,3,5));
Assert.AreEqual(12, Yahtzee.ScorePair(5,3,6,6,5));
}
[Test]
public void two_Pair()
{
Assert.AreEqual(16, Yahtzee.TwoPair(3,3,5,4,5));
Assert.AreEqual(0, Yahtzee.TwoPair(3,3,5,5,5));
}
[Test]
public void three_of_a_kind()
{
Assert.AreEqual(9, Yahtzee.ThreeOfAKind(3,3,3,4,5));
Assert.AreEqual(15, Yahtzee.ThreeOfAKind(5,3,5,4,5));
Assert.AreEqual(0, Yahtzee.ThreeOfAKind(3,3,3,3,5));
}
[Test]
public void four_of_a_knd()
{
Assert.AreEqual(12, Yahtzee.FourOfAKind(3,3,3,3,5));
Assert.AreEqual(20, Yahtzee.FourOfAKind(5,5,5,4,5));
Assert.AreEqual(0, Yahtzee.FourOfAKind(3,3,3,3,3));
}
[Test]
public void smallStraight()
{
Assert.AreEqual(15, Yahtzee.SmallStraight(1,2,3,4,5));
Assert.AreEqual(15, Yahtzee.SmallStraight(2,3,4,5,1));
Assert.AreEqual(0, Yahtzee.SmallStraight(1,2,2,4,5));
}
[Test]
public void largeStraight()
{
Assert.AreEqual(20, Yahtzee.LargeStraight(6,2,3,4,5));
Assert.AreEqual(20, Yahtzee.LargeStraight(2,3,4,5,6));
Assert.AreEqual(0, Yahtzee.LargeStraight(1,2,2,4,5));
}
[Test]
public void fullHouse()
{
Assert.AreEqual(18, Yahtzee.FullHouse(6,2,2,2,6));
Assert.AreEqual(0, Yahtzee.FullHouse(2,3,4,5,6));
}
}

241
Yahtzee/java/Yahtzee.java Normal file
View File

@ -0,0 +1,241 @@
public class Yahtzee {
public static int chance(int d1, int d2, int d3, int d4, int d5)
{
int total = 0;
total += d1;
total += d2;
total += d3;
total += d4;
total += d5;
return total;
}
public static int yahtzee(int... dice)
{
int[] counts = new int[6];
for (int die : dice)
counts[die-1]++;
for (int i = 0; i != 6; i++)
if (counts[i] == 5)
return 50;
return 0;
}
public static int ones(int d1, int d2, int d3, int d4, int d5) {
int sum = 0;
if (d1 == 1) sum++;
if (d2 == 1) sum++;
if (d3 == 1) sum++;
if (d4 == 1) sum++;
if (d5 == 1)
sum++;
return sum;
}
public static int twos(int d1, int d2, int d3, int d4, int d5) {
int 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;
}
public static int threes(int d1, int d2, int d3, int d4, int d5) {
int s;
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;
}
protected int[] dice;
public Yahtzee(int d1, int d2, int d3, int d4, int _5)
{
dice = new int[5];
dice[0] = d1;
dice[1] = d2;
dice[2] = d3;
dice[3] = d4;
dice[4] = _5;
}
public int fours()
{
int sum;
sum = 0;
for (int at = 0; at != 5; at++) {
if (dice[at] == 4) {
sum += 4;
}
}
return sum;
}
public int fives()
{
int s = 0;
int i;
for (i = 0; i < dice.length; i++)
if (dice[i] == 5)
s = s + 5;
return s;
}
public int sixes()
{
int sum = 0;
for (int at = 0; at < dice.length; at++)
if (dice[at] == 6)
sum = sum + 6;
return sum;
}
public static int score_pair(int d1, int d2, int d3, int d4, int d5)
{
int[] counts = new int[6];
counts[d1-1]++;
counts[d2-1]++;
counts[d3-1]++;
counts[d4-1]++;
counts[d5-1]++;
int at;
for (at = 0; at != 6; at++)
if (counts[6-at-1] == 2)
return (6-at)*2;
return 0;
}
public static int two_pair(int d1, int d2, int d3, int d4, int d5)
{
int[] counts = new int[6];
counts[d1-1]++;
counts[d2-1]++;
counts[d3-1]++;
counts[d4-1]++;
counts[d5-1]++;
int n = 0;
int score = 0;
for (int i = 0; i < 6; i += 1)
if (counts[6-i-1] == 2) {
n++;
score += (6-i);
}
if (n == 2)
return score * 2;
else
return 0;
}
public static int four_of_a_kind(int _1, int _2, int d3, int d4, int d5)
{
int[] tallies;
tallies = new int[6];
tallies[_1-1]++;
tallies[_2-1]++;
tallies[d3-1]++;
tallies[d4-1]++;
tallies[d5-1]++;
for (int i = 0; i < 6; i++)
if (tallies[i] == 4)
return (i+1) * 4;
return 0;
}
public static int three_of_a_kind(int d1, int d2, int d3, int d4, int d5)
{
int[] t;
t = new int[6];
t[d1-1]++;
t[d2-1]++;
t[d3-1]++;
t[d4-1]++;
t[d5-1]++;
for (int i = 0; i < 6; i++)
if (t[i] == 3)
return (i+1) * 3;
return 0;
}
public static int smallStraight(int d1, int d2, int d3, int d4, int d5)
{
int[] tallies;
tallies = new int[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 &&
tallies[1] == 1 &&
tallies[2] == 1 &&
tallies[3] == 1 &&
tallies[4] == 1)
return 15;
return 0;
}
public static int largeStraight(int d1, int d2, int d3, int d4, int d5)
{
int[] tallies;
tallies = new int[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 &&
tallies[2] == 1 &&
tallies[3] == 1 &&
tallies[4] == 1
&& tallies[5] == 1)
return 20;
return 0;
}
public static int fullHouse(int d1, int d2, int d3, int d4, int d5)
{
int[] tallies;
boolean _2 = false;
int i;
int _2_at = 0;
boolean _3 = false;
int _3_at = 0;
tallies = new int[6];
tallies[d1-1] += 1;
tallies[d2-1] += 1;
tallies[d3-1] += 1;
tallies[d4-1] += 1;
tallies[d5-1] += 1;
for (i = 0; i != 6; i += 1)
if (tallies[i] == 2) {
_2 = true;
_2_at = i+1;
}
for (i = 0; i != 6; i += 1)
if (tallies[i] == 3) {
_3 = true;
_3_at = i+1;
}
if (_2 && _3)
return _2_at * 2 + _3_at * 3;
else
return 0;
}
}

View File

@ -0,0 +1,110 @@
import org.junit.*;
import static org.junit.Assert.*;
public class YahtzeeTest {
@Test
public void chance_scores_sum_of_all_dice() {
int expected = 15;
int actual = Yahtzee.chance(2,3,4,5,1);
assertEquals(expected, actual);
assertEquals(16, Yahtzee.chance(3,3,4,5,1));
}
@Test public void yahtzee_scores_50() {
int expected = 50;
int actual = Yahtzee.yahtzee(4,4,4,4,4);
assertEquals(expected, actual);
assertEquals(50, Yahtzee.yahtzee(6,6,6,6,6));
assertEquals(0, Yahtzee.yahtzee(6,6,6,6,3));
}
@Test public void test_1s() {
assertTrue(Yahtzee.ones(1,2,3,4,5) == 1);
assertEquals(2, Yahtzee.ones(1,2,1,4,5));
assertEquals(0, Yahtzee.ones(6,2,2,4,5));
assertEquals(4, Yahtzee.ones(1,2,1,1,1));
}
@Test
public void test_2s() {
assertEquals(4, Yahtzee.twos(1,2,3,2,6));
assertEquals(10, Yahtzee.twos(2,2,2,2,2));
}
@Test
public void test_threes() {
assertEquals(6, Yahtzee.threes(1,2,3,2,3));
assertEquals(12, Yahtzee.threes(2,3,3,3,3));
}
@Test
public void fours_test()
{
assertEquals(12, new Yahtzee(4,4,4,5,5).fours());
assertEquals(8, new Yahtzee(4,4,5,5,5).fours());
assertEquals(4, new Yahtzee(4,5,5,5,5).fours());
}
@Test
public void fives() {
assertEquals(10, new Yahtzee(4,4,4,5,5).fives());
assertEquals(15, new Yahtzee(4,4,5,5,5).fives());
assertEquals(20, new Yahtzee(4,5,5,5,5).fives());
}
@Test
public void sixes_test() {
assertEquals(0, new Yahtzee(4,4,4,5,5).sixes());
assertEquals(6, new Yahtzee(4,4,6,5,5).sixes());
assertEquals(18, new Yahtzee(6,5,6,6,5).sixes());
}
@Test
public void one_pair() {
assertEquals(6, Yahtzee.score_pair(3,4,3,5,6));
assertEquals(10, Yahtzee.score_pair(5,3,3,3,5));
assertEquals(12, Yahtzee.score_pair(5,3,6,6,5));
}
@Test
public void two_Pair() {
assertEquals(16, Yahtzee.two_pair(3,3,5,4,5));
assertEquals(0, Yahtzee.two_pair(3,3,5,5,5));
}
@Test
public void three_of_a_kind()
{
assertEquals(9, Yahtzee.three_of_a_kind(3,3,3,4,5));
assertEquals(15, Yahtzee.three_of_a_kind(5,3,5,4,5));
assertEquals(0, Yahtzee.three_of_a_kind(3,3,3,3,5));
}
@Test
public void four_of_a_knd() {
assertEquals(12, Yahtzee.four_of_a_kind(3,3,3,3,5));
assertEquals(20, Yahtzee.four_of_a_kind(5,5,5,4,5));
assertEquals(0, Yahtzee.three_of_a_kind(3,3,3,3,3));
}
@Test
public void smallStraight() {
assertEquals(15, Yahtzee.smallStraight(1,2,3,4,5));
assertEquals(15, Yahtzee.smallStraight(2,3,4,5,1));
assertEquals(0, Yahtzee.smallStraight(1,2,2,4,5));
}
@Test
public void largeStraight() {
assertEquals(20, Yahtzee.largeStraight(6,2,3,4,5));
assertEquals(20, Yahtzee.largeStraight(2,3,4,5,6));
assertEquals(0, Yahtzee.largeStraight(1,2,2,4,5));
}
@Test
public void fullHouse() {
assertEquals(18, Yahtzee.fullHouse(6,2,2,2,6));
assertEquals(0, Yahtzee.fullHouse(2,3,4,5,6));
}
}

View File

@ -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)

230
Yahtzee/python/yahtzee.py Normal file
View File

@ -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