From d4f49fae84873979da8574b6a20d3aa9d4b414b2 Mon Sep 17 00:00:00 2001 From: Mateusz Pietrzak Date: Mon, 15 Jan 2024 08:25:46 +0100 Subject: [PATCH 1/2] Completed core tasks --- exercise.main/Core.cs | 64 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 53 insertions(+), 11 deletions(-) diff --git a/exercise.main/Core.cs b/exercise.main/Core.cs index 7568220..e1142e0 100644 --- a/exercise.main/Core.cs +++ b/exercise.main/Core.cs @@ -1,8 +1,6 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Reflection; -using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; @@ -10,23 +8,67 @@ namespace exercise.main { 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); - + result = new Tuple(string.Empty, string.Empty); - + Dictionary cardValues = new Dictionary(); + bool hasPair = false; - + foreach (var pair in hand) + { + string card1 = pair.Item1; + string card2 = pair.Item2; - return result.Item1!=string.Empty ? true : false; + int value1 = GetValueOfCard(card1); + int value2 = GetValueOfCard(card2); + + if (value1 == value2) + { + hasPair = true; + + if (!cardValues.ContainsKey(card1)) + { + cardValues[card1] = value1; + } + + if (!cardValues.ContainsKey(card2)) + { + cardValues[card2] = value2; + } + } + } + + if (hasPair) + { + var highestPair = cardValues.OrderByDescending(x => x.Value).FirstOrDefault(); + + result = new Tuple(highestPair.Key, highestPair.Key); + return true; + } + + return false; } + public int GetValueOfCard(string card) { - return 0; + switch (card) + { + case "J": + return 11; + case "Q": + return 12; + case "K": + return 13; + case "A": + return 14; + default: + if (int.TryParse(card, out int value)) + { + return value; + } + return 0; + } } } } From 81ec709f6ada92dd2197d05ca5c22bc21aec7e36 Mon Sep 17 00:00:00 2001 From: Mateusz Pietrzak Date: Sun, 4 Feb 2024 14:02:49 +0100 Subject: [PATCH 2/2] Completed extension tasks --- exercise.main/Extension.cs | 65 ++++++++++++++++++++++++++- exercise.tests/ExtensionTests.cs | 76 ++++++++++++++++++++++++++++++++ 2 files changed, 139 insertions(+), 2 deletions(-) create mode 100644 exercise.tests/ExtensionTests.cs diff --git a/exercise.main/Extension.cs b/exercise.main/Extension.cs index b33ba1f..5ad15e8 100644 --- a/exercise.main/Extension.cs +++ b/exercise.main/Extension.cs @@ -1,8 +1,6 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace exercise.main { @@ -13,8 +11,71 @@ public bool winningThree(IEnumerable> hand, out Tu { result = new Tuple(string.Empty, string.Empty, string.Empty); + Dictionary cardValues = new Dictionary(); + bool hasTriplet = false; + + foreach (var triplet in hand) + { + string card1 = triplet.Item1; + string card2 = triplet.Item2; + string card3 = triplet.Item3; + + int value1 = GetValueOfCard(card1); + int value2 = GetValueOfCard(card2); + int value3 = GetValueOfCard(card3); + + if (value1 == value2 && value2 == value3) + { + hasTriplet = true; + + if (!cardValues.ContainsKey(card1)) + { + cardValues[card1] = value1; + } + + // Add the other cards to the dictionary if not present + if (!cardValues.ContainsKey(card2)) + { + cardValues[card2] = value2; + } + + if (!cardValues.ContainsKey(card3)) + { + cardValues[card3] = value3; + } + } + } + + if (hasTriplet) + { + var highestTriplet = cardValues.OrderByDescending(x => x.Value).FirstOrDefault(); + + result = new Tuple(highestTriplet.Key, highestTriplet.Key, highestTriplet.Key); + return true; + } + return false; } + public int GetValueOfCard(string card) + { + switch (card) + { + case "J": + return 11; + case "Q": + return 12; + case "K": + return 13; + case "A": + return 14; + default: + if (int.TryParse(card, out int value)) + { + return value; + } + return 0; + } + } } } diff --git a/exercise.tests/ExtensionTests.cs b/exercise.tests/ExtensionTests.cs new file mode 100644 index 0000000..1f24f30 --- /dev/null +++ b/exercise.tests/ExtensionTests.cs @@ -0,0 +1,76 @@ +using exercise.main; +using NUnit.Framework; +using System; +using System.Collections.Generic; + +namespace exercise.tests +{ + [TestFixture] + public class ExtensionTests + { + [Test] + public void WinningThree_Scenario1() + { + Extension extension = new Extension(); + + List> hand = new List> + { + new Tuple("K", "5", "3"), + new Tuple("3", "7", "A") + }; + + 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)); + Assert.That(winner.Item3, Is.EqualTo(String.Empty)); + } + + [Test] + public void WinningThree_Scenario2() + { + Extension extension = new Extension(); + + List> hand = new List> + { + new Tuple("K", "5", "2"), + new Tuple("2", "2", "7") + }; + + 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)); + Assert.That(winner.Item3, Is.EqualTo(String.Empty)); + } + + [Test] + public void WinningThree_Scenario3() + { + Extension extension = new Extension(); + + List> hand = new List> + { + new Tuple("4", "3", "A"), + new Tuple("6", "6", "6"), + new Tuple("7", "7", "K"), + new Tuple("3", "3", "3") + }; + + Tuple winner; + + bool result = extension.winningThree(hand, out winner); + + Assert.That(result, Is.True); + Assert.That(winner.Item1, Is.EqualTo("6")); + Assert.That(winner.Item2, Is.EqualTo("6")); + Assert.That(winner.Item3, Is.EqualTo("6")); + } + } +}