diff --git a/README.md b/README.md index bbb4afb..74c1b8f 100644 --- a/README.md +++ b/README.md @@ -22,12 +22,14 @@ 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 +40,15 @@ 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/Card.cs b/exercise.main/Card.cs new file mode 100644 index 0000000..bfe9a0a --- /dev/null +++ b/exercise.main/Card.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace exercise.main +{ + public class Card + { + public int cardValue = 0; // 2-A + public string color = string.Empty; // spade, diamond, club or heart + public string face = string.Empty; // 2 3 4 5 6 7 8 9 10 J Q K A + + public string RepresentCard() //returns a nice looking representation of the card + { + string theCard = this.face + " of " + this.color; + + + + return theCard; + } + } +} \ No newline at end of file diff --git a/exercise.main/Core.cs b/exercise.main/Core.cs index bf5077d..e7202ff 100644 --- a/exercise.main/Core.cs +++ b/exercise.main/Core.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics.Metrics; using System.Linq; using System.Reflection; using System.Runtime.InteropServices; @@ -18,15 +19,46 @@ public class Core //TODO: complete the winningPair method, without chaning the method signature public bool winningPair(IEnumerable> hands, out Tuple result) { - result = new Tuple(string.Empty, string.Empty); + int highestPair = 0; + string highestFace = string.Empty; + foreach (var pair in hands) + { + if (pair.Item1 == pair.Item2) //is pair + { + if (GetValueOfCard(pair.Item2) > highestPair) + { + highestPair = GetValueOfCard(pair.Item2); + highestFace = pair.Item2; + } + } + } - // your code here... + result = new Tuple(highestFace, highestFace); return result.Item1!=string.Empty ? true : false; } public int GetValueOfCard(string card) { - return 0; + Dictionary cardValues = new Dictionary(); + cardValues.Add("2", 2); + cardValues.Add("3", 3); + cardValues.Add("4", 4); + cardValues.Add("5", 5); + cardValues.Add("6", 6); + cardValues.Add("7", 7); + cardValues.Add("8", 8); + cardValues.Add("9", 9); + cardValues.Add("10", 10); + cardValues.Add("J", 11); + cardValues.Add("Q", 12); + cardValues.Add("K", 13); + cardValues.Add("A", 14); + + if(cardValues.ContainsKey(card)) + { + return cardValues[card]; + } + return 0; //failed case } } } diff --git a/exercise.main/Deck.cs b/exercise.main/Deck.cs new file mode 100644 index 0000000..1ff9654 --- /dev/null +++ b/exercise.main/Deck.cs @@ -0,0 +1,70 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + + +namespace exercise.main +{ + public class Deck + { + public List deck = new List(); + + + public void ShuffleDeck() + { + + Random random = new Random(); + int n = deck.Count; + while(n > 1) + { + n--; + int j = random.Next(n + 1); + Card temp = deck[j]; + deck[j] = deck[n]; + deck[n] = temp; + } + } + + + + public void InitializeDeck() + { + deck.Clear(); // remove all old cards + + List color = new List + { + "hearts", "spades", "clubs", "diamonds" + }; + List face = new List + { + "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A" + }; + + + for (int i = 0; i < face.Count; i++) // for each of the 12 f + { + foreach (var col in color) + { + Card card = new Card(); + card.color = col; + card.cardValue = i + 2; // as i goes from 0-12 and value goes from 2-14 + card.face = face[i]; + deck.Add(card); + + } + } + } + + public Card DealCard() + { + Card card = new Card(); + + card = deck[0]; + deck.RemoveAt(0); + return card; + } + } + +} diff --git a/exercise.main/Extension.cs b/exercise.main/Extension.cs index b33ba1f..8ea85fc 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,9 +10,53 @@ namespace exercise.main public class Extension { //TODO: complete the following method, keeping the signature the same + + public int GetValueOfCard(string card) + { + Dictionary cardValues = new Dictionary(); + cardValues.Add("2", 2); + cardValues.Add("3", 3); + cardValues.Add("4", 4); + cardValues.Add("5", 5); + cardValues.Add("6", 6); + cardValues.Add("7", 7); + cardValues.Add("8", 8); + cardValues.Add("9", 9); + cardValues.Add("10", 10); + cardValues.Add("J", 11); + cardValues.Add("Q", 12); + cardValues.Add("K", 13); + cardValues.Add("A", 14); + + if (cardValues.ContainsKey(card)) + { + return cardValues[card]; + } + return 0; //failed case + } public bool winningThree(IEnumerable> hand, out Tuple result) { - result = new Tuple(string.Empty, string.Empty, string.Empty); + + int highestPair = 0; + string highestFace = string.Empty; + foreach (var triple in hand) + { + if (triple.Item1 == triple.Item2 && triple.Item1 == triple.Item3) //is triple + { + if (GetValueOfCard(triple.Item2) > highestPair) + { + highestPair = GetValueOfCard(triple.Item2); + highestFace = triple.Item2; + } + } + } + + + + + + + result = new Tuple(highestFace, highestFace, highestFace); return false; } diff --git a/exercise.main/Player.cs b/exercise.main/Player.cs new file mode 100644 index 0000000..35d7203 --- /dev/null +++ b/exercise.main/Player.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace exercise.main +{ + public class Player + { + public string name; + public List cards; + + public Player(string nameOfPlayer) + { + this.name = nameOfPlayer; + cards = new List(); + } + + public void AddCard(Card card) + { + Card newCard = new Card(); + this.cards.Add(card); + } + + public void EmptyHand() + { + cards.Clear(); + } + } +} diff --git a/exercise.main/PokerGame.cs b/exercise.main/PokerGame.cs new file mode 100644 index 0000000..256ce22 --- /dev/null +++ b/exercise.main/PokerGame.cs @@ -0,0 +1,95 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace exercise.main +{ + public class PokerGame + { + Deck deck = new Deck(); + List players = new List(); + List board = new List(); + + + public PokerGame(string player1Name, string player2Name) + { + Player player1 = new Player(player1Name); + Player player2 = new Player(player2Name); + players.Add(player1); + players.Add(player2); + } + + private string CreateBoardString() + { + string text = ""; + foreach (var card in board) + { + text = text + card.RepresentCard() + ", "; + } + text = text.Remove(text.Length - 2); + return text; + } + + public void RunAGame() + { + Console.WriteLine("New game started!\nDealing cards!"); + + foreach (var player in players) + { + player.EmptyHand(); + } + board.Clear(); + + // start by generating the deck. + deck.InitializeDeck(); + + //then we shuffle the deck + deck.ShuffleDeck(); + + + //as in real texas holdem, we yeet the first card + deck.DealCard(); + + //time to deal cards to players in this order 0,0 -> 1,0 ->1,1 -> 2,1 -> 2,2 + foreach (var player in players) + { + player.AddCard(deck.DealCard()); + } + foreach (var player in players) + { + player.AddCard(deck.DealCard()); + } + + foreach (var player in players) + { + Console.WriteLine(player.name + "'s" + " hand: " + player.cards[0].RepresentCard() + " and " + player.cards[1].RepresentCard()); + } + + Console.WriteLine("Dealing the flop: "); + // deal first 3 cards, also known as the flop + for (int i = 0; i < 3; i++) + { + board.Add(deck.DealCard()); + } + + Console.WriteLine("Flop shows: " + CreateBoardString()); + + // as in texas holdem, we yeet one card before the turn + deck.DealCard(); + board.Add(deck.DealCard()); + + Console.WriteLine("Turn shows: " + CreateBoardString()); + + // as in texas holdem, we yeet one card before the river + deck.DealCard(); + board.Add(deck.DealCard()); + + Console.WriteLine("River shows: " + CreateBoardString()); + + } + + + } +} diff --git a/exercise.main/Program.cs b/exercise.main/Program.cs index 3751555..a7181e9 100644 --- a/exercise.main/Program.cs +++ b/exercise.main/Program.cs @@ -1,2 +1,27 @@ // See https://aka.ms/new-console-template for more information -Console.WriteLine("Hello, World!"); + +using exercise.main; + +Console.WriteLine("Enter player 1 name: "); +string player1Name = Console.ReadLine(); + +Console.WriteLine("Enter player 2 name: "); +string player2Name = Console.ReadLine(); + +PokerGame pokerGame = new PokerGame(player1Name, player2Name); + + + +bool running = true; +while(running) +{ + Console.WriteLine("Would you like to play a game of texas hold 'em?\nPress ENTER to deal or enter 'no' to exit!"); + string answer = Console.ReadLine(); + if(answer == "no") + { + running = false; + break; + } + + pokerGame.RunAGame(); +} \ No newline at end of file