using System; using System.Collections.Generic; using System.Linq; namespace Kata { public class Yahtzee { public static int Chance(int[] dice) { return dice.Sum(); } public static int yahtzee(params int[] dice) { if (dice.Any(die => die != dice[0])) { return 0; } return 50; } public static int Ones(int[] dice) { return CalculateArray(dice, 1); } public static int Twos(int[] dice) { return CalculateArray(dice, 2); } public static int Threes(int[] dice) { return CalculateArray(dice, 3); } public static int Fours(int[] dice) { return CalculateArray(dice, 4); } public static int Fives(int[] dice) { return CalculateArray(dice, 5); } public static int Sixes(int[] dice) { return CalculateArray(dice, 6); } private static int CalculateArray(IEnumerable arr, int compareValue) { return arr.Count(die => die == compareValue) * compareValue; } public static int TwoPair(int[] dice) { var set = GetDuplicates(dice, 2); if (set.Count == 2) return set.Sum() * 2; return 0; } public static int ScorePair(int[] dice) { return GetNumberOfaKind(dice, 2); } public static int ThreeOfAKind(int[] dice) { return GetNumberOfaKind(dice, 3); } public static int FourOfAKind(int[] dice) { return GetNumberOfaKind(dice, 4); } private static int GetNumberOfaKind(int[] dice, int n) { var set = GetDuplicates(dice, n); if (set.Count > 0) return set.Max() * n; return 0; } public static HashSet GetDuplicates(int[] dice, int a) { var encountered = new int[6]; var set = new HashSet(); foreach (var die in dice) { encountered[die - 1]++; } for (var i = 0; i < 6; i++) { if (encountered[i] == a) { set.Add(i + 1); } } return set; } public static int SmallStraight(int[] dice) { return Straight(dice, false); } public static int LargeStraight(int[] dice) { return Straight(dice, true); } private static int Straight(int[] dice, bool largeStraight) { Array.Sort(dice); var currentStraightValue = (largeStraight) ? 2 : 1; return !dice.Any(die => die != currentStraightValue++) ? dice.Sum() : 0; } public static int FullHouse(int[] dice) { var pair = GetDuplicates(dice, 2); var threeOfAKind = GetDuplicates(dice, 3); if (pair.Count != 0 && threeOfAKind.Count != 0) return pair.First() * 2 + threeOfAKind.First() * 3; return 0; } } } using Kata; using NUnit.Framework; namespace TestYahtzee2 { [TestFixture] public class TestYahtzee { [Test] public void TestChanceSum() { int expected = 15; int actual = Yahtzee.Chance(new[] { 2, 3, 4, 5, 1 }); Assert.AreEqual(expected, actual); Assert.AreEqual(16, Yahtzee.Chance(new[] { 3, 3, 4, 5, 1 })); } [Test] public void TestFives() { Assert.AreEqual(10, Yahtzee.Fives(new[] { 4, 4, 4, 5, 5 })); Assert.AreEqual(15, Yahtzee.Fives(new[] { 4, 4, 5, 5, 5 })); Assert.AreEqual(20, Yahtzee.Fives(new[] { 4, 5, 5, 5, 5 })); } [Test] public void TestFours() { Assert.AreEqual(12, Yahtzee.Fours(new[] { 4, 4, 4, 5, 5 })); Assert.AreEqual(8, Yahtzee.Fours(new[] { 4, 4, 5, 5, 5 })); Assert.AreEqual(4, Yahtzee.Fours(new[] { 4, 5, 5, 5, 5 })); } [Test] public void TestFullHouse() { Assert.AreEqual(18, Yahtzee.FullHouse(new[] { 6, 2, 2, 2, 6 })); Assert.AreEqual(0, Yahtzee.FullHouse(new[] { 2, 3, 4, 5, 6 })); } [Test] public void TestLargeStraight() { Assert.AreEqual(20, Yahtzee.LargeStraight(new[] { 6, 2, 3, 4, 5 })); Assert.AreEqual(20, Yahtzee.LargeStraight(new[] { 2, 3, 4, 5, 6 })); Assert.AreEqual(0, Yahtzee.LargeStraight(new[] { 1, 2, 2, 4, 5 })); } [Test] public void TestOnePair() { Assert.AreEqual(6, Yahtzee.ScorePair(new[] { 3, 4, 3, 5, 6 })); Assert.AreEqual(10, Yahtzee.ScorePair(new[] { 5, 3, 3, 3, 5 })); Assert.AreEqual(12, Yahtzee.ScorePair(new[] { 5, 3, 6, 6, 5 })); } [Test] public void TestOnes() { Assert.IsTrue(Yahtzee.Ones(new[] { 1, 2, 3, 4, 5 }) == 1); Assert.AreEqual(2, Yahtzee.Ones(new[] { 1, 2, 1, 4, 5 })); Assert.AreEqual(0, Yahtzee.Ones(new[] { 6, 2, 2, 4, 5 })); Assert.AreEqual(4, Yahtzee.Ones(new[] { 1, 2, 1, 1, 1 })); } [Test] public void TestSixes() { Assert.AreEqual(0, Yahtzee.Sixes(new[] { 4, 4, 4, 5, 5 })); Assert.AreEqual(6, Yahtzee.Sixes(new[] { 4, 4, 6, 5, 5 })); Assert.AreEqual(18, Yahtzee.Sixes(new[] { 6, 5, 6, 6, 5 })); } [Test] public void TestSmallStraight() { Assert.AreEqual(15, Yahtzee.SmallStraight(new[] { 1, 2, 3, 4, 5 })); Assert.AreEqual(15, Yahtzee.SmallStraight(new[] { 2, 3, 4, 5, 1 })); Assert.AreEqual(0, Yahtzee.SmallStraight(new[] { 1, 2, 2, 4, 5 })); } [Test] public void TestThreeOfAKind() { Assert.AreEqual(9, Yahtzee.ThreeOfAKind(new[] { 3, 3, 3, 4, 5 })); Assert.AreEqual(15, Yahtzee.ThreeOfAKind(new[] { 5, 3, 5, 4, 5 })); Assert.AreEqual(0, Yahtzee.ThreeOfAKind(new[] { 3, 3, 3, 3, 5 })); } [Test] public void TestThrees() { Assert.AreEqual(6, Yahtzee.Threes(new[] { 1, 2, 3, 2, 3 })); Assert.AreEqual(12, Yahtzee.Threes(new[] { 2, 3, 3, 3, 3 })); } [Test] public void TestTwoPair() { Assert.AreEqual(16, Yahtzee.TwoPair(new[] { 3, 3, 5, 4, 5 })); Assert.AreEqual(0, Yahtzee.TwoPair(new[] { 3, 3, 5, 5, 5 })); } [Test] public void TestTwos() { Assert.AreEqual(4, Yahtzee.Twos(new[] { 1, 2, 3, 2, 6 })); Assert.AreEqual(10, Yahtzee.Twos(new[] { 2, 2, 2, 2, 2 })); } [Test] public void TestfourOfAKind() { Assert.AreEqual(12, Yahtzee.FourOfAKind(new[] { 3, 3, 3, 3, 5 })); Assert.AreEqual(20, Yahtzee.FourOfAKind(new[] { 5, 5, 5, 4, 5 })); Assert.AreEqual(0, Yahtzee.FourOfAKind(new[] { 3, 3, 3, 3, 3 })); } [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)); } } }