From dfc154c13196ce7fc3aed66d513aee41bf942a6d Mon Sep 17 00:00:00 2001 From: Aleksander Kolsrud Date: Fri, 12 Jan 2024 11:37:23 +0100 Subject: [PATCH] Aleksander Kolsrud C#ex3 --- exercise.main/Core.cs | 30 ++++++++++--- exercise.main/Extension.cs | 33 +++++++++++++- exercise.tests/ExtensionTests.cs | 75 ++++++++++++++++++++++++++++++++ 3 files changed, 132 insertions(+), 6 deletions(-) create mode 100644 exercise.tests/ExtensionTests.cs diff --git a/exercise.main/Core.cs b/exercise.main/Core.cs index 7568220..1054a49 100644 --- a/exercise.main/Core.cs +++ b/exercise.main/Core.cs @@ -12,21 +12,41 @@ public class Core { + //TODO: complete the following method, keeping the signature the same public bool winningPair(IEnumerable> hand, out Tuple result) { result = new Tuple(string.Empty, string.Empty); - - - - + foreach (var pair in hand) + { + if (pair.Item1 == pair.Item2) + { + if (result.Item1 != string.Empty && GetValueOfCard(pair.Item1) > GetValueOfCard(result.Item1)) + { + result = pair; + } + else if (result.Item1 == string.Empty) + { + result = pair; + } + } + } return result.Item1!=string.Empty ? true : false; } public int GetValueOfCard(string card) { - return 0; + Dictionary _cv = new Dictionary() + { + {"1", 1}, {"2", 2}, {"3", 3},{"4", 4}, {"5", 5}, {"6", 6}, + {"7", 7}, {"8", 8}, {"9", 9}, {"10", 10}, {"J", 11}, {"Q", 12}, + {"K", 13}, {"A", 14} + }; + + return _cv[card]; + + } } } diff --git a/exercise.main/Extension.cs b/exercise.main/Extension.cs index b33ba1f..6a69e64 100644 --- a/exercise.main/Extension.cs +++ b/exercise.main/Extension.cs @@ -13,8 +13,39 @@ public bool winningThree(IEnumerable> hand, out Tu { result = new Tuple(string.Empty, string.Empty, string.Empty); - return false; + + foreach (var cards in hand) + { + if (cards.Item1 == cards.Item2 && cards.Item2 == cards.Item3) + { + if (result.Item1 != string.Empty && GetValueOfCard(cards.Item1) > GetValueOfCard(result.Item1)) + { + result = cards; + } + else if (result.Item1 == string.Empty) + { + result = cards; + } + } + } + + + return result.Item1 != string.Empty ? true : false; } + public int GetValueOfCard(string card) + { + Dictionary _cv = new Dictionary() + { + {"1", 1}, {"2", 2}, {"3", 3},{"4", 4}, {"5", 5}, {"6", 6}, + {"7", 7}, {"8", 8}, {"9", 9}, {"10", 10}, {"J", 11}, {"Q", 12}, + {"K", 13}, {"A", 14} + }; + + return _cv[card]; + + + } + } } diff --git a/exercise.tests/ExtensionTests.cs b/exercise.tests/ExtensionTests.cs new file mode 100644 index 0000000..0f191e5 --- /dev/null +++ b/exercise.tests/ExtensionTests.cs @@ -0,0 +1,75 @@ +using exercise.main; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace exercise.tests +{ + internal class ExtensionTests + { + [Order(0)] + [Test] + public void Scenario1() + { + + + Extension extension = new Extension(); + + List> hand = new List> + { + new Tuple("K", "5", "Q"), + new Tuple("3","7", "Q") + }; + Tuple winner; + + bool result = extension.winningThree(hand, out winner); + + Assert.That(result, Is.False); + Assert.That(winner.Item1, Is.EqualTo(String.Empty)); + Assert.That(winner.Item2, Is.EqualTo(String.Empty)); + } + //("K","K","K"), ("A","A","A")} => true out ("A","A","A") + [Test] + public void Scenario2() + { + + + Extension extension = new Extension(); + + List> hand = new List> + { + new Tuple("K", "K", "K"), + new Tuple("A","A", "A") + }; + Tuple winner; + bool result = extension.winningThree(hand, out winner); + + Assert.That(result, Is.True); + + Assert.IsTrue(winner.Item1 == "A" && winner.Item2 == "A" && winner.Item3 == "A"); + + } + //{("4", "3", "2"),("6","6","6"),("7","7","7"),("3","3","3")} => true ("7", "7", "7") + [Test] + public void Scenario3() + { + Extension extension = new Extension(); + + List> hand = new List> + { + new Tuple("4", "3", "2"), + new Tuple("6", "6", "6"), + new Tuple("7", "7", "7"), + new Tuple("3", "3", "3") + }; + Tuple winner; + bool result = extension.winningThree(hand, out winner); + + Assert.That(result, Is.True); + + Assert.IsTrue(winner.Item1 == "7" && winner.Item2 == "7" && winner.Item3 == "7"); + } + } +} \ No newline at end of file