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
66 changes: 63 additions & 3 deletions exercise.main/Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,73 @@ public bool winningPair(IEnumerable<Tuple<string, string>> hands, out Tuple<stri
{
result = new Tuple<string,string>(string.Empty, string.Empty);

// your code here...

foreach (var hand in hands)
{
if (hand.Item1 == hand.Item2)
{
if (result.Item1 != string.Empty)
{
if (GetValueOfCard(hand.Item1) > GetValueOfCard(result.Item1))
{
result = hand;
}
} else
{
result = hand;
}
}
}
return result.Item1!=string.Empty ? true : false;
}

public bool winningTriplet(IEnumerable<Tuple<string, string, string>> hands, out Tuple<string, string, string> result)
{
result = new Tuple<string, string, string>(string.Empty, string.Empty, string.Empty);

foreach (var hand in hands)
{
if (hand.Item1 == hand.Item2 && hand.Item2 == hand.Item3)
{
if (result.Item1 != string.Empty)
{
if (GetValueOfCard(hand.Item1) > GetValueOfCard(result.Item1))
{
result = hand;
}
}
else
{
result = hand;
}
}
}
return result.Item1 != string.Empty ? true : false;
}

public int GetValueOfCard(string card)
{
return 0;
bool isNumericCard = int.TryParse(card, out int cardNumber);

if (isNumericCard)
{
return cardNumber;
}
else
{
switch (card)
{
case "J":
return 11;
case "Q":
return 12;
case "K":
return 13;
case "A":
return 14;
default:
return 0;
}
}
}
}
}
24 changes: 24 additions & 0 deletions exercise.main/Models/Card.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace exercise.main.Models
{
public class Card
{
private string _cardValue;
private string _suit;

public string CardValue { get => _cardValue; set => _cardValue = value; }
public string Suit { get => _suit; set => _suit = value; }
/*
public Card(string cardValue, string suit)
{
_cardValue = cardValue;
_suit = suit;
}
*/
}
}
109 changes: 109 additions & 0 deletions exercise.main/Models/Deck.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using exercise.main;

namespace exercise.main.Models
{
public class Deck
{
private List<Card> _cards;

public List<Card> Cards { get => _cards; set => _cards = value; }

// Shuffling / generating 52 standard decks size cards in the deck
public void Shuffle()
{
var rnd = new Random();
_cards = new List<Card>();
for (int i = 0; i < 53; i++)
{
Card newCard = new Card();
int generatedCardValue = rnd.Next(1, 15);

bool numericValue = generatedCardValue > 0 && generatedCardValue < 11;
if (!numericValue)
{
switch (generatedCardValue)
{
case 11:
newCard.CardValue = "J";
break;
case 12:
newCard.CardValue = "Q";
break;
case 13:
newCard.CardValue = "K";
break;
case 14:
newCard.CardValue = "A";
break;
}
}
else
{
newCard.CardValue = generatedCardValue.ToString();
}

int generatedSuitValue = rnd.Next(1, 5);

switch (generatedSuitValue)
{
case 1:
newCard.Suit = "Spades";
break;
case 2:
newCard.Suit = "Hearts";
break;
case 3:
newCard.Suit = "Clubs";
break;
case 4:
newCard.Suit = "Diamonds";
break;
}

Cards.Add(newCard);
}
}

public void Deal()
{
var rnd = new Random();
int cardToPick = rnd.Next(0, Cards.Count);

Card card = Cards[cardToPick];
Cards.RemoveAt(cardToPick);

Console.WriteLine($"{card.CardValue} of {card.Suit}");
}

public int GetValueOfCard(string card)
{
bool isNumericCard = int.TryParse(card, out int cardNumber);

if (isNumericCard)
{
return cardNumber;
}
else
{
switch (card)
{
case "J":
return 11;
case "Q":
return 12;
case "K":
return 13;
case "A":
return 14;
default:
return 0;
}
}
}
}
}
23 changes: 23 additions & 0 deletions exercise.tests/CoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,29 @@ public void Scenario3()

}

//Selfmade test scenario for extension task testing for triplets
//{("4", "3", "5"),("6","6","6"),("7","7","K"),("3","3","3")} => true ("6", "6", "6")
[Test]
public void Scenario4()
{
Core core = new Core();

List<Tuple<string, string, string>> hand = new List<Tuple<string, string, string>>
{
new Tuple<string, string, string>("4", "3", "5"),
new Tuple<string, string, string>("6", "6", "6"),
new Tuple<string, string, string>("7", "7", "K"),
new Tuple<string, string, string>("3","3", "3")
};
Tuple<string, string, string> winner;
bool result = core.winningTriplet(hand, out winner);

Assert.That(result, Is.True);

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

}

[TestCase("2",2)]
[TestCase("3",3)]
[TestCase("4",4)]
Expand Down