From 7c74faabd9b8606ae6ae4dd0602b0d1c41b95144 Mon Sep 17 00:00:00 2001 From: Rose Breedveld Date: Fri, 9 Feb 2024 16:19:57 +0100 Subject: [PATCH] Rose Breedveld - fundamentals-Poker core and basic Extensions passed --- exercise.main/Core.cs | 82 ++++++++++++++++++-- exercise.main/Extension.cs | 80 +++++++++++++++++++- exercise.tests/CoreTests.cs | 17 +++-- exercise.tests/ExtensionTests.cs | 108 +++++++++++++++++++++++++++ exercise.tests/exercise.tests.csproj | 8 ++ 5 files changed, 281 insertions(+), 14 deletions(-) create mode 100644 exercise.tests/ExtensionTests.cs diff --git a/exercise.main/Core.cs b/exercise.main/Core.cs index 7568220..4b4d13c 100644 --- a/exercise.main/Core.cs +++ b/exercise.main/Core.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; using System.Linq; using System.Reflection; using System.Runtime.InteropServices; @@ -8,24 +9,91 @@ namespace exercise.main { - public class Core - { - - + public class Core { + + + //public string card; + public 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 } + }; + //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); - - + // create a list to store the hand with two same values as winning hand + List < Tuple> winningHand = new List>(); - + // to store the int value of the card + List winningPairValues = new List(); + + // find the pair in the parameter given hands (given in the tests). + foreach(Tuple pair in hand) + { + if ( pair.Item1 == pair.Item2) + { + //add the winning hand to the List with a new Tuple > winningHand + // search in hands, and find a match of the same cards, if there is a pair store it in winningHands. If there + // isnt a pair, return string.empty + winningHand.Add(pair); + } + } + + if(winningHand.Count == 1) + { + // result is the first value of index of winningHand + result = winningHand[0]; + // if there are more pairs in hands + } else if (winningHand.Count > 1) + { + // then check/loop each winningHand what pairs + + /*for(int i = 0; i < winningHand.Count; i++) + { + // use the method getvalueofcard to check each value of the card in that pair. + // store at the same index of winningPairValues the winningHand item1 + // Tuple tuple = winningHand[i]; + */ + foreach (var cards in winningHand) + { + winningPairValues.Add(GetValueOfCard(cards.Item1)); + } + + int winningIndex = winningPairValues.IndexOf(winningPairValues.Max()); + + // what pair contains the highest value pair? + result = winningHand[winningIndex]; + } return result.Item1!=string.Empty ? true : false; + } + public int GetValueOfCard(string card) { + // get value of card out of cardValues + // key value pairs in cardValues to check the highest value of cards\ + // card toString() ? + + if (cardValues.ContainsKey(card.ToUpper())) + { + return cardValues[card.ToUpper()]; + } + return 0; } } diff --git a/exercise.main/Extension.cs b/exercise.main/Extension.cs index b33ba1f..4c4a1f4 100644 --- a/exercise.main/Extension.cs +++ b/exercise.main/Extension.cs @@ -8,13 +8,91 @@ namespace exercise.main { public class Extension { + //public string card; + public 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 } + }; //TODO: complete the following method, keeping the signature the same public bool winningThree(IEnumerable> hand, out Tuple result) { result = new Tuple(string.Empty, string.Empty, string.Empty); - return false; + // create a list to store the hand with two same values as winning hand + List> winningHand = new List>(); + + // to store the int value of the card + List winningPairValues = new List(); + + // find the pair in the parameter given hands (given in the tests). + foreach (Tuple pairs in hand) + { + if (pairs.Item1 == pairs.Item2 && pairs.Item2 == pairs.Item3) + { + //add the winning hand to the List with a new Tuple > winningHand + // search in hands, and find a match of the same cards, if there is a pair store it in winningHands. If there + // isnt a pair, return string.empty + winningHand.Add(pairs); + } + } + + if (winningHand.Count == 1) + { + // result is the first value of index of winningHand + result = winningHand[0]; + // if there are more pairs in hands + } + else if (winningHand.Count > 1) + { + // then check/loop each winningHand what pairs + + /*for(int i = 0; i < winningHand.Count; i++) + { + // use the method getvalueofcard to check each value of the card in that pair. + // store at the same index of winningPairValues the winningHand item1 + // Tuple tuple = winningHand[i]; + */ + foreach (var cards in winningHand) + { + winningPairValues.Add(GetValueOfCard(cards.Item1)); + } + + int winningIndex = winningPairValues.IndexOf(winningPairValues.Max()); + + // what pair contains the highest value pair? + result = winningHand[winningIndex]; + } + + return result.Item1 != string.Empty ? true : false; + // return false; + } + public int GetValueOfCard(string card) + { + // get value of card out of cardValues + // key value pairs in cardValues to check the highest value of cards\ + // card toString() ? + + if (cardValues.ContainsKey(card.ToUpper())) + { + return cardValues[card.ToUpper()]; + } + + return 0; + } } } + diff --git a/exercise.tests/CoreTests.cs b/exercise.tests/CoreTests.cs index 5adefb3..67097c3 100644 --- a/exercise.tests/CoreTests.cs +++ b/exercise.tests/CoreTests.cs @@ -18,7 +18,7 @@ public void Scenario1() { new Tuple("K", "5"), new Tuple("3","7") - }; + }; Tuple winner; bool result = core.winningPair(hand, out winner); @@ -29,6 +29,7 @@ public void Scenario1() } + [Test] //{("K","5"),("2","2")} => true out ("2","2") public void Scenario1b() { @@ -38,25 +39,25 @@ public void Scenario1b() List> hand = new List> { - new Tuple("K", "5"), + new Tuple("K","5"), new Tuple("2","2") }; Tuple winner; bool result = core.winningPair(hand, out winner); - Assert.That(result, Is.False); + Assert.That(result, Is.True); Assert.That(winner.Item1, Is.EqualTo("2")); Assert.That(winner.Item2, Is.EqualTo("2")); } + + //("K","K"), ("A","A")} => true out ("A","A") [Test] public void Scenario2() { - - Core core = new Core(); List> hand = new List> @@ -64,6 +65,7 @@ public void Scenario2() new Tuple("K", "K"), new Tuple("A","A") }; + Tuple winner; bool result = core.winningPair(hand, out winner); @@ -72,9 +74,11 @@ public void Scenario2() Assert.IsTrue(winner.Item1=="A" && winner.Item2=="A"); } + + //{("4", "3"),("6","6"),("7","7"),("3","3")} => true ("7", "7") [Test] - public void Scenario3() + public void Scenario3() { Core core = new Core(); @@ -93,6 +97,7 @@ public void Scenario3() Assert.IsTrue(winner.Item1 == "7" && winner.Item2 == "7"); } + [TestCase("2",2)] [TestCase("3",3)] diff --git a/exercise.tests/ExtensionTests.cs b/exercise.tests/ExtensionTests.cs new file mode 100644 index 0000000..a0bba6f --- /dev/null +++ b/exercise.tests/ExtensionTests.cs @@ -0,0 +1,108 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace exercise.tests +{ + class ExtensionTest + { + + //{("K","5","8"),("3","7","8")} => false out ("","","") + [Test] + public void ScenarioE1() + { + + + Extension extension = new Extension(); + + List> hand = new List> + { + new Tuple("K", "5", "8"), + new Tuple("3","7", "8"), + new Tuple("2","8", "8") + }; + 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)); + + } + + //{("K","5","J"), ("J","A","J"),("2","2", "2")} => true out ("2","2","2") + [Test] + public void ScenarioEb() + { + + Extension extension = new Extension(); + + List> hand = new List> + + { + new Tuple("K", "5","J"), + new Tuple("J","A","J"), + new Tuple("2","2","2") + }; + + Tuple winner; + + bool result = extension.winningThree(hand, out winner); + + Assert.That(result, Is.True); + Assert.That(winner.Item1, Is.EqualTo("2")); + Assert.That(winner.Item2, Is.EqualTo("2")); + Assert.That(winner.Item3, Is.EqualTo("2")); + } + + //("J","J","J"), ("K","K","K"), ("A","A","A")} => true out ("A","A","A") + [Test] + public void ScenarioE2() + { + Extension extension = new Extension(); + + List> hand = new List> + + { + new Tuple("J","J","J"), + 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 ScenarioE3() + { + Extension extension = new Extension(); + + List> hand = new List> + + { + new Tuple< string, string, string >("4", "3", "2"), + new Tuple< string, string, string >("6", "6", "6"), + new Tuple< string, string, string >("7", "7", "7"), + new Tuple< string, string, string >("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"); + + } + } +} diff --git a/exercise.tests/exercise.tests.csproj b/exercise.tests/exercise.tests.csproj index 93317f6..2e6636a 100644 --- a/exercise.tests/exercise.tests.csproj +++ b/exercise.tests/exercise.tests.csproj @@ -9,6 +9,14 @@ true + + + + + + + +