Skip to content

Instantly share code, notes, and snippets.

@JL-Cox
Last active December 28, 2018 20:45
Show Gist options
  • Save JL-Cox/86a87cbddc885818a97b3e27b7be9cec to your computer and use it in GitHub Desktop.
Save JL-Cox/86a87cbddc885818a97b3e27b7be9cec to your computer and use it in GitHub Desktop.
CodeWars.com Kata (Ranking Poker Hands)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
namespace TexasHoldem
{
public class Program
{
static void Main(string[] args)
{
// Create the hands (Not in Kata)
//var myHand = CreateHand();
//var theirHand = CreateHand();
//Evaluate the hands
//Console.WriteLine($"My Hand: {myHand}");
//Console.WriteLine($"Thier Hand: {theirHand}\n");
var result = new PokerHand("JS QS 9H TS KH").CompareWith(new PokerHand("QC KH TS JS AH"));
Console.ReadKey();
}
public static string CreateHand()
{
string[] faces = { "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A" };
string[] suits = { "H", "D", "C", "S" };
Random random = new Random();
var hand = "";
for (int i = 0; i < 5; i++)
{
var face = faces[random.Next(0, 12)];
var suit = suits[random.Next(0, 3)];
hand += string.Join("", face, suit, " ");
}
return hand.Trim();
}
}
}
public enum Result
{
Win,
Loss,
Tie
}
public class PokerHand
{
//
public int score { get; set; }
public string description { get; set; }
public bool straight { get; set; }
public bool flush { get; set; }
public int highcard { get; set; }
public string cards { get; set; }
public int total_val { get; set; }
public List<int> handList = new List<int>();
Dictionary<string, int> faces = new Dictionary<string, int>()
{
{"aces", 0},
{"kings", 0},
{"queens", 0},
{"jacks", 0},
{"tens", 0},
{"nines", 0},
{"eights", 0},
{"sevens", 0},
{"sixes", 0},
{"fives", 0},
{"fours", 0},
{"threes", 0},
{"twos", 0}
};
Dictionary<string, int> suits = new Dictionary<string, int>()
{
{"hearts", 0},
{"clubs", 0},
{"diamonds", 0},
{"spades", 0}
};
public PokerHand(string hand)
{
this.cards = hand;
}
public static int EvaluateHand(PokerHand hand)
{
Regex reg_Nums = new Regex(@"\d+"); // find digit face values
Regex reg_Letters = new Regex(@"\D"); // find non-digit face/suit values
Match match_Nums = reg_Nums.Match(hand.cards);
Match match_Letters = reg_Letters.Match(hand.cards);
// Regex catches number values and records them in this.dictionary
while (match_Nums.Success)
{
switch (int.Parse(match_Nums.Value))
{
case 2:
hand.faces["twos"]++;
break;
case 3:
hand.faces["threes"]++;
break;
case 4:
hand.faces["fours"]++;
break;
case 5:
hand.faces["fives"]++;
break;
case 6:
hand.faces["sixes"]++;
break;
case 7:
hand.faces["sevens"]++;
break;
case 8:
hand.faces["eights"]++;
break;
case 9:
hand.faces["nines"]++;
break;
default:
break;
}
match_Nums = match_Nums.NextMatch();
}
// Regex catches letter values and records them in this.dictionary
while (match_Letters.Success)
{
switch (match_Letters.Value)
{
case "A":
hand.faces["aces"]++;
break;
case "K":
hand.faces["kings"]++;
break;
case "Q":
hand.faces["queens"]++;
break;
case "J":
hand.faces["jacks"]++;
break;
case "H":
hand.suits["hearts"]++;
break;
case "D":
hand.suits["diamonds"]++;
break;
case "C":
hand.suits["clubs"]++;
break;
case "S":
hand.suits["spades"]++;
break;
case "T":
hand.faces["tens"]++;
break;
default:
break;
}
match_Letters = match_Letters.NextMatch();
}
// Check for straight
while (true)
{
if (hand.faces["aces"] == 1 && hand.faces["kings"] == 1 && hand.faces["queens"] == 1 && hand.faces["jacks"] == 1 && hand.faces["tens"] == 1)
{
hand.straight = true;
break;
}
if (hand.faces["kings"] == 1 && hand.faces["queens"] == 1 && hand.faces["jacks"] == 1 && hand.faces["tens"] == 1 && hand.faces["nines"] == 1)
{
hand.straight = true;
break;
}
if (hand.faces["queens"] == 1 && hand.faces["jacks"] == 1 && hand.faces["tens"] == 1 && hand.faces["nines"] == 1 && hand.faces["eights"] == 1)
{
hand.straight = true;
break;
}
if (hand.faces["jacks"] == 1 && hand.faces["tens"] == 1 && hand.faces["nines"] == 1 && hand.faces["eights"] == 1 && hand.faces["sevens"] == 1)
{
hand.straight = true;
break;
}
if (hand.faces["tens"] == 1 && hand.faces["nines"] == 1 && hand.faces["eights"] == 1 && hand.faces["sevens"] == 1 && hand.faces["sixes"] == 1)
{
hand.straight = true;
break;
}
if (hand.faces["nines"] == 1 && hand.faces["eights"] == 1 && hand.faces["sevens"] == 1 && hand.faces["sixes"] == 1 && hand.faces["fives"] == 1)
{
hand.straight = true;
break;
}
if (hand.faces["eights"] == 1 && hand.faces["sevens"] == 1 && hand.faces["sixes"] == 1 && hand.faces["fives"] == 1 && hand.faces["fours"] == 1)
{
hand.straight = true;
break;
}
if (hand.faces["sevens"] == 1 && hand.faces["sixes"] == 1 && hand.faces["fives"] == 1 && hand.faces["fours"] == 1 && hand.faces["threes"] == 1)
{
hand.straight = true;
break;
}
if (hand.faces["sixes"] == 1 && hand.faces["fives"] == 1 && hand.faces["fours"] == 1 && hand.faces["threes"] == 1 && hand.faces["twos"] == 1)
{
hand.straight = true;
break;
}
if (hand.faces["fives"] == 1 && hand.faces["fours"] == 1 && hand.faces["threes"] == 1 && hand.faces["twos"] == 1 && hand.faces["aces"] == 1)
{
hand.straight = true;
break;
}
hand.straight = false;
break;
}
// Check for flush
while (true)
{
if (hand.suits.Where(x => x.Value == 5).Count() > 0)
{
hand.flush = true;
break;
}
else
{
hand.flush = false;
break;
}
}
// Add card face values to list for finding highcard value later in CompareWith()
if (true)
{
if (hand.faces["aces"] >= 1)
{
for (int i = 0; i < hand.faces["aces"]; i++)
{
hand.handList.Add(14);
}
hand.handList.OrderByDescending(x => x);
}
if (hand.faces["kings"] >= 1)
{
for (int i = 0; i < hand.faces["kings"]; i++)
{
hand.handList.Add(13);
}
hand.handList.Sort();
}
if (hand.faces["queens"] >= 1)
{
for (int i = 0; i < hand.faces["queens"]; i++)
{
hand.handList.Add(12);
}
hand.handList.Sort();
}
if (hand.faces["jacks"] >= 1)
{
for (int i = 0; i < hand.faces["jacks"]; i++)
{
hand.handList.Add(11);
}
hand.handList.Sort();
}
if (hand.faces["tens"] >= 1)
{
for (int i = 0; i < hand.faces["tens"]; i++)
{
hand.handList.Add(10);
}
hand.handList.Sort();
}
if (hand.faces["nines"] >= 1)
{
for (int i = 0; i < hand.faces["nines"]; i++)
{
hand.handList.Add(9);
}
hand.handList.Sort();
}
if (hand.faces["eights"] >= 1)
{
for (int i = 0; i < hand.faces["eights"]; i++)
{
hand.handList.Add(8);
}
hand.handList.Sort();
}
if (hand.faces["sevens"] >= 1)
{
for (int i = 0; i < hand.faces["sevens"]; i++)
{
hand.handList.Add(7);
}
hand.handList.Sort();
}
if (hand.faces["sixes"] >= 1)
{
for (int i = 0; i < hand.faces["sixes"]; i++)
{
hand.handList.Add(6);
}
hand.handList.Sort();
}
if (hand.faces["fives"] >= 1)
{
for (int i = 0; i < hand.faces["fives"]; i++)
{
hand.handList.Add(5);
}
hand.handList.Sort();
}
if (hand.faces["fours"] >= 1)
{
for (int i = 0; i < hand.faces["fours"]; i++)
{
hand.handList.Add(4);
}
hand.handList.Sort();
}
if (hand.faces["threes"] >= 1)
{
for (int i = 0; i < hand.faces["threes"]; i++)
{
hand.handList.Add(3);
}
hand.handList.Sort();
}
if (hand.faces["twos"] >= 1)
{
for (int i = 0; i < hand.faces["twos"]; i++)
{
hand.handList.Add(2);
}
hand.handList.Sort();
}
hand.handList.Sort();
}
//1.Royal flush (1000)
if (hand.flush == true && hand.straight == true && hand.faces["aces"] == 1)
{
hand.score = 1000 + hand.highcard;
hand.description = "Royal Flush!!!";
return hand.score;
}
//2.Straight flush (750 + HC)
else if (hand.straight == true && hand.flush == true)
{
hand.score = 750 + hand.highcard;
hand.description = "Straight Flush!!";
return hand.score;
}
//3.Four of a kind (500 + 4-card value)
else if (hand.faces.Where(x => x.Value == 4).Count() == 1)
{
hand.score = 500 + hand.highcard;
hand.description = "Four of a kind!";
return hand.score;
}
//4.Full house (400 + 3-card value)
else if (hand.faces.Where(x => x.Value == 3).Count() == 1 && hand.faces.Where(x => x.Value == 2).Count() == 1)
{
hand.score = 400 + hand.highcard;
hand.description = "Full House!";
return hand.score;
}
//5.Flush (300 + HC)
else if (hand.flush == true && hand.straight == false)
{
hand.score = 300 + hand.highcard;
hand.description = "Flush!";
return hand.score;
}
//6.Straight (250 + HC)
else if (hand.straight == true && hand.flush == false)
{
hand.score = 250 + hand.highcard;
hand.description = "Straight!";
return hand.score;
}
//7.Three of a kind (200 + card value)
else if (hand.faces.Where(x => x.Value == 3).Count() == 1)
{
hand.score = 200 + hand.highcard;
hand.description = "Three of a kind!";
return hand.score;
}
//8.Two pair (100 + card value)
else if (hand.faces.Where(x => x.Value == 2).Count() == 2)
{
hand.score = 100 + hand.highcard;
hand.description = "Two pair!";
return hand.score;
}
//9.Pair (50 + card value)
else if (hand.faces.Where(x => x.Value == 2).Count() == 1)
{
hand.score = 50 + hand.highcard;
hand.description = "Pair!";
return hand.score;
}
//10.High Card (card value)
else
{
hand.score = hand.highcard;
hand.description = "High Card!";
return hand.score;
}
}
public Result CompareWith(PokerHand hand)
{
var mine = EvaluateHand(this);
var theirs = EvaluateHand(hand);
if (mine > theirs)
{
return Result.Win;
}
else if (mine < theirs)
{
return Result.Loss;
}
else
{
for (int i = 4; i >= 0; i--)
{
if (this.handList[i] > hand.handList[i])
{
return Result.Win;
}
else if (this.handList[i] < hand.handList[i])
{
return Result.Loss;
}
if (i == 0)
{
break;
}
}
return Result.Tie;
}
}
}
//// faces
//public string cards { get; set; }
//public int aces { get; set; }
//public int kings { get; set; }
//public int queens { get; set; }
//public int jacks { get; set; }
//public int tens { get; set; }
//public int nines { get; set; }
//public int eights { get; set; }
//public int sevens { get; set; }
//public int sixes { get; set; }
//public int fives { get; set; }
//public int fours { get; set; }
//public int threes { get; set; }
//public int twos { get; set; }
//// suits
//public int hearts { get; set; }
//public int diamonds { get; set; }
//public int clubs { get; set; }
//public int spades { get; set; }
// Testing methods
//PokerHand myHandObj = new PokerHand("AS AC AH AD 10S");
//PokerHand theirHandObj = new PokerHand("3C 3S 3H 2S 2C");
//var mine = PokerHand.EvaluateHand(myHandObj);
//var theirs = PokerHand.EvaluateHand(theirHandObj);
//Console.WriteLine($"\n\nA: {hand.aces}\nK: {hand.kings}\nQ: {hand.queens}\n" +
//$"J: {hand.jacks}\n10: {hand.tens}\n9: {hand.nines}\n" +
//$"8: {hand.eights}\n7: {hand.sevens}\n6: {hand.sixes}\n" +
//$"5: {hand.fives}\n4: {hand.fours}\n3: {hand.threes}\n2: {hand.twos}\n\n" +
//$"H: {hand.hearts}\nD: {hand.diamonds}\nC: {hand.clubs}\nS: {hand.spades}\n\n");
//if (hand.faces["aces"] >= 1)
// {
// current_val = 14;
// total_val += current_val;
// if (current_val > highcard)
// {
// highcard = current_val;
// }
// }
//Console.WriteLine($"Yours: {this.cards} :: Theirs {hand.cards}");
//Console.WriteLine($"Your Hand: {this.description} :: Their Hand: {hand.description}");
//Console.WriteLine($"Your Score: {this.score} :: Their Score: {hand.score}");
//Console.WriteLine("Players Tie!");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment