To determine the highest poker hand ranking from a given set of five cards in C#, we'll follow these steps:
-
Parse the Cards:
- Extract the rank and suit from each card.
- Assign numeric values to the ranks to facilitate easier comparison (for example, "2" = 2, "J" = 11, "Q" = 12, "K" = 13, "A" = 14).
-
Count Frequencies:
- Count the frequency of each rank and each suit.
- This helps in identifying pairs, three of a kind, four of a kind, etc.
-
Determine Flush and Straight:
- Flush: All cards have the same suit.
- Straight: Five consecutive ranks. Special attention is needed for the Ace, which can be either low (preceding 2) or high (following King).
-
Identify Specific Hands:
- Check for Royal Flush, Straight Flush, Four of a Kind, Full House, Flush, Straight, Three of a Kind, Two Pair, Pair, and High Card in that order of precedence.
Here's a comprehensive C# implementation based on the above approach:
using System;
using System.Collections.Generic;
using System.Linq;
public class PokerHandEvaluator
{
public static string PokerHandRanking(string[] hand)
{
// Define rank mapping
Dictionary<string, int> rankMap = new Dictionary<string, int>()
{
{"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}
};
// Extract ranks and suits
List<int> ranks = new List<int>();
List<char> suits = new List<char>();
foreach(var card in hand)
{
string rankStr = "";
char suit;
if (card.Length == 3) // "10c"
{
rankStr = card.Substring(0,2);
suit = card[2];
}
else // "Ah", "Ks", etc.
{
rankStr = card.Substring(0,1);
suit = card[1];
}
if (!rankMap.ContainsKey(rankStr))
throw new ArgumentException($"Invalid card rank: {rankStr}");
ranks.Add(rankMap[rankStr]);
suits.Add(suit);
}
ranks.Sort();
// Count ranks and suits
var rankCounts = ranks.GroupBy(x => x).Select(g => g.Count()).OrderByDescending(x => x).ToList();
var uniqueRanks = ranks.Distinct().Count();
var isFlush = suits.Distinct().Count() == 1;
// Check for straight
bool isStraight = false;
// Standard straight
if (ranks.SequenceEqual(Enumerable.Range(ranks[0], 5)))
{
isStraight = true;
}
// Ace-low straight (A,2,3,4,5)
else if (ranks.SequenceEqual(new List<int> {2,3,4,5,14}))
{
isStraight = true;
}
// Determine hand ranking
if (isFlush && isStraight && ranks.Max() == 14 && ranks.Min() == 10)
return "Royal Flush";
if (isFlush && isStraight)
return "Straight Flush";
if (rankCounts[0] == 4)
return "Four of a Kind";
if (rankCounts[0] == 3 && rankCounts[1] == 2)
return "Full House";
if (isFlush)
return "Flush";
if (isStraight)
return "Straight";
if (rankCounts[0] == 3)
return "Three of a Kind";
if (rankCounts[0] == 2 && rankCounts[1] == 2)
return "Two Pair";
if (rankCounts[0] == 2)
return "Pair";
return "High Card";
}
// For testing purposes
public static void Main()
{
var hand1 = new string[] { "10h", "Jh", "Qh", "Ah", "Kh" };
Console.WriteLine(PokerHandRanking(hand1)); // Royal Flush
var hand2 = new string[] { "3h", "5h", "Qs", "9h", "Ad" };
Console.WriteLine(PokerHandRanking(hand2)); // High Card
var hand3 = new string[] { "10s", "10c", "8d", "10d", "10h" };
Console.WriteLine(PokerHandRanking(hand3)); // Four of a Kind
var hand4 = new string[] { "2h", "3d", "5s", "9c", "Kd" };
Console.WriteLine(PokerHandRanking(hand4)); // High Card
var hand5 = new string[] { "2h", "2d", "4c", "4d", "4s" };
Console.WriteLine(PokerHandRanking(hand5)); // Full House
var hand6 = new string[] { "3c", "3d", "3s", "9s", "9d" };
Console.WriteLine(PokerHandRanking(hand6)); // Full House
var hand7 = new string[] { "Ah", "2h", "3h", "4h", "5h" };
Console.WriteLine(PokerHandRanking(hand7)); // Straight Flush
var hand8 = new string[] { "Ah", "Kh", "Qh", "Jh", "10h" };
Console.WriteLine(PokerHandRanking(hand8)); // Royal Flush
}
}
Explanation of the Code:
-
Rank Mapping:
- A dictionary
rankMap maps the string representation of each rank to its numeric value for easy comparison.
-
Parsing the Hand:
- For each card in the hand, extract the rank and suit.
- Handle cases where the rank is "10" (which has two characters) separately from single-character ranks.
-
Sorting and Counting:
- Sort the ranks to facilitate straight checking.
- Use LINQ to group and count occurrences of each rank and suit.
-
Checking for Flush:
- All suits are the same if the count of distinct suits is 1.
-
Checking for Straight:
- A standard straight has consecutive ranks.
- An Ace-low straight is a special case (A, 2, 3, 4, 5).
-
Determining the Highest Hand:
- Check for each hand ranking in order of precedence:
- Royal Flush
- Straight Flush
- Four of a Kind
- Full House
- Flush
- Straight
- Three of a Kind
- Two Pair
- Pair
- High Card
-
Testing:
- The
Main method includes several test cases to validate the correctness of the function.
Additional Notes:
-
Error Handling:
- The function throws an
ArgumentException if an invalid card rank is encountered.
-
Ace Handling:
- The Ace can act as the highest card (
14) or the lowest card (1) in a straight. The code accounts for the Ace-low straight by specifically checking for the sequence {2, 3, 4, 5, 14}.
-
Performance:
- The use of LINQ ensures efficient grouping and counting of ranks and suits.
Sample Output:
Royal Flush
High Card
Four of a Kind
High Card
Full House
Full House
Straight Flush
Royal Flush
This implementation should accurately determine the highest poker hand ranking for any given set of five cards.