From d7053757a967c7bf05b441c2350792ed91e06a36 Mon Sep 17 00:00:00 2001 From: Aziz Date: Fri, 12 Jan 2024 08:36:12 +0100 Subject: [PATCH] Aziz Zafar --- exercise.main/Core.cs | 70 ++++++++++++++++++++++++++++++------- exercise.main/Extension.cs | 30 +++++++++++++--- exercise.tests/CoreTests.cs | 2 -- 3 files changed, 83 insertions(+), 19 deletions(-) diff --git a/exercise.main/Core.cs b/exercise.main/Core.cs index 7568220..7b28083 100644 --- a/exercise.main/Core.cs +++ b/exercise.main/Core.cs @@ -1,32 +1,76 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Reflection; -using System.Runtime.InteropServices; -using System.Text; -using System.Threading.Tasks; 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); - + if (hand == null || !hand.Any()) + return false; - + Dictionary cardCounts = new Dictionary(); + List> winningPairs = new List>(); - return result.Item1!=string.Empty ? true : false; + foreach (var card in hand) + { + string cardValue1 = card.Item1; + string cardValue2 = card.Item2; + + // Check for a winning pair with cardValue1 + if (cardCounts.ContainsKey(cardValue1)) + { + cardCounts[cardValue1]++; + if (cardCounts[cardValue1] == 2) + { + winningPairs.Add(card); + } + } + else + { + cardCounts[cardValue1] = 1; + } + + // Check for a winning pair with cardValue2 + if (cardCounts.ContainsKey(cardValue2)) + { + cardCounts[cardValue2]++; + if (cardCounts[cardValue2] == 2) + { + winningPairs.Add(card); + } + } + else + { + cardCounts[cardValue2] = 1; + } + } + + if (winningPairs.Any()) + { + // Find the pair with the highest value + result = winningPairs.OrderByDescending(pair => GetValueOfCard(pair.Item1)).First(); + return true; + } + + return false; } + public int GetValueOfCard(string card) { - return 0; + Dictionary cardValues = new Dictionary + { + {"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} + }; + + // Use the TryGetValue method to get the value of the card + return cardValues.TryGetValue(card, out int value) ? value : 0; } } } diff --git a/exercise.main/Extension.cs b/exercise.main/Extension.cs index b33ba1f..eeaa6f6 100644 --- a/exercise.main/Extension.cs +++ b/exercise.main/Extension.cs @@ -1,20 +1,42 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace exercise.main { public class Extension { //TODO: complete the following method, keeping the signature the same - public bool winningThree(IEnumerable> hand, out Tuple result) + public bool WinningThree(IEnumerable> hand, out Tuple result) { result = new Tuple(string.Empty, string.Empty, string.Empty); + // Dictionary to store the count of each card value + Dictionary cardCounts = new Dictionary(); + + // Iterate through the hand and count occurrences of each card value + foreach (var card in hand) + { + string cardValue = card.Item1; + if (cardCounts.ContainsKey(cardValue)) + { + cardCounts[cardValue]++; + // Check for a winning triplet + if (cardCounts[cardValue] == 3) + { + result = card; + Console.WriteLine($"Found a winning triplet: {card.Item1} {card.Item2} {card.Item3}"); + return true; + } + } + else + { + cardCounts[cardValue] = 1; + } + } + + Console.WriteLine("No winning triplet found."); return false; } - } } diff --git a/exercise.tests/CoreTests.cs b/exercise.tests/CoreTests.cs index 5adefb3..669e7b2 100644 --- a/exercise.tests/CoreTests.cs +++ b/exercise.tests/CoreTests.cs @@ -55,8 +55,6 @@ public void Scenario1b() [Test] public void Scenario2() { - - Core core = new Core(); List> hand = new List>