From 4b0beed9420cb65ed079aff90daefa8b01dc8ae7 Mon Sep 17 00:00:00 2001 From: Louis Date: Thu, 11 Jan 2024 14:31:02 +0100 Subject: [PATCH 1/2] All tests pass. Currently working on the poker challenge, it is not completed yet --- README.md | 19 ++++++++--- exercise.main/Core.cs | 59 +++++++++++++++++++++++++++++---- exercise.main/Extension.cs | 31 ++++++++++++++++- exercise.main/PokerGame/Card.cs | 20 +++++++++++ exercise.main/PokerGame/Deck.cs | 39 ++++++++++++++++++++++ exercise.main/Program.cs | 32 ++++++++++++++++++ exercise.tests/CoreTests.cs | 22 ++++++++++++ 7 files changed, 210 insertions(+), 12 deletions(-) create mode 100644 exercise.main/PokerGame/Card.cs create mode 100644 exercise.main/PokerGame/Deck.cs diff --git a/README.md b/README.md index d566543..39e0dd1 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,8 @@ Welcome to the Poker challenge! 1. Fork this repository and then: 2. Run the tests from the Test Explorer. There should be a lot of failures to begin with. -3. In GitHub, [open a Pull Request](https://docs.github.com/en/github/collaborating-with-issues-and-pull-requests/creating-a-pull-request) from your forked repository to the challenge repository. +3. In GitHub, [open a Pull Request](https://docs.github.com/en/github/collaborating-with-issues-and-pull-requests/creating-a-pull-request) +1. from your forked repository to the challenge repository. 4. Implement the criteria below locally and [push your code](https://docs.github.com/en/github/managing-files-in-a-repository/adding-a-file-to-a-repository-using-the-command-line) to your repository! Every push to a branch that has an open Pull Request will update it automatically with your changes. 5. Check the status of the automated tests on the Pull Request - update your implementation if needed. @@ -22,12 +23,15 @@ All cards have a numeric value, number cards having their face value, Jack=11, Q This exercises uses Tuples, a lightweight type for storing, specifically a Tuple which can be thought as a pair of strings to represent each card in the pair. # Core Task -Given two hands of cards, return true if there is a winning hand and return it. A winning hand is one where both cards are the same value. If more than one hand is a pair of matching cards, then the highest pair must win. +Given two hands of cards, return true if there is a winning hand and return it. A winning hand is one where both cards are the +same value. If more than one hand is a pair of matching cards, then the highest pair must win. If none of the hands is a pair, then just return false and an empty string tuple. ## Extension Task -Given hands of three cards, calculate the winning hand when the hand is a triplet of three cards of the same value. If none of the hands are a triplet, return false and an empty string tuple. +Given hands of three cards, calculate the winning hand when the hand +is a triplet of three cards of the same value. If none of the hands are +a triplet, return false and an empty string tuple. # Ungraded Extension @@ -38,8 +42,13 @@ 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 `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). +- 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. This is open ended, there are many ways to implement this. diff --git a/exercise.main/Core.cs b/exercise.main/Core.cs index 7568220..bb19233 100644 --- a/exercise.main/Core.cs +++ b/exercise.main/Core.cs @@ -16,17 +16,64 @@ public class Core public bool winningPair(IEnumerable> hand, out Tuple result) { result = new Tuple(string.Empty, string.Empty); - - - - - + Dictionary valueToImageCards = new Dictionary() + { + {"J",11}, + {"Q",12}, + {"K",13}, + {"A",14}, + }; + int temp = 0; + + foreach (var cards in hand) + { + if (cards.Item1 == cards.Item2) + { + int sum = 0; + if (valueToImageCards.ContainsKey(cards.Item1)) + { + sum = valueToImageCards[cards.Item1] * 2; + } + else + { + sum = int.Parse(cards.Item1) * 2; + } + if (sum > temp) + { + temp = sum; + result = cards; + } + } + } return result.Item1!=string.Empty ? true : false; } public int GetValueOfCard(string card) { - return 0; + Dictionary valueToImageCards = new Dictionary() + { + {"J",11}, + {"Q",12}, + {"K",13}, + {"A",14}, + }; + return valueToImageCards.ContainsKey(card) ? valueToImageCards[card] : int.Parse(card); } } } +/* + * else + { + if (!pairFound) + { + int firstValue = valueToImageCards.ContainsKey(cards.Item1) ? valueToImageCards[cards.Item1] : int.Parse(cards.Item1); + int secondValue = valueToImageCards.ContainsKey(cards.Item2) ? valueToImageCards[cards.Item2] : int.Parse(cards.Item2); + int sum = firstValue + secondValue; + if (sum > temp) + { + temp = sum; + result = cards; + } + } + } + */ \ No newline at end of file diff --git a/exercise.main/Extension.cs b/exercise.main/Extension.cs index b33ba1f..0292917 100644 --- a/exercise.main/Extension.cs +++ b/exercise.main/Extension.cs @@ -12,8 +12,37 @@ public class Extension public bool winningThree(IEnumerable> hand, out Tuple result) { result = new Tuple(string.Empty, string.Empty, string.Empty); + Dictionary valueToImageCards = new Dictionary() + { + {"J",11}, + {"Q",12}, + {"K",13}, + {"A",14}, + }; + int temp = 0; - return false; + + foreach (var cards in hand) + { + if (cards.Item1 == cards.Item2 && cards.Item2 == cards.Item3) + { + int sum = 0; + if (valueToImageCards.ContainsKey(cards.Item1)) + { + sum = valueToImageCards[cards.Item1] * 3; + } + else + { + sum = int.Parse(cards.Item1) * 3; + } + if (sum > temp) + { + temp = sum; + result = cards; + } + } + } + return result.Item1 != string.Empty ? true : false; } } diff --git a/exercise.main/PokerGame/Card.cs b/exercise.main/PokerGame/Card.cs new file mode 100644 index 0000000..23245d8 --- /dev/null +++ b/exercise.main/PokerGame/Card.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace exercise.main.PokerGame +{ + internal class Card + { + public string _cardValue { get; set; } + public string suit { get; set; } + + public Card(string cardValue, string suit) + { + this._cardValue = cardValue; + this.suit = suit; + } + } +} diff --git a/exercise.main/PokerGame/Deck.cs b/exercise.main/PokerGame/Deck.cs new file mode 100644 index 0000000..1d37ef6 --- /dev/null +++ b/exercise.main/PokerGame/Deck.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace exercise.main.PokerGame +{ + internal class Deck + { + public List _cards { get; set; } + public Deck(List cards) + { + this._cards = cards; + } + + public List Shuffle(List cards) + { + Random random = new Random(); + for (int i = 0; i < cards.Count()-1; i++) + { + int newPlacement = random.Next(i, cards.Count()); + Card temp = cards[newPlacement]; + cards[newPlacement] = cards[i]; + cards[i] = temp; + } + return cards.ToList(); + } + + public Card Deal() + { + Random random = new Random(); + int index = random.Next(0, _cards.Count); + Card cardToDeal = _cards[index]; + this._cards.RemoveAt(index); + return cardToDeal; + } + } +} diff --git a/exercise.main/Program.cs b/exercise.main/Program.cs index 3751555..a2dcf55 100644 --- a/exercise.main/Program.cs +++ b/exercise.main/Program.cs @@ -1,2 +1,34 @@ // See https://aka.ms/new-console-template for more information +using exercise.main; +using exercise.main.PokerGame; + Console.WriteLine("Hello, World!"); + + + +List cards = new List() +{ + new Card("4", "heart"), + new Card("10", "spade"), + new Card("8", "heart"), + new Card("2", "spade") +}; +Deck deck = new Deck(cards); +Card rnd = deck.Deal(); + +Console.WriteLine($"val: {rnd._cardValue}, suite: {rnd.suit}"); + +/* +List> hand = new List> + { + new Tuple("4", "3"), + new Tuple("6", "6"), + new Tuple("7", "7"), + new Tuple("3","3") + }; +Tuple winner; +Core core = new Core(); +bool res = core.winningPair(hand, out winner); +Console.WriteLine(res); +*/ + diff --git a/exercise.tests/CoreTests.cs b/exercise.tests/CoreTests.cs index 5adefb3..47059a0 100644 --- a/exercise.tests/CoreTests.cs +++ b/exercise.tests/CoreTests.cs @@ -114,4 +114,26 @@ public void TestCards(string card, int value) Assert.That(result, Is.EqualTo(value)); } + [Test] + public void testTriplet() + { + Extension e = new Extension(); + + List> hand = new List> + { + new Tuple("K", "5","K"), + new Tuple("3","7", "K"), + new Tuple("9","9", "9") + }; + Tuple winner; + + bool result = e.winningThree(hand, out winner); + + Assert.That(result, Is.True); + Assert.That(winner.Item1, Is.EqualTo("9")); + Assert.That(winner.Item2, Is.EqualTo("9")); + Assert.That(winner.Item3, Is.EqualTo("9")); + + } + } \ No newline at end of file From 81994c14580e0295bf740ec1ef650eae86e00c10 Mon Sep 17 00:00:00 2001 From: Louis Date: Fri, 12 Jan 2024 08:24:19 +0100 Subject: [PATCH 2/2] poker game finished, but can be improved --- README.md | 5 +- exercise.main/PokerGame/Deck.cs | 41 +++++++++++++- exercise.main/PokerGame/Player.cs | 30 ++++++++++ exercise.main/PokerGame/PokerGame.cs | 84 ++++++++++++++++++++++++++++ exercise.main/Program.cs | 4 +- 5 files changed, 158 insertions(+), 6 deletions(-) create mode 100644 exercise.main/PokerGame/Player.cs create mode 100644 exercise.main/PokerGame/PokerGame.cs diff --git a/README.md b/README.md index 39e0dd1..081c4ef 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,9 @@ in the hand; a method for returning what cards are in the hand. 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. +- 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. This is open ended, there are many ways to implement this. diff --git a/exercise.main/PokerGame/Deck.cs b/exercise.main/PokerGame/Deck.cs index 1d37ef6..a23ff90 100644 --- a/exercise.main/PokerGame/Deck.cs +++ b/exercise.main/PokerGame/Deck.cs @@ -9,9 +9,9 @@ namespace exercise.main.PokerGame internal class Deck { public List _cards { get; set; } - public Deck(List cards) + public Deck() { - this._cards = cards; + this._cards = this.GenerateDeck(); } public List Shuffle(List cards) @@ -35,5 +35,42 @@ public Card Deal() this._cards.RemoveAt(index); return cardToDeal; } + + public List GenerateDeck() + { + string[] suties = new string[] { "heart","spade","something1", "something2"}; + Dictionary specialCards = new Dictionary() + { + {11, "J"}, + {12, "Q"}, + {13, "K"}, + }; + + List cards = new List(); + for (int i = 0; i < 4; i++) + { + Card newCard = new Card("A", suties[i]); + cards.Add(newCard); + } + + for (int i = 2; i <= 13; i++) + { + for (int j = 0; j < 4; j++) + { + if (specialCards.ContainsKey(i)) + { + Card newCard = new Card(specialCards[i], suties[j]); + cards.Add(newCard); + } + else + { + Card newCard = new Card(i.ToString(), suties[j]); + cards.Add(newCard); + } + } + } + return cards; + + } } } diff --git a/exercise.main/PokerGame/Player.cs b/exercise.main/PokerGame/Player.cs new file mode 100644 index 0000000..c110617 --- /dev/null +++ b/exercise.main/PokerGame/Player.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace exercise.main.PokerGame +{ + internal class Player + { + public string _name { get; set; } + public List _cards { get; set; } + + public Player(string name) + { + this._name = name; + + } + + public void AddCard(Card card) + { + this._cards.Add(card); + } + + public void ClearCards() + { + this._cards.Clear(); + } + } +} diff --git a/exercise.main/PokerGame/PokerGame.cs b/exercise.main/PokerGame/PokerGame.cs new file mode 100644 index 0000000..b558f38 --- /dev/null +++ b/exercise.main/PokerGame/PokerGame.cs @@ -0,0 +1,84 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace exercise.main.PokerGame +{ + internal class PokerGame + { + public PokerGame() + { + Console.WriteLine("generating deck"); + Deck deck = new Deck(); + Console.WriteLine("dealing cards"); + Player p1 = new Player("mandy"); + Player p2 = new Player("andy"); + List cardsOnTable = new List(); + List players = new List() { p1, p2 }; + + foreach (Player p in players) + { + List cards = new List(); + for (int i = 0; i < 2; i++) + { + cards.Add(deck.Deal()); + } + p._cards = cards; + } + Console.WriteLine("cards are dealt"); + + Console.WriteLine("dealing first three cards"); + for (int i = 0; i < 3; i++) + { + Card card = deck.Deal(); + cardsOnTable.Add(card); + } + + Console.WriteLine("dealing last two cards"); + + for (int i = 0; i < 2; i++) + { + Card card = deck.Deal(); + cardsOnTable.Add(card); + } + Console.WriteLine("finding the winner"); + string winner = getWinner(p1, p2, cardsOnTable); + Console.WriteLine("the winner is: " + winner); + } + + public string getWinner(Player player1, Player player2, List deck) + { + List players = new List() + { + player1, + player2, + }; + string winner = "draw"; + int occs = 0; + foreach (Player p in players) + { + int sumPair1 = 0; + int sumPair2 = 0; + + if (p._cards[0] == p._cards[1]) + { + sumPair1++; + sumPair2++; + } + + int occurence1 = deck.Count(d => d._cardValue == p._cards[0]._cardValue); + int occurence2 = deck.Count(d => d._cardValue == p._cards[1]._cardValue); + + int total = occurence1 >= occurence2 ? occurence1 : occurence2; + if (total > occs) + { + occs = total; + winner = p._name; + } + } + return winner; + } + } +} diff --git a/exercise.main/Program.cs b/exercise.main/Program.cs index a2dcf55..a61a837 100644 --- a/exercise.main/Program.cs +++ b/exercise.main/Program.cs @@ -13,10 +13,8 @@ new Card("8", "heart"), new Card("2", "spade") }; -Deck deck = new Deck(cards); -Card rnd = deck.Deal(); -Console.WriteLine($"val: {rnd._cardValue}, suite: {rnd.suit}"); +PokerGame p = new PokerGame(); /* List> hand = new List>