ze poker hand checker

This commit is contained in:
Gardient
2016-07-01 00:31:12 +03:00
committed by Petrutiu Mihai
parent 0a6d290c3e
commit b5997ced99
34 changed files with 612 additions and 183 deletions

View File

@@ -0,0 +1,16 @@
using ChainOfResponssibility.Validators;
using System;
using System.Linq;
namespace ChainOfResponssibility.Poker.Validators
{
public class CardSuiteValidation : ChainValidation<Card>
{
protected override ValidationResult IsValid(Card obj)
{
if (Card.ValidSuites.Contains(obj.Suite))
return ValidationResult.GetValidResult();
return ValidationResult.GetInvalidResult(new Exception("Invalid Suite, must be one of " + Card.ValidSuites));
}
}
}

View File

@@ -0,0 +1,24 @@
using ChainOfResponssibility.Validators;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace ChainOfResponssibility.Poker.Validators
{
public class CardValidationFactory
{
private static ChainValidation<Card> _validator;
public static ChainValidation<Card> GetValidator()
{
if (_validator != null)
return _validator;
_validator = new CardSuiteValidation();
_validator.SetSuccessor(new CardValueValidation());
return _validator;
}
}
}

View File

@@ -0,0 +1,16 @@
using ChainOfResponssibility.Validators;
using System;
using System.Linq;
namespace ChainOfResponssibility.Poker.Validators
{
public class CardValueValidation : ChainValidation<Card>
{
protected override ValidationResult IsValid(Card obj)
{
if (Card.ValidValues.Contains(obj.Value))
return ValidationResult.GetValidResult();
return ValidationResult.GetInvalidResult(new Exception("Invalid Value, must be one of " + Card.ValidValues));
}
}
}

View File

@@ -0,0 +1,17 @@
using System.Linq;
namespace ChainOfResponssibility.Poker.Validators.HandValidators
{
public class FlushValidation : HandValidation
{
protected override string IsValid(Card[] hand)
{
var gr = hand.GroupBy(x => x.Suite);
var flush = gr.FirstOrDefault(x => x.Count() == 5);
if (flush != null)
return "You have a Flush of " + flush.First().Suite + "s";
return string.Empty;
}
}
}

View File

@@ -0,0 +1,17 @@
using System.Linq;
namespace ChainOfResponssibility.Poker.Validators.HandValidators
{
public class FourOfAKindValidation : HandValidation
{
protected override string IsValid(Card[] hand)
{
var gr = hand.GroupBy(x => x.Value);
var four = gr.FirstOrDefault(x => x.Count() == 4);
if (four != null)
return "You have Four of " + four.First().Value + "s (Four of a kind)";
return string.Empty;
}
}
}

View File

@@ -0,0 +1,21 @@
using System.Linq;
namespace ChainOfResponssibility.Poker.Validators.HandValidators
{
public class FullHouseValidation : HandValidation
{
protected override string IsValid(Card[] hand)
{
var gr = hand.GroupBy(x => x.Value);
var pair = gr.FirstOrDefault(x => x.Count() == 2);
if (pair != null)
{
var OhBabyATripple = gr.FirstOrDefault(x => x.Count() == 3);
if (OhBabyATripple != null)
return "You have a Full House of " + OhBabyATripple.First().Value + "s (3) and " + pair.First().Value + "s (2)";
}
return string.Empty;
}
}
}

View File

@@ -0,0 +1,28 @@
namespace ChainOfResponssibility.Poker.Validators.HandValidators
{
public abstract class HandValidation
{
public HandValidation Successor { get; private set; }
public HandValidation SetSuccessor(HandValidation successor)
{
return Successor = successor;
}
protected abstract string IsValid(Card[] hand);
public string Validate(Card[] hand)
{
string result = IsValid(hand);
if (!string.IsNullOrWhiteSpace(result))
return result;
if (Successor != null)
return Successor.Validate(hand);
else
return result;
}
}
}

View File

@@ -0,0 +1,45 @@
namespace ChainOfResponssibility.Poker.Validators.HandValidators
{
public class HandValidationFactory
{
private static HandValidation _cachedValidation;
public static HandValidation GetValidation()
{
if (_cachedValidation != null)
return _cachedValidation;
var royalflush = new RoyalFlushValidation();
var straightflush = new StraightFlushValidation();
royalflush.SetSuccessor(straightflush);
var fourofakind = new FourOfAKindValidation();
straightflush.SetSuccessor(fourofakind);
var fullhouse = new FullHouseValidation();
fourofakind.SetSuccessor(fullhouse);
var flush = new FlushValidation();
fullhouse.SetSuccessor(flush);
var straight = new StraightValidation();
flush.SetSuccessor(straight);
var threeofakind = new ThreeOfAKindValidation();
straight.SetSuccessor(threeofakind);
var twopairs = new TwoPairsValidation();
threeofakind.SetSuccessor(twopairs);
var pairs = new PairValidation();
twopairs.SetSuccessor(pairs);
var high = new HighCardValidation();
pairs.SetSuccessor(high);
_cachedValidation = royalflush;
return _cachedValidation;
}
}
}

View File

@@ -0,0 +1,13 @@
using System.Linq;
namespace ChainOfResponssibility.Poker.Validators.HandValidators
{
public class HighCardValidation : HandValidation
{
protected override string IsValid(Card[] hand)
{
return string.Format("You only have high cards, here I ordered them for you: {0}",
string.Join(", ", hand.OrderByDescending(x => x.GetIntValue()).Select(x => x.ToString())));
}
}
}

View File

@@ -0,0 +1,17 @@
using System.Linq;
namespace ChainOfResponssibility.Poker.Validators.HandValidators
{
public class PairValidation : HandValidation
{
protected override string IsValid(Card[] hand)
{
var gr = hand.GroupBy(x => x.Value);
var pair = gr.FirstOrDefault(x => x.Count() == 2);
if (pair != null)
return "You have a pair of " + pair.First().Value + "s";
return string.Empty;
}
}
}

View File

@@ -0,0 +1,23 @@
using System.Linq;
namespace ChainOfResponssibility.Poker.Validators.HandValidators
{
public class RoyalFlushValidation : HandValidation
{
protected override string IsValid(Card[] hand)
{
var gr = hand.GroupBy(x => x.Suite);
var flush = gr.FirstOrDefault(x => x.Count() == 5);
if (flush != null)
{
var Value2Index = hand.Select((i, j) => i.GetIntValue() - j).Distinct();
if (!Value2Index.Skip(1).Any() && Value2Index.First() == 10)// consecutive and 10, meaning 10, J, Q, K, A
return "You have a Royal Flush with " + string.Join(", ", hand.Select(x => x.ToString()));
return string.Empty;
}
return string.Empty;
}
}
}

View File

@@ -0,0 +1,29 @@
using System.Linq;
namespace ChainOfResponssibility.Poker.Validators.HandValidators
{
public class StraightFlushValidation : HandValidation
{
protected override string IsValid(Card[] hand)
{
var gr = hand.GroupBy(x => x.Suite);
var flush = gr.FirstOrDefault(x => x.Count() == 5);
if (flush != null)
{
var Value2Index = hand.Select((i, j) => i.GetIntValue() - j).Distinct();
if (!Value2Index.Skip(1).Any())// consecutive
return "You have a Straight Flush with " + string.Join(", ", hand.Select(x => x.ToString()));
//special case for A,2,3,4,5
if (Value2Index.Count() == 2
&& Value2Index.First() == 2//for 2,3,4,5
&& Value2Index.Last() == 10)//for A
return "You have a Straight Flush with " + string.Join(", ", hand.Select(x => x.ToString()));
return string.Empty;
}
return string.Empty;
}
}
}

View File

@@ -0,0 +1,22 @@
using System.Linq;
namespace ChainOfResponssibility.Poker.Validators.HandValidators
{
public class StraightValidation : HandValidation
{
protected override string IsValid(Card[] hand)
{
var Value2Index = hand.Select((i, j) => i.GetIntValue() - j).Distinct();
if (!Value2Index.Skip(1).Any())// consecutive
return "You have a Straight with " + string.Join(", ", hand.Select(x => x.ToString()));
//special case for A,2,3,4,5
if (Value2Index.Count() == 2
&& Value2Index.First() == 2//for 2,3,4,5
&& Value2Index.Last() == 10)//for A
return "You have a Straight with " + string.Join(", ", hand.Select(x => x.ToString()));
return string.Empty;
}
}
}

View File

@@ -0,0 +1,17 @@
using System.Linq;
namespace ChainOfResponssibility.Poker.Validators.HandValidators
{
public class ThreeOfAKindValidation : HandValidation
{
protected override string IsValid(Card[] hand)
{
var gr = hand.GroupBy(x => x.Value);
var OhBabyATripple = gr.FirstOrDefault(x => x.Count() == 3);
if (OhBabyATripple != null)
return "You have three of " + OhBabyATripple.First().Value + "s (Three of a kind)";
return string.Empty;
}
}
}

View File

@@ -0,0 +1,17 @@
using System.Linq;
namespace ChainOfResponssibility.Poker.Validators.HandValidators
{
public class TwoPairsValidation : HandValidation
{
protected override string IsValid(Card[] hand)
{
var gr = hand.GroupBy(x => x.Value);
var pairs = gr.Where(x => x.Count() == 2);
if (pairs != null && pairs.Count() == 2)
return "You have pairs of " + string.Join(" and ", pairs.Select(x=>x.First().Value + "s"))+ " (Two Pairs)";
return string.Empty;
}
}
}