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
17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<string,string> 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

Expand All @@ -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.
Expand Down
25 changes: 25 additions & 0 deletions exercise.main/Card.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
38 changes: 35 additions & 3 deletions exercise.main/Core.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.Metrics;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
Expand All @@ -18,15 +19,46 @@ public class Core
//TODO: complete the winningPair method, without chaning the method signature
public bool winningPair(IEnumerable<Tuple<string, string>> hands, out Tuple<string, string> result)
{
result = new Tuple<string,string>(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<string,string>(highestFace, highestFace);

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


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<string> color = new List<string>
{
"hearts", "spades", "clubs", "diamonds"
};
List<string> face = new List<string>
{
"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;
}
}

}
47 changes: 46 additions & 1 deletion exercise.main/Extension.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;

Expand All @@ -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<string, int> cardValues = new Dictionary<string, int>();
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<Tuple<string, string, string>> hand, out Tuple<string, string, string> result)
{
result = new Tuple<string, string, string>(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<string, string, string>(highestFace, highestFace, highestFace);

return false;
}
Expand Down
31 changes: 31 additions & 0 deletions exercise.main/Player.cs
Original file line number Diff line number Diff line change
@@ -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<Card> cards;

public Player(string nameOfPlayer)
{
this.name = nameOfPlayer;
cards = new List<Card>();
}

public void AddCard(Card card)
{
Card newCard = new Card();
this.cards.Add(card);
}

public void EmptyHand()
{
cards.Clear();
}
}
}
95 changes: 95 additions & 0 deletions exercise.main/PokerGame.cs
Original file line number Diff line number Diff line change
@@ -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<Player> players = new List<Player>();
List<Card> board = new List<Card>();


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());

}


}
}
Loading