diff --git a/exercise.main/CardHandler.cs b/exercise.main/CardHandler.cs new file mode 100644 index 0000000..a3c4b4d --- /dev/null +++ b/exercise.main/CardHandler.cs @@ -0,0 +1,46 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace exercise.main +{ + public class CardHandler + { + public Dictionary CardValues { get; set; } = new Dictionary + { + {"1",1 }, + {"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}, + }; + public CardHandler() { } + + public int GetValueOfCard(string card) + { + return CardValues[card]; + } + + public bool isPair(Tuple pair) + { + return pair.Item1 == pair.Item2; + } + + public bool isTriplet(Tuple triplet) { + return triplet.Item1 == triplet.Item2 && triplet.Item2 == triplet.Item3; + } + + + } +} diff --git a/exercise.main/Core.cs b/exercise.main/Core.cs index bf5077d..58934ac 100644 --- a/exercise.main/Core.cs +++ b/exercise.main/Core.cs @@ -10,23 +10,41 @@ namespace exercise.main { public class Core { - + + public Core() { } // Note: // IEnumerable is a class that the List inherits (ie List has all the functionality of IEnumerable) // IEnumerable allows you to use a foreach loop on the variable hands (which is a List of tuples) - + //TODO: complete the winningPair method, without chaning the method signature + CardHandler handler = new CardHandler(); + public bool winningPair(IEnumerable> hands, out Tuple result) { result = new Tuple(string.Empty, string.Empty); - // your code here... + // your code here... + + var winningValue = 0; + var currentIndex = 0; + foreach (var item in hands) + { + if(handler.isPair(item) && handler.GetValueOfCard(item.Item1) > winningValue) + { + result = item; + winningValue = handler.GetValueOfCard(item.Item2); + } + currentIndex++; + } + + return result.Item1!=string.Empty ? true : false; } + public int GetValueOfCard(string card) { - return 0; + return handler.GetValueOfCard(card); } } } diff --git a/exercise.main/Extension.cs b/exercise.main/Extension.cs index b33ba1f..7e848cc 100644 --- a/exercise.main/Extension.cs +++ b/exercise.main/Extension.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Reflection.Metadata; using System.Text; using System.Threading.Tasks; @@ -9,12 +10,27 @@ 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> hands, out Tuple result) { result = new Tuple(string.Empty, string.Empty, string.Empty); - return false; + + CardHandler handler = new CardHandler(); + var winningValue = 0; + var currentIndex = 0; + foreach (var item in hands) + { + if (handler.isTriplet(item) && handler.GetValueOfCard(item.Item1) > winningValue) + { + result = item; + winningValue = handler.GetValueOfCard(item.Item2); + } + currentIndex++; + } + return result.Item1 != string.Empty ? true : false; } + } + } diff --git a/exercise.main/Program.cs b/exercise.main/Program.cs index 3751555..f8d527a 100644 --- a/exercise.main/Program.cs +++ b/exercise.main/Program.cs @@ -1,2 +1,19 @@ // See https://aka.ms/new-console-template for more information +using exercise.main.UngradedExtension; + Console.WriteLine("Hello, World!"); + + +PokerGame game = new PokerGame("Nigel", "AJ"); + +game.Start(); +game.PrintGameStatus(); +game.playTurn(); + +game.playTurn(); + +Console.WriteLine("Game over"); + +game.PrintGameStatus(); + + diff --git a/exercise.main/UngradedExtension/Card.cs b/exercise.main/UngradedExtension/Card.cs new file mode 100644 index 0000000..3813da2 --- /dev/null +++ b/exercise.main/UngradedExtension/Card.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace exercise.main.UngradedExtension +{ + public class Card + + { + public Suit Suit { get; set; } + public string Value { get; set; } + + public Card(string value, Suit suit) { + Suit = suit; + Value = value; + } + + public void PrintCard() + { + Console.WriteLine(this.ToString()); + } + public override string ToString() + { + return this.Value + " of " + this.Suit; + } + } + +} diff --git a/exercise.main/UngradedExtension/Deck.cs b/exercise.main/UngradedExtension/Deck.cs new file mode 100644 index 0000000..e092932 --- /dev/null +++ b/exercise.main/UngradedExtension/Deck.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace exercise.main.UngradedExtension +{ + public class Deck + { + List cards; + + public Deck() { + InitializeDeck(); + } + + public void InitializeDeck() + { + CardHandler cardHandler = new CardHandler(); + cards = new List(); + foreach (var suit in Enum.GetValues()) { + foreach (var value in cardHandler.CardValues) + { + Card newCard = new Card(value.Key, suit); + cards.Add(newCard); + } + } + Shuffle(); + } + public void Shuffle() + { + Random rng = new Random(); + int n = cards.Count; + while (n > 1) + { + n--; + int k = rng.Next(n + 1); + Card card = cards[k]; + cards[k] = cards[n]; + cards[n] = card; + } + } + + public Card Deal() + { + Card dealtCard = cards.Last(); + cards.Remove(dealtCard); + return dealtCard; + } + } +} diff --git a/exercise.main/UngradedExtension/Player.cs b/exercise.main/UngradedExtension/Player.cs new file mode 100644 index 0000000..a2210aa --- /dev/null +++ b/exercise.main/UngradedExtension/Player.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace exercise.main.UngradedExtension +{ + public class Player + { + + public string Name { get; set; } + public List hand { get; set; } + + public Player(string name) + { + Name = name; + } + + public void ClearHand() + { + hand = new List(); + } + + public void AddCardToHand(Card card) + { + hand.Add(card); + } + + public List GetHand() + { + return hand; + } + + public void PrintHand() + { + Console.WriteLine(this.Name + " has:"); + hand.ForEach(x => x.PrintCard()); + } + } +} diff --git a/exercise.main/UngradedExtension/PokerGame.cs b/exercise.main/UngradedExtension/PokerGame.cs new file mode 100644 index 0000000..f165069 --- /dev/null +++ b/exercise.main/UngradedExtension/PokerGame.cs @@ -0,0 +1,67 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace exercise.main.UngradedExtension +{ + public class PokerGame + { + Player player1; + Player player2; + Table table; + Deck deck; + public PokerGame(string p1, string p2) { + player1 = new Player(p1); + player2 = new Player(p2); + table = new Table(); + deck = new Deck(); + } + + + public void Start() + { + deck.InitializeDeck(); + player1.ClearHand(); + player2.ClearHand(); + table.clearTable(); + + player1.AddCardToHand(deck.Deal()); + player2.AddCardToHand(deck.Deal()); + player1.AddCardToHand(deck.Deal()); + player2.AddCardToHand(deck.Deal()); + + table.AddCard(deck.Deal()); + table.AddCard(deck.Deal()); + table.AddCard(deck.Deal()); + + } + + public void playTurn() + { + table.AddCard(deck.Deal()); + } + + public void simulateGame() + { + Start(); + PrintGameStatus(); + playTurn(); + + playTurn(); + + Console.WriteLine("Game over"); + + PrintGameStatus(); + } + + public void PrintGameStatus() + { + Console.WriteLine("The game now looks like: "); + this.table.PrintTable(); + this.player1.PrintHand(); + this.player2.PrintHand(); + } + } +} diff --git a/exercise.main/UngradedExtension/Suit.cs b/exercise.main/UngradedExtension/Suit.cs new file mode 100644 index 0000000..8eafb16 --- /dev/null +++ b/exercise.main/UngradedExtension/Suit.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace exercise.main.UngradedExtension +{ + public enum Suit + { + Hearts, + Spades, + Diamonds, + Clubs + } +} diff --git a/exercise.main/UngradedExtension/Table.cs b/exercise.main/UngradedExtension/Table.cs new file mode 100644 index 0000000..bc71d76 --- /dev/null +++ b/exercise.main/UngradedExtension/Table.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace exercise.main.UngradedExtension +{ + public class Table + { + List cards; + public Table() + { + cards = new List(); + } + + public void AddCard(Card card) + { + cards.Add(card); + Console.WriteLine("Added " + card.ToString() + " to the table"); + } + + public void clearTable() + { + cards.Clear(); + } + + public void PrintTable() + { + Console.WriteLine("Table cards:"); + cards.ForEach(x => x.PrintCard()); + } + } +} diff --git a/exercise.tests/CoreTests.cs b/exercise.tests/CoreTests.cs index 5adefb3..0737f95 100644 --- a/exercise.tests/CoreTests.cs +++ b/exercise.tests/CoreTests.cs @@ -114,4 +114,67 @@ public void TestCards(string card, int value) Assert.That(result, Is.EqualTo(value)); } + + + [Test] + public void Scenario4() + { + Extension extension = new Extension(); + + List> hand = new List> + { + new Tuple("4", "3", "3"), + new Tuple("6", "6", "6"), + new Tuple("7", "7", "7"), + new Tuple("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"); + } + + + [Test] + public void Scenario5() + { + Extension extension = new Extension(); + + List> hand = new List> + { + new Tuple("4", "3", "3"), + new Tuple("6", "2", "6"), + new Tuple("9", "7", "7"), + new Tuple("3","K", "3") + }; + 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)); + } + + [Test] + public void Scenario6() + { + Extension extension = new Extension(); + + List> hand = new List> + { + new Tuple("4", "3", "3"), + new Tuple("2", "2", "2"), + new Tuple("A", "A", "K"), + new Tuple("3","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")); + } } \ No newline at end of file