diff --git a/README.md b/README.md index d566543..717b43f 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,10 @@ Requirements: - Implement a `Deck` class that stores a list / dictionary / array of `Card` objects - The `Deck` should have a method to `Shuffle()` / regenerate the deck - The `Deck` should have a method to `Deal()` one card from the deck, removing it from the list of cards in the deck -- Implement a `Player` class that stores the Name of the player and a list of cards that the player has in their hand. The class should have a method for adding 1 card to the hand; a method to clear the cards in the hand; a method for returning what cards are in the hand. +- Implement a `Player` class that stores the Name of the player and a list of cards that the player has in + their hand. The class should have a method for adding 1 card to the hand; +a method to clear the cards in the hand; +a method for returning what cards are in the hand. - Implement a `PokerGame` class. This class should create 2 `Players` and a `Deck`; this class should have different methods as required to start a game; check if anyone has won; store a list of cards on the table; and functions to progress from the start of a Poker round until a player has won (5 cards have been dealt). - Write some code in `Program.cs` to run a full simulation of a round of Poker which involves resetting a deck, clearing the player's hands; dealing 2 cards to each player + 3 cards on the table; then dealing 2 more cards onto the table and finally computing the winner of that round. Your code should clearly print out messages about each stage and display the player's hands, the tables cards and the winner. diff --git a/exercise.main/Core.cs b/exercise.main/Core.cs index 7568220..0abdeb0 100644 --- a/exercise.main/Core.cs +++ b/exercise.main/Core.cs @@ -11,22 +11,46 @@ 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); - - + string winningPair = string.Empty; + int winningValue = 0; - + foreach(Tuple pair in hand) + { + if(pair.Item1.Equals(pair.Item2) && GetValueOfCard(pair.Item1)>winningValue) + { + winningPair = pair.Item1; + winningValue = GetValueOfCard(pair.Item1); + } + } + + result = new Tuple(winningPair, winningPair); return result.Item1!=string.Empty ? true : false; } public int GetValueOfCard(string card) - { - return 0; + { + Dictionary lookup = 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} + }; + return lookup[card]; } } } diff --git a/exercise.main/Poker/Card.cs b/exercise.main/Poker/Card.cs new file mode 100644 index 0000000..3141b0b --- /dev/null +++ b/exercise.main/Poker/Card.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace exercise.main.Poker +{ + public class Card + { + public string Value { get; } + public string Suit { get; } + public Card(string value, string suit) + { + Value = value; + Suit = suit; + } + } +} diff --git a/exercise.main/Poker/Deck.cs b/exercise.main/Poker/Deck.cs new file mode 100644 index 0000000..7ca9eb7 --- /dev/null +++ b/exercise.main/Poker/Deck.cs @@ -0,0 +1,62 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace exercise.main.Poker +{ + public class Deck + { + private string[] _Values = new string[13] { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"}; + private string[] _Suits = new string[4] { "Hearts", "Diamonds", "Clubs", "Spades" }; + + private Stack _Deck = new Stack(); + + public Deck() + { + foreach(string value in _Values) + { + foreach(string suit in _Suits) + { + _Deck.Push(new Card(value, suit)); + } + } + + ShuffleCards(); + } + public void ShuffleCards() + { + List cards = new List(_Deck); + Random random = new Random(); + int n = cards.Count; + + for (int i = n - 1; i > 0; i--) + { + int index = random.Next(i + 1); + + // Swap the cards at i and index + Card temp = cards[i]; + cards[i] = cards[index]; + cards[index] = temp; + } + + _Deck.Clear(); + foreach (Card card in cards) + { + _Deck.Push(card); + } + + } + + public Stack GetCards() + { + return _Deck; + } + + public Card Deal() + { + return _Deck.Pop(); + } + } +} diff --git a/exercise.main/Poker/Player.cs b/exercise.main/Poker/Player.cs new file mode 100644 index 0000000..1930af6 --- /dev/null +++ b/exercise.main/Poker/Player.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace exercise.main.Poker +{ + public class Player + { + public string Name { get; set; } + private List Cards { get; set; } + public Player(string name) + { + Name = name; + Cards = new List(); + } + public Player() + { + Name = "John Doe(anon)"; + Cards = new List(); + } + + public void TakeCard(Card card) + { + Cards.Add(card); + } + public List ShowCards() + { + return Cards; + } + + public void ThrowCards() + { + Cards.Clear(); + } + } +} diff --git a/exercise.main/Poker/PokerGame.cs b/exercise.main/Poker/PokerGame.cs new file mode 100644 index 0000000..191a3db --- /dev/null +++ b/exercise.main/Poker/PokerGame.cs @@ -0,0 +1,92 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading.Tasks; + +namespace exercise.main.Poker +{ + public class PokerGame + { + public List Players; + public Deck Deck; + public List Table; + public PokerGame() + { + Players = new List(); + + Player player1 = new Player("Martin"); + JoinGame(player1); + + Player player2 = new Player(); + JoinGame(player2); + + Deck = new Deck(); + Table = new List(); + } + + public void JoinGame(Player player) + { + Players.Add(player); + } + + public void DealTable() + { + Card card = Deck.Deal(); + Console.WriteLine($"[{card.Value} of {card.Suit}] "); + Table.Add(card); + } + + public void DealPlayer(Player player) + { + player.TakeCard(Deck.Deal()); + } + + public int GetValueOfCard(string card) + { + Dictionary lookup = 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} + }; + return lookup[card]; + } + public List>> GetResults() + { + + List>> results = new List>>(); + + foreach (Player p in Players) + { + List allCards = p.ShowCards().Concat(Table).ToList(); + + var bestGroup = allCards.GroupBy(card => GetValueOfCard(card.Value)) + .OrderByDescending(group => group.Count()) + .ThenByDescending(group => group.Key) + .First(); + + Tuple> playerResults = Tuple.Create(p, bestGroup.ToList()); + results.Add(playerResults); + + } + + results = results.OrderByDescending(a => a.Item2.Count()) + .ThenByDescending(a => GetValueOfCard(a.Item2[0].Value)).ToList(); + + return results; + } + } +} diff --git a/exercise.main/Program.cs b/exercise.main/Program.cs index 3751555..3100462 100644 --- a/exercise.main/Program.cs +++ b/exercise.main/Program.cs @@ -1,2 +1,166 @@ // See https://aka.ms/new-console-template for more information -Console.WriteLine("Hello, World!"); +using exercise.main.Poker; + +Console.WriteLine("Press enter to continue the poker simulation"); + +Console.ReadLine(); + +bool goOn = true; + +while (goOn) +{ + PokerGame pokerGame = new PokerGame(); // Texas hold'em + + Player player3 = new Player("Hans"); //two players join automatically, we add a third. + + pokerGame.JoinGame(player3); + + Console.Write("The players "); + foreach (Player p in pokerGame.Players) + { + if (!pokerGame.Players.First().Equals(p)) + { + Console.Write(", "); + } + if (pokerGame.Players.Last().Equals(p)) + { + Console.WriteLine("and "); + } + + Console.Write("+" + p.Name + "+"); + } + Console.WriteLine(" joined the dealer for a game of poker."); + + Console.ReadLine(); + + Console.WriteLine(); + Console.WriteLine(); + + Console.WriteLine("Each player is dealt two cards."); + foreach (var player in pokerGame.Players) //each player gets two cards to start with + { + pokerGame.DealPlayer(player); + pokerGame.DealPlayer(player); + } + + Console.ReadLine(); + Console.WriteLine(); + Console.WriteLine(); + Console.WriteLine("The table recieves three cards."); + Console.ReadLine(); + + pokerGame.DealTable(); + pokerGame.DealTable(); + pokerGame.DealTable(); + + + Console.WriteLine(); + + Random random = new Random(); + List>> results = new List>>(); + + int randomNumber = random.Next(3); + if (randomNumber == 0) + { + results = pokerGame.GetResults(); + var worstHand = results.Last(); + pokerGame.Players.Remove(worstHand.Item1); + Console.WriteLine($"{worstHand.Item1.Name} folds (bad hand?)."); + Console.ReadLine(); + } + Console.WriteLine("The table recieves a fourth card"); + pokerGame.DealTable(); + Console.ReadLine(); + + randomNumber = random.Next(2); + if (randomNumber == 0) + { + + results = pokerGame.GetResults(); + var worstHand = results.Last(); + pokerGame.Players.Remove(worstHand.Item1); + Console.WriteLine($"{worstHand.Item1.Name} folds (bad hand?)."); + Console.ReadLine(); + } + Console.WriteLine("The table recieves a fifth card"); + + pokerGame.DealTable(); + Console.ReadLine(); + + Console.WriteLine(); + Console.WriteLine(); + Console.WriteLine(); + Console.WriteLine("Show your hands players"); + Console.ReadLine(); + + foreach ( var player in pokerGame.Players ) { + Console.WriteLine(player.Name+" shows "); + foreach (Card c in player.ShowCards()) + { + Console.WriteLine($"[{c.Value} of {c.Suit}] "); + } + Console.ReadLine(); + + } + + Console.WriteLine(); + Console.WriteLine(); + Console.WriteLine(); + Console.WriteLine("The players look at the final result"); + Console.ReadLine(); + Console.WriteLine(); + + results = pokerGame.GetResults(); + + int numOfDupes = results.First().Item2.Count; + Dictionary handType = new Dictionary() + { + {1, "high card"}, + {2, "two of a kind" }, + {3, "three of a kind" }, + {4, "four of a kind"} + }; + + Console.WriteLine(results.First().Item1.Name.ToUpper() + $" won the round with {handType[numOfDupes]}!"); + foreach (Card c in results.First().Item2) + { + Console.WriteLine($"[{c.Value} of {c.Suit}] "); + } + results.RemoveAt(0); + + Console.WriteLine(); + Console.Write("Against "); + + if (results.Count == 0) + { + Console.WriteLine("*crickets*"); + } + + foreach (Tuple> t in results) + { + numOfDupes = t.Item2.Count; + Console.WriteLine(t.Item1.Name + $"'s {handType[numOfDupes]} "); + foreach (Card c in t.Item2) + { + Console.WriteLine( $"[{c.Value} of {c.Suit}] "); + } + } + + Console.WriteLine(); + Console.WriteLine(); + Console.WriteLine(); + Console.WriteLine(); + Console.WriteLine(); + Console.WriteLine(); + Console.WriteLine("Press enter to start a new simulation, or q to quit"); + + + + string? input = Console.ReadLine(); + + if ( input == "q" ) + { + goOn = false; + } +} + diff --git a/exercise.tests/CoreTests.cs b/exercise.tests/CoreTests.cs index 5adefb3..a81dae5 100644 --- a/exercise.tests/CoreTests.cs +++ b/exercise.tests/CoreTests.cs @@ -10,8 +10,6 @@ public class Tests [Test] public void Scenario1() { - - Core core = new Core(); List> hand = new List>