Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
38 changes: 31 additions & 7 deletions exercise.main/Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,46 @@ namespace exercise.main
public class Core
{


//TODO: complete the following method, keeping the signature the same
public bool winningPair(IEnumerable<Tuple<string, string>> hand, out Tuple<string, string> result)
{
result = new Tuple<string,string>(string.Empty, string.Empty);



string winningPair = string.Empty;
int winningValue = 0;


foreach(Tuple<string, string> pair in hand)
{
if(pair.Item1.Equals(pair.Item2) && GetValueOfCard(pair.Item1)>winningValue)
{
winningPair = pair.Item1;
winningValue = GetValueOfCard(pair.Item1);
}
}

result = new Tuple<string,string>(winningPair, winningPair);

return result.Item1!=string.Empty ? true : false;
}
public int GetValueOfCard(string card)
{
return 0;
{
Dictionary<string,int> lookup = new Dictionary<string, int>()
{
{"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];
}
}
}
19 changes: 19 additions & 0 deletions exercise.main/Poker/Card.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
62 changes: 62 additions & 0 deletions exercise.main/Poker/Deck.cs
Original file line number Diff line number Diff line change
@@ -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<Card> _Deck = new Stack<Card>();

public Deck()
{
foreach(string value in _Values)
{
foreach(string suit in _Suits)
{
_Deck.Push(new Card(value, suit));
}
}

ShuffleCards();
}
public void ShuffleCards()
{
List<Card> cards = new List<Card>(_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<Card> GetCards()
{
return _Deck;
}

public Card Deal()
{
return _Deck.Pop();
}
}
}
38 changes: 38 additions & 0 deletions exercise.main/Poker/Player.cs
Original file line number Diff line number Diff line change
@@ -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<Card> Cards { get; set; }
public Player(string name)
{
Name = name;
Cards = new List<Card>();
}
public Player()
{
Name = "John Doe(anon)";
Cards = new List<Card>();
}

public void TakeCard(Card card)
{
Cards.Add(card);
}
public List<Card> ShowCards()
{
return Cards;
}

public void ThrowCards()
{
Cards.Clear();
}
}
}
92 changes: 92 additions & 0 deletions exercise.main/Poker/PokerGame.cs
Original file line number Diff line number Diff line change
@@ -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<Player> Players;
public Deck Deck;
public List<Card> Table;
public PokerGame()
{
Players = new List<Player>();

Player player1 = new Player("Martin");
JoinGame(player1);

Player player2 = new Player();
JoinGame(player2);

Deck = new Deck();
Table = new List<Card>();
}

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<string, int> lookup = new Dictionary<string, int>()
{
{"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<Tuple<Player, List<Card>>> GetResults()
{

List<Tuple<Player, List<Card>>> results = new List<Tuple<Player, List<Card>>>();

foreach (Player p in Players)
{
List<Card> allCards = p.ShowCards().Concat(Table).ToList();

var bestGroup = allCards.GroupBy(card => GetValueOfCard(card.Value))
.OrderByDescending(group => group.Count())
.ThenByDescending(group => group.Key)
.First();

Tuple<Player, List<Card>> 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;
}
}
}
Loading