Advertisement
Guest User

Yahtzee Refactored

a guest
Oct 25th, 2012
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.80 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Kata
  6. {
  7.     public class Yahtzee
  8.     {
  9.         public static int Chance(int[] dice)
  10.         {
  11.             return dice.Sum();
  12.         }
  13.  
  14.         public static int yahtzee(params int[] dice)
  15.         {
  16.             if (dice.Any(die => die != dice[0]))
  17.             {
  18.                 return 0;
  19.             }
  20.             return 50;
  21.         }
  22.  
  23.         public static int Ones(int[] dice)
  24.         {
  25.             return CalculateArray(dice, 1);
  26.         }
  27.  
  28.         public static int Twos(int[] dice)
  29.         {
  30.             return CalculateArray(dice, 2);
  31.         }
  32.  
  33.         public static int Threes(int[] dice)
  34.         {
  35.             return CalculateArray(dice, 3);
  36.         }
  37.  
  38.         public static int Fours(int[] dice)
  39.         {
  40.             return CalculateArray(dice, 4);
  41.         }
  42.  
  43.         public static int Fives(int[] dice)
  44.         {
  45.             return CalculateArray(dice, 5);
  46.         }
  47.  
  48.         public static int Sixes(int[] dice)
  49.         {
  50.             return CalculateArray(dice, 6);
  51.         }
  52.  
  53.         private static int CalculateArray(IEnumerable<int> arr, int compareValue)
  54.         {
  55.             return arr.Count(die => die == compareValue) * compareValue;
  56.         }
  57.  
  58.         public static int TwoPair(int[] dice)
  59.         {
  60.             var set = GetDuplicates(dice, 2);
  61.             if (set.Count == 2)
  62.                 return set.Sum() * 2;
  63.             return 0;
  64.         }
  65.  
  66.         public static int ScorePair(int[] dice)
  67.         {
  68.             return GetNumberOfaKind(dice, 2);
  69.         }
  70.  
  71.         public static int ThreeOfAKind(int[] dice)
  72.         {
  73.             return GetNumberOfaKind(dice, 3);
  74.         }
  75.  
  76.         public static int FourOfAKind(int[] dice)
  77.         {
  78.             return GetNumberOfaKind(dice, 4);
  79.         }
  80.  
  81.         private static int GetNumberOfaKind(int[] dice, int n)
  82.         {
  83.             var set = GetDuplicates(dice, n);
  84.             if (set.Count > 0)
  85.                 return set.Max() * n;
  86.             return 0;
  87.         }
  88.  
  89.         public static HashSet<int> GetDuplicates(int[] dice, int a)
  90.         {
  91.             var encountered = new int[6];
  92.             var set = new HashSet<int>();
  93.  
  94.             foreach (var die in dice)
  95.             {
  96.                 encountered[die - 1]++;
  97.             }
  98.  
  99.             for (var i = 0; i < 6; i++)
  100.             {
  101.                 if (encountered[i] == a)
  102.                 {
  103.                     set.Add(i + 1);
  104.                 }
  105.             }
  106.  
  107.             return set;
  108.         }
  109.  
  110.         public static int SmallStraight(int[] dice)
  111.         {
  112.             return Straight(dice, false);
  113.         }
  114.  
  115.         public static int LargeStraight(int[] dice)
  116.         {
  117.             return Straight(dice, true);
  118.         }
  119.  
  120.         private static int Straight(int[] dice, bool largeStraight)
  121.         {
  122.             Array.Sort(dice);
  123.             var currentStraightValue = (largeStraight) ? 2 : 1;
  124.             return !dice.Any(die => die != currentStraightValue++) ? dice.Sum() : 0;
  125.         }
  126.  
  127.         public static int FullHouse(int[] dice)
  128.         {
  129.             var pair = GetDuplicates(dice, 2);
  130.             var threeOfAKind = GetDuplicates(dice, 3);
  131.  
  132.             if (pair.Count != 0 && threeOfAKind.Count != 0)
  133.                 return pair.First() * 2 + threeOfAKind.First() * 3;
  134.             return 0;
  135.         }
  136.     }
  137. }
  138.  
  139.  
  140.  
  141. using Kata;
  142. using NUnit.Framework;
  143.  
  144. namespace TestYahtzee2
  145. {
  146.     [TestFixture]
  147.     public class TestYahtzee
  148.     {
  149.         [Test]
  150.         public void TestChanceSum()
  151.         {
  152.             int expected = 15;
  153.             int actual = Yahtzee.Chance(new[] { 2, 3, 4, 5, 1 });
  154.             Assert.AreEqual(expected, actual);
  155.             Assert.AreEqual(16, Yahtzee.Chance(new[] { 3, 3, 4, 5, 1 }));
  156.         }
  157.  
  158.         [Test]
  159.         public void TestFives()
  160.         {
  161.             Assert.AreEqual(10, Yahtzee.Fives(new[] { 4, 4, 4, 5, 5 }));
  162.             Assert.AreEqual(15, Yahtzee.Fives(new[] { 4, 4, 5, 5, 5 }));
  163.             Assert.AreEqual(20, Yahtzee.Fives(new[] { 4, 5, 5, 5, 5 }));
  164.         }
  165.  
  166.         [Test]
  167.         public void TestFours()
  168.         {
  169.             Assert.AreEqual(12, Yahtzee.Fours(new[] { 4, 4, 4, 5, 5 }));
  170.             Assert.AreEqual(8, Yahtzee.Fours(new[] { 4, 4, 5, 5, 5 }));
  171.             Assert.AreEqual(4, Yahtzee.Fours(new[] { 4, 5, 5, 5, 5 }));
  172.         }
  173.  
  174.         [Test]
  175.         public void TestFullHouse()
  176.         {
  177.             Assert.AreEqual(18, Yahtzee.FullHouse(new[] { 6, 2, 2, 2, 6 }));
  178.             Assert.AreEqual(0, Yahtzee.FullHouse(new[] { 2, 3, 4, 5, 6 }));
  179.         }
  180.  
  181.         [Test]
  182.         public void TestLargeStraight()
  183.         {
  184.             Assert.AreEqual(20, Yahtzee.LargeStraight(new[] { 6, 2, 3, 4, 5 }));
  185.             Assert.AreEqual(20, Yahtzee.LargeStraight(new[] { 2, 3, 4, 5, 6 }));
  186.             Assert.AreEqual(0, Yahtzee.LargeStraight(new[] { 1, 2, 2, 4, 5 }));
  187.         }
  188.  
  189.  
  190.         [Test]
  191.         public void TestOnePair()
  192.         {
  193.             Assert.AreEqual(6, Yahtzee.ScorePair(new[] { 3, 4, 3, 5, 6 }));
  194.             Assert.AreEqual(10, Yahtzee.ScorePair(new[] { 5, 3, 3, 3, 5 }));
  195.             Assert.AreEqual(12, Yahtzee.ScorePair(new[] { 5, 3, 6, 6, 5 }));
  196.         }
  197.  
  198.         [Test]
  199.         public void TestOnes()
  200.         {
  201.             Assert.IsTrue(Yahtzee.Ones(new[] { 1, 2, 3, 4, 5 }) == 1);
  202.             Assert.AreEqual(2, Yahtzee.Ones(new[] { 1, 2, 1, 4, 5 }));
  203.             Assert.AreEqual(0, Yahtzee.Ones(new[] { 6, 2, 2, 4, 5 }));
  204.             Assert.AreEqual(4, Yahtzee.Ones(new[] { 1, 2, 1, 1, 1 }));
  205.         }
  206.  
  207.         [Test]
  208.         public void TestSixes()
  209.         {
  210.             Assert.AreEqual(0, Yahtzee.Sixes(new[] { 4, 4, 4, 5, 5 }));
  211.             Assert.AreEqual(6, Yahtzee.Sixes(new[] { 4, 4, 6, 5, 5 }));
  212.             Assert.AreEqual(18, Yahtzee.Sixes(new[] { 6, 5, 6, 6, 5 }));
  213.         }
  214.  
  215.         [Test]
  216.         public void TestSmallStraight()
  217.         {
  218.             Assert.AreEqual(15, Yahtzee.SmallStraight(new[] { 1, 2, 3, 4, 5 }));
  219.             Assert.AreEqual(15, Yahtzee.SmallStraight(new[] { 2, 3, 4, 5, 1 }));
  220.             Assert.AreEqual(0, Yahtzee.SmallStraight(new[] { 1, 2, 2, 4, 5 }));
  221.         }
  222.  
  223.         [Test]
  224.         public void TestThreeOfAKind()
  225.         {
  226.             Assert.AreEqual(9, Yahtzee.ThreeOfAKind(new[] { 3, 3, 3, 4, 5 }));
  227.             Assert.AreEqual(15, Yahtzee.ThreeOfAKind(new[] { 5, 3, 5, 4, 5 }));
  228.             Assert.AreEqual(0, Yahtzee.ThreeOfAKind(new[] { 3, 3, 3, 3, 5 }));
  229.         }
  230.  
  231.         [Test]
  232.         public void TestThrees()
  233.         {
  234.             Assert.AreEqual(6, Yahtzee.Threes(new[] { 1, 2, 3, 2, 3 }));
  235.             Assert.AreEqual(12, Yahtzee.Threes(new[] { 2, 3, 3, 3, 3 }));
  236.         }
  237.  
  238.         [Test]
  239.         public void TestTwoPair()
  240.         {
  241.             Assert.AreEqual(16, Yahtzee.TwoPair(new[] { 3, 3, 5, 4, 5 }));
  242.             Assert.AreEqual(0, Yahtzee.TwoPair(new[] { 3, 3, 5, 5, 5 }));
  243.         }
  244.  
  245.         [Test]
  246.         public void TestTwos()
  247.         {
  248.             Assert.AreEqual(4, Yahtzee.Twos(new[] { 1, 2, 3, 2, 6 }));
  249.             Assert.AreEqual(10, Yahtzee.Twos(new[] { 2, 2, 2, 2, 2 }));
  250.         }
  251.  
  252.         [Test]
  253.         public void TestfourOfAKind()
  254.         {
  255.             Assert.AreEqual(12, Yahtzee.FourOfAKind(new[] { 3, 3, 3, 3, 5 }));
  256.             Assert.AreEqual(20, Yahtzee.FourOfAKind(new[] { 5, 5, 5, 4, 5 }));
  257.             Assert.AreEqual(0, Yahtzee.FourOfAKind(new[] { 3, 3, 3, 3, 3 }));
  258.         }
  259.  
  260.         [Test]
  261.         public void Yahtzee_scores_50()
  262.         {
  263.             int expected = 50;
  264.             int actual = Yahtzee.yahtzee(4, 4, 4, 4, 4);
  265.             Assert.AreEqual(expected, actual);
  266.             Assert.AreEqual(50, Yahtzee.yahtzee(6, 6, 6, 6, 6));
  267.             Assert.AreEqual(0, Yahtzee.yahtzee(6, 6, 6, 6, 3));
  268.         }
  269.     }
  270. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement