Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 75 additions & 7 deletions exercise.main/Core.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
Expand All @@ -8,24 +9,91 @@

namespace exercise.main
{
public class Core
{


public class Core {


//public string card;
public Dictionary<string, int> cardValues = new Dictionary<string, int>()
{
{"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<Tuple<string, string>> hand, out Tuple<string, string> result)
{
result = new Tuple<string,string>(string.Empty, string.Empty);



// create a list to store the hand with two same values as winning hand
List < Tuple<string, string>> winningHand = new List<Tuple<string, string>>();


// to store the int value of the card
List<int> winningPairValues = new List<int>();

// find the pair in the parameter given hands (given in the tests).
foreach(Tuple<string, string> 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<string, string> 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;
}
}
Expand Down
80 changes: 79 additions & 1 deletion exercise.main/Extension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,91 @@ namespace exercise.main
{
public class Extension
{
//public string card;
public Dictionary<string, int> cardValues = new Dictionary<string, int>()
{
{"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<Tuple<string, string, string>> hand, out Tuple<string, string, string> result)
{
result = new Tuple<string, string, string>(string.Empty, string.Empty, string.Empty);

return false;
// create a list to store the hand with two same values as winning hand
List<Tuple<string, string, string>> winningHand = new List<Tuple<string, string, string>>();

// to store the int value of the card
List<int> winningPairValues = new List<int>();

// find the pair in the parameter given hands (given in the tests).
foreach (Tuple<string, string, string> 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<string, string> 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;
}
}
}

17 changes: 11 additions & 6 deletions exercise.tests/CoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public void Scenario1()
{
new Tuple<string, string>("K", "5"),
new Tuple<string, string>("3","7")
};
};
Tuple<string, string> winner;

bool result = core.winningPair(hand, out winner);
Expand All @@ -29,6 +29,7 @@ public void Scenario1()

}

[Test]
//{("K","5"),("2","2")} => true out ("2","2")
public void Scenario1b()
{
Expand All @@ -38,32 +39,33 @@ public void Scenario1b()

List<Tuple<string, string>> hand = new List<Tuple<string, string>>
{
new Tuple<string, string>("K", "5"),
new Tuple<string, string>("K","5"),
new Tuple<string, string>("2","2")
};
Tuple<string, string> 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<Tuple<string, string>> hand = new List<Tuple<string, string>>
{
new Tuple<string, string>("K", "K"),
new Tuple<string, string>("A","A")
};

Tuple<string, string> winner;
bool result = core.winningPair(hand, out winner);

Expand All @@ -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();

Expand All @@ -93,6 +97,7 @@ public void Scenario3()
Assert.IsTrue(winner.Item1 == "7" && winner.Item2 == "7");

}


[TestCase("2",2)]
[TestCase("3",3)]
Expand Down
108 changes: 108 additions & 0 deletions exercise.tests/ExtensionTests.cs
Original file line number Diff line number Diff line change
@@ -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<Tuple<string, string, string>> hand = new List<Tuple<string, string, string>>
{
new Tuple<string, string, string>("K", "5", "8"),
new Tuple<string, string, string>("3","7", "8"),
new Tuple<string, string, string>("2","8", "8")
};
Tuple<string, string, string> 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<Tuple<string, string, string>> hand = new List<Tuple<string, string, string>>

{
new Tuple<string, string, string>("K", "5","J"),
new Tuple<string, string, string>("J","A","J"),
new Tuple<string, string, string>("2","2","2")
};

Tuple<string, string, string> 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<Tuple<string, string, string>> hand = new List<Tuple<string, string, string>>

{
new Tuple<string, string, string>("J","J","J"),
new Tuple<string, string, string>("K","K","K"),
new Tuple<string, string, string>("A","A","A")
};

Tuple<string, string, string> 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<Tuple<string, string, string>> hand = new List<Tuple<string, string, string>>

{
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<string, string, string> winner;
bool result = extension.winningThree(hand, out winner);

Assert.That(result, Is.True);

Assert.IsTrue(winner.Item1 == "7" && winner.Item2 == "7" && winner.Item3 == "7");

}
}
}
8 changes: 8 additions & 0 deletions exercise.tests/exercise.tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<Compile Remove="ExtensionTests.cs" />
</ItemGroup>

<ItemGroup>
<None Include="ExtensionTests.cs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
<PackageReference Include="NUnit" Version="3.13.3" />
Expand Down