From 7f6a2c6b2a9fe29a0f76b2b62734416f37dca6ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Skivenesv=C3=A5g=20Johannessen?= Date: Fri, 12 Jan 2024 12:37:45 +0100 Subject: [PATCH 1/6] Martin S. Johannessen C#3 --- README.md | 5 +- exercise.main/Core.cs | 38 +++++++++++--- exercise.main/Poker/Card.cs | 19 +++++++ exercise.main/Poker/Deck.cs | 62 ++++++++++++++++++++++ exercise.main/Poker/Player.cs | 38 ++++++++++++++ exercise.main/Poker/PokerGame.cs | 90 ++++++++++++++++++++++++++++++++ exercise.main/Program.cs | 52 +++++++++++++++++- exercise.tests/CoreTests.cs | 2 - 8 files changed, 295 insertions(+), 11 deletions(-) create mode 100644 exercise.main/Poker/Card.cs create mode 100644 exercise.main/Poker/Deck.cs create mode 100644 exercise.main/Poker/Player.cs create mode 100644 exercise.main/Poker/PokerGame.cs 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..b663f31 --- /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 player)"; + 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..954a8c5 --- /dev/null +++ b/exercise.main/Poker/PokerGame.cs @@ -0,0 +1,90 @@ +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() + { + Table.Add(Deck.Deal()); + } + + 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..70a1c35 100644 --- a/exercise.main/Program.cs +++ b/exercise.main/Program.cs @@ -1,2 +1,52 @@ // See https://aka.ms/new-console-template for more information -Console.WriteLine("Hello, World!"); +using exercise.main.Poker; + +PokerGame pokerGame = new PokerGame(); // Texas hold'em + +Player player3 = new Player("Hans"); //two players join automatically, we add a third. + +pokerGame.JoinGame(player3); + + +foreach (var player in pokerGame.Players) //each player gets two cards to start with +{ + pokerGame.DealPlayer(player); + pokerGame.DealPlayer(player); +} + +for (int i = 0; i < 4; i++) //add 5 cards to the table +{ + pokerGame.DealTable(); +} + +var results = pokerGame.GetResults(); + +int numOfDupes = results[0].Item2.Count; +Dictionary handType = new Dictionary() +{ + {1, "high card"}, + {2, "pair" }, + {3, "trifecta" }, + {4, "quadlecta"} +}; + +Console.WriteLine(results[0].Item1.Name + $" won the round with the {handType[numOfDupes]}"); +foreach(Card c in results[0].Item2) +{ + Console.WriteLine($"{c.Value} of {c.Suit} "); +} + +results.RemoveAt(0); + +Console.WriteLine("Against "); +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} "); + } +} + + 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> From f1a2bff2a5d816dbde9a7254d773cdff3117baa6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Skivenesv=C3=A5g=20Johannessen?= Date: Fri, 12 Jan 2024 15:48:24 +0100 Subject: [PATCH 2/6] Martin S. Johannessen C#3 --- exercise.main/Poker/Player.cs | 2 +- exercise.main/Program.cs | 109 ++++++++++++++++++++++++++++++---- 2 files changed, 100 insertions(+), 11 deletions(-) diff --git a/exercise.main/Poker/Player.cs b/exercise.main/Poker/Player.cs index b663f31..1930af6 100644 --- a/exercise.main/Poker/Player.cs +++ b/exercise.main/Poker/Player.cs @@ -17,7 +17,7 @@ public Player(string name) } public Player() { - Name = "John Doe (anon player)"; + Name = "John Doe(anon)"; Cards = new List(); } diff --git a/exercise.main/Program.cs b/exercise.main/Program.cs index 70a1c35..ba93758 100644 --- a/exercise.main/Program.cs +++ b/exercise.main/Program.cs @@ -1,51 +1,140 @@ // See https://aka.ms/new-console-template for more information using exercise.main.Poker; +Console.WriteLine("Press any key"); + +Console.ReadLine(); + 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); } -for (int i = 0; i < 4; i++) //add 5 cards to the table +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(6); +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 another card"); +Console.ReadLine(); +pokerGame.DealTable(); + +randomNumber = random.Next(4); +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 another card"); +Console.ReadLine(); +pokerGame.DealTable(); + +randomNumber = random.Next(2); +if (randomNumber == 0) { - pokerGame.DealTable(); + + 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 another card"); +Console.ReadLine(); +pokerGame.DealTable(); -var results = pokerGame.GetResults(); +Console.WriteLine(); +Console.WriteLine(); +Console.WriteLine(); +Console.WriteLine("Show your hands players"); +Console.ReadLine(); +Console.WriteLine(); -int numOfDupes = results[0].Item2.Count; +results = pokerGame.GetResults(); + +int numOfDupes = results.First().Item2.Count; Dictionary handType = new Dictionary() { {1, "high card"}, - {2, "pair" }, - {3, "trifecta" }, - {4, "quadlecta"} + {2, "two of a kind" }, + {3, "three of a kind" }, + {4, "four of a kind"} }; -Console.WriteLine(results[0].Item1.Name + $" won the round with the {handType[numOfDupes]}"); +Console.WriteLine(results[0].Item1.Name + $" won the round with {handType[numOfDupes]}"); foreach(Card c in results[0].Item2) { - Console.WriteLine($"{c.Value} of {c.Suit} "); + Console.WriteLine($"[{c.Value} of {c.Suit}] "); } results.RemoveAt(0); Console.WriteLine("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($"[{c.Value} of {c.Suit}] "); } } From 47103e3157b903de8bf54bc927a269ba7b5e1adc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Skivenesv=C3=A5g=20Johannessen?= Date: Fri, 12 Jan 2024 18:17:37 +0100 Subject: [PATCH 3/6] Martin S. Johannessen C#3 fixed the continue prompt --- exercise.main/Program.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/exercise.main/Program.cs b/exercise.main/Program.cs index ba93758..f7fd959 100644 --- a/exercise.main/Program.cs +++ b/exercise.main/Program.cs @@ -1,7 +1,7 @@ // See https://aka.ms/new-console-template for more information using exercise.main.Poker; -Console.WriteLine("Press any key"); +Console.WriteLine("Press enter to continue the poker simulation"); Console.ReadLine(); @@ -22,8 +22,8 @@ { Console.WriteLine("and "); } - - Console.Write("+"+p.Name+"+"); + + Console.Write("+"+ p.Name +"+"); } Console.WriteLine(" joined the dealer for a game of poker."); From 83723fde9bbc18f084293eff9854d3cd1be02e3f Mon Sep 17 00:00:00 2001 From: Martin Johannessen Date: Sun, 14 Jan 2024 21:17:06 +0100 Subject: [PATCH 4/6] Martin S. Johannessen C#3 --- exercise.main/Program.cs | 224 +++++++++++++++++++++------------------ 1 file changed, 121 insertions(+), 103 deletions(-) diff --git a/exercise.main/Program.cs b/exercise.main/Program.cs index f7fd959..f3cf7ea 100644 --- a/exercise.main/Program.cs +++ b/exercise.main/Program.cs @@ -5,137 +5,155 @@ Console.ReadLine(); -PokerGame pokerGame = new PokerGame(); // Texas hold'em +bool goOn = true; -Player player3 = new Player("Hans"); //two players join automatically, we add a third. +while (goOn) +{ + PokerGame pokerGame = new PokerGame(); // Texas hold'em -pokerGame.JoinGame(player3); + Player player3 = new Player("Hans"); //two players join automatically, we add a third. -Console.Write("The players "); -foreach(Player p in pokerGame.Players) -{ - if (!pokerGame.Players.First().Equals(p)) - { - Console.Write(", "); + 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 + "+"); } - if (pokerGame.Players.Last().Equals(p)) + 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 { - Console.WriteLine("and "); + pokerGame.DealPlayer(player); + pokerGame.DealPlayer(player); } - Console.Write("+"+ p.Name +"+"); -} -Console.WriteLine(" joined the dealer for a game of poker."); + Console.ReadLine(); + Console.WriteLine(); + Console.WriteLine(); + Console.WriteLine("The table recieves three cards."); + Console.ReadLine(); -Console.ReadLine(); + pokerGame.DealTable(); + pokerGame.DealTable(); + pokerGame.DealTable(); -Console.WriteLine(); -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); -} + Random random = new Random(); + List>> results = new List>>(); -Console.ReadLine(); -Console.WriteLine() ; -Console.WriteLine(); -Console.WriteLine("The table recieves three cards."); -Console.ReadLine(); + int randomNumber = random.Next(6); + 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 another card"); + Console.ReadLine(); + pokerGame.DealTable(); -pokerGame.DealTable(); -pokerGame.DealTable(); -pokerGame.DealTable(); + randomNumber = random.Next(4); + if (randomNumber == 0) + { -Console.WriteLine(); + 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 another card"); + Console.ReadLine(); + pokerGame.DealTable(); -Random random = new Random(); -List>> results = new List>>(); + randomNumber = random.Next(2); + if (randomNumber == 0) + { -int randomNumber = random.Next(6); -if (randomNumber == 0) -{ - results = pokerGame.GetResults(); - var worstHand = results.Last(); - pokerGame.Players.Remove(worstHand.Item1); - Console.WriteLine($"{worstHand.Item1.Name} folds (bad hand?)."); + 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 another card"); Console.ReadLine(); -} -Console.WriteLine("The table recieves another card"); -Console.ReadLine(); -pokerGame.DealTable(); + pokerGame.DealTable(); -randomNumber = random.Next(4); -if (randomNumber == 0) -{ - - results = pokerGame.GetResults(); - var worstHand = results.Last(); - pokerGame.Players.Remove(worstHand.Item1); - Console.WriteLine($"{worstHand.Item1.Name} folds (bad hand?)."); + Console.WriteLine(); + Console.WriteLine(); + Console.WriteLine(); + Console.WriteLine("Show your hands players"); Console.ReadLine(); -} -Console.WriteLine("The table recieves another card"); -Console.ReadLine(); -pokerGame.DealTable(); + Console.WriteLine(); -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 another card"); -Console.ReadLine(); -pokerGame.DealTable(); -Console.WriteLine(); -Console.WriteLine(); -Console.WriteLine(); -Console.WriteLine("Show your hands players"); -Console.ReadLine(); -Console.WriteLine(); + 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[0].Item1.Name + $" won the round with {handType[numOfDupes]}"); + foreach (Card c in results[0].Item2) + { + Console.WriteLine($"[{c.Value} of {c.Suit}] "); + } -results = pokerGame.GetResults(); + results.RemoveAt(0); -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[0].Item1.Name + $" won the round with {handType[numOfDupes]}"); -foreach(Card c in results[0].Item2) -{ - Console.WriteLine($"[{c.Value} of {c.Suit}] "); -} + Console.WriteLine("Against "); -results.RemoveAt(0); + if (results.Count == 0) + { + Console.WriteLine("*crickets*"); + } -Console.WriteLine("Against "); + 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}] "); + } + } -if (results.Count == 0) -{ - Console.WriteLine("*crickets*"); -} + Console.WriteLine(); + Console.WriteLine(); + Console.WriteLine(); + Console.WriteLine("Press enter to start a new simulation, or q to quit"); -foreach (Tuple> t in results) -{ - numOfDupes = t.Item2.Count; - Console.WriteLine(t.Item1.Name + $"'s {handType[numOfDupes]} "); - foreach (Card c in t.Item2) + + + string? input = Console.ReadLine(); + + if ( input == "q" ) { - Console.WriteLine($"[{c.Value} of {c.Suit}] "); + goOn = false; } } - From 8b3ed25eb503ca415ec7ce667e1202d969fea5c0 Mon Sep 17 00:00:00 2001 From: Martin Johannessen Date: Sun, 14 Jan 2024 21:39:00 +0100 Subject: [PATCH 5/6] finish --- exercise.main/Poker/PokerGame.cs | 4 +++- exercise.main/Program.cs | 35 +++++++++++++++----------------- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/exercise.main/Poker/PokerGame.cs b/exercise.main/Poker/PokerGame.cs index 954a8c5..191a3db 100644 --- a/exercise.main/Poker/PokerGame.cs +++ b/exercise.main/Poker/PokerGame.cs @@ -33,7 +33,9 @@ public void JoinGame(Player player) public void DealTable() { - Table.Add(Deck.Deal()); + Card card = Deck.Deal(); + Console.WriteLine($"[{card.Value} of {card.Suit}] "); + Table.Add(card); } public void DealPlayer(Player player) diff --git a/exercise.main/Program.cs b/exercise.main/Program.cs index f3cf7ea..d836ac2 100644 --- a/exercise.main/Program.cs +++ b/exercise.main/Program.cs @@ -53,12 +53,13 @@ pokerGame.DealTable(); pokerGame.DealTable(); + Console.WriteLine(); Random random = new Random(); List>> results = new List>>(); - int randomNumber = random.Next(6); + int randomNumber = random.Next(3); if (randomNumber == 0) { results = pokerGame.GetResults(); @@ -67,21 +68,7 @@ Console.WriteLine($"{worstHand.Item1.Name} folds (bad hand?)."); Console.ReadLine(); } - Console.WriteLine("The table recieves another card"); - Console.ReadLine(); - pokerGame.DealTable(); - - randomNumber = random.Next(4); - 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 another card"); + Console.WriteLine("The table recieves a fourth card"); Console.ReadLine(); pokerGame.DealTable(); @@ -95,7 +82,7 @@ Console.WriteLine($"{worstHand.Item1.Name} folds (bad hand?)."); Console.ReadLine(); } - Console.WriteLine("The table recieves another card"); + Console.WriteLine("The table recieves a fifth card"); Console.ReadLine(); pokerGame.DealTable(); @@ -103,6 +90,16 @@ Console.WriteLine(); Console.WriteLine(); Console.WriteLine("Show your hands players"); + + 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(); @@ -117,8 +114,8 @@ {4, "four of a kind"} }; - Console.WriteLine(results[0].Item1.Name + $" won the round with {handType[numOfDupes]}"); - foreach (Card c in results[0].Item2) + Console.WriteLine(results.First().Item1.Name + $" won the round with {handType[numOfDupes]}"); + foreach (Card c in results.First().Item2) { Console.WriteLine($"[{c.Value} of {c.Suit}] "); } From 3def60167b9fa298bdbe725424558f088011f6a8 Mon Sep 17 00:00:00 2001 From: Martin Johannessen Date: Sun, 14 Jan 2024 22:06:47 +0100 Subject: [PATCH 6/6] finish final --- exercise.main/Program.cs | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/exercise.main/Program.cs b/exercise.main/Program.cs index d836ac2..3100462 100644 --- a/exercise.main/Program.cs +++ b/exercise.main/Program.cs @@ -69,8 +69,8 @@ Console.ReadLine(); } Console.WriteLine("The table recieves a fourth card"); - Console.ReadLine(); pokerGame.DealTable(); + Console.ReadLine(); randomNumber = random.Next(2); if (randomNumber == 0) @@ -83,13 +83,15 @@ Console.ReadLine(); } Console.WriteLine("The table recieves a fifth card"); - Console.ReadLine(); + 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 "); @@ -97,9 +99,14 @@ { 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(); @@ -114,15 +121,15 @@ {4, "four of a kind"} }; - Console.WriteLine(results.First().Item1.Name + $" won the round with {handType[numOfDupes]}"); + 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("Against "); + Console.WriteLine(); + Console.Write("Against "); if (results.Count == 0) { @@ -135,10 +142,13 @@ Console.WriteLine(t.Item1.Name + $"'s {handType[numOfDupes]} "); foreach (Card c in t.Item2) { - Console.WriteLine($"[{c.Value} of {c.Suit}] "); + Console.WriteLine( $"[{c.Value} of {c.Suit}] "); } } + Console.WriteLine(); + Console.WriteLine(); + Console.WriteLine(); Console.WriteLine(); Console.WriteLine(); Console.WriteLine();