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
24 changes: 18 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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<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 +42,16 @@ 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).
- 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.
- 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.
59 changes: 53 additions & 6 deletions exercise.main/Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,64 @@ public class Core
public bool winningPair(IEnumerable<Tuple<string, string>> hand, out Tuple<string, string> result)
{
result = new Tuple<string,string>(string.Empty, string.Empty);





Dictionary<string,int> valueToImageCards = new Dictionary<string,int>()
{
{"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<string, int> valueToImageCards = new Dictionary<string, int>()
{
{"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;
}
}
}
*/
31 changes: 30 additions & 1 deletion exercise.main/Extension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,37 @@ public class Extension
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);
Dictionary<string, int> valueToImageCards = new Dictionary<string, int>()
{
{"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;
}

}
Expand Down
20 changes: 20 additions & 0 deletions exercise.main/PokerGame/Card.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
76 changes: 76 additions & 0 deletions exercise.main/PokerGame/Deck.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
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<Card> _cards { get; set; }
public Deck()
{
this._cards = this.GenerateDeck();
}

public List<Card> Shuffle(List<Card> 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;
}

public List<Card> GenerateDeck()
{
string[] suties = new string[] { "heart","spade","something1", "something2"};
Dictionary<int, string> specialCards = new Dictionary<int, string>()
{
{11, "J"},
{12, "Q"},
{13, "K"},
};

List<Card> cards = new List<Card>();
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;

}
}
}
30 changes: 30 additions & 0 deletions exercise.main/PokerGame/Player.cs
Original file line number Diff line number Diff line change
@@ -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<Card> _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();
}
}
}
84 changes: 84 additions & 0 deletions exercise.main/PokerGame/PokerGame.cs
Original file line number Diff line number Diff line change
@@ -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<Card> cardsOnTable = new List<Card>();
List<Player> players = new List<Player>() { p1, p2 };

foreach (Player p in players)
{
List<Card> cards = new List<Card>();
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<Card> deck)
{
List<Player> players = new List<Player>()
{
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;
}
}
}
Loading