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,90 @@
using System;
using ChainOfResponssibility.Validators;
using ChainOfResponssibility.Poker.Validators;
namespace ChainOfResponssibility.Poker
{
public class Card : IEquatable<Card>
{
public const string ValidValues = "2,3,4,5,6,7,8,9,10,J,Q,K,A";
public const string ValidSuites = "C,H,S,D";
public string Value { get; private set; }
public char Suite { get; private set; }
public static ChainValidation<Card> CardValidatorFactory { get; private set; }
public Card(string value, char suite)
{
Value = value.ToUpper();
Suite = suite.ToString().ToUpper()[0];
}
public static Card Parse(string toParse)
{
Card c = null;
switch (toParse.Length)
{
case 2:
c = new Card(toParse[0].ToString(), toParse[1]);
break;
case 3:
c = new Card(toParse.Substring(0, 2), toParse[2]);
break;
default:
throw new ArgumentException(string.Format("Must be in format VS where V is one of [{0}] and S is one of [{1}]", ValidValues, ValidSuites), nameof(toParse));
}
ChainValidation<Card> Validation = CardValidationFactory.GetValidator();
var validationResult = Validation.Validate(c);
if (!validationResult.IsValid)
throw validationResult.Exception;
return c;
}
public int GetIntValue()
{
int val = 0;
if (int.TryParse(Value, out val))
return val;
else
switch (Value)
{
case "J":
return 11;
case "Q":
return 12;
case "K":
return 13;
case "A":
return 14;
}
return 0;
}
public bool Equals(Card other)
{
if (this.Value == other.Value && this.Suite == other.Suite)
return true;
return false;
}
public static bool operator ==(Card c1, Card c2)
{
return c1.Equals(c2);
}
public static bool operator !=(Card c1, Card c2)
{
return !c1.Equals(c2);
}
public override string ToString()
{
return Value + Suite;
}
}
}

View File

@@ -0,0 +1,38 @@
using ChainOfResponssibility.Poker.Validators.HandValidators;
using System;
using System.Linq;
namespace ChainOfResponssibility.Poker
{
public class PokerGame
{
public void newGame()
{
Console.WriteLine("!!NOT AN ACTUAL POKER GAME!!{0}{1}{0}{2}{0}{3}{0}{4}{0}{5}", Environment.NewLine,
"This tells you what hand you have",
"Just enter the 5 cards you have, separated with spaces, and press enter",
"Card values: 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K, A",
"Card Suites: H, D, C, S",
"ex.:\"QH AH JH KH 10H\" (yes that is a royal flush right there)");
string cards = string.Empty;
while (cards.ToLower()!="exit")
{
Console.Write(">");
cards = Console.ReadLine();
try
{
var hand = cards.Split(' ').Select(x => Card.Parse(x)).OrderBy(x=>x.GetIntValue()).ToArray();
Console.WriteLine(HandValidationFactory.GetValidation().Validate(hand));
}
catch (Exception e)
{
Console.WriteLine(e);
continue;
}
}
Console.Clear();
}
}
}

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;
}
}
}

View File

@@ -1,4 +1,5 @@
using ChainOfResponssibility.PurchaseExample;
using ChainOfResponssibility.Poker;
using ChainOfResponssibility.PurchaseExample;
using ChainOfResponssibility.TransferFileExample;
using ChainOfResponssibility.Validators.UserEntities;
using System;
@@ -12,40 +13,63 @@ namespace ChainOfResponssibility
Console.WriteLine(GetPatternDescription());
GoToNextStep();
Console.WriteLine(ExeucteFirstWhenConditionMatchesFlavorDescription());
GoToNextStep();
while (true)
{
Console.Clear();
Console.WriteLine(ExeucteFirstWhenConditionMatchesFlavorDescription());
Console.WriteLine(ExecuteAllUntilConditionIsFalseFlavorDescription());
Console.WriteLine(ExecuteAllFlavorDescritpion());
Console.WriteLine("1: Money Spender (flavor 1)");
Console.WriteLine("2: File Transfer (flavor 1)");
Console.WriteLine("3: User Processor (flavor 1 and 3)");
Console.WriteLine("4: pitfals");
Console.WriteLine("5: Poker Game (flavor 1)");
Console.Write("0: exit\r\n>");
CheckAuthority moneySpender = new CheckAuthority();
var keyChar = GoToNextStep();
Console.WriteLine(moneySpender.GetDescriptionOfExample());
GoToNextStep();
switch (keyChar)
{
case '1':
CheckAuthority moneySpender = new CheckAuthority();
moneySpender.PrintHowMuchEachCanSpend();
moneySpender.SpendMoney();
GoToNextStep();
Console.WriteLine(moneySpender.GetDescriptionOfExample());
GoToNextStep();
TransferFilesManager transferFilesManager = new TransferFilesManager();
moneySpender.PrintHowMuchEachCanSpend();
moneySpender.SpendMoney();
break;
case '2':
TransferFilesManager transferFilesManager = new TransferFilesManager();
Console.WriteLine(transferFilesManager.GetDescriptionOfExample());
GoToNextStep();
transferFilesManager.TransferFiles();
Console.WriteLine(transferFilesManager.GetDescriptionOfExample());
GoToNextStep();
transferFilesManager.TransferFiles();
break;
case '3':
UserProcessor userProcessor = new UserProcessor();
userProcessor.DoStuff();
break;
case '4':
Console.WriteLine(GetPitfalls());
GoToNextStep();
break;
case '5':
PokerGame poke = new PokerGame();
poke.newGame();
break;
}
GoToNextStep();
Console.WriteLine(ExecuteAllUntilConditionIsFalseFlavorDescription());
Console.WriteLine(ExecuteAllFlavorDescritpion());
GoToNextStep();
UserProcessor userProcessor = new UserProcessor();
userProcessor.DoStuff();
GoToNextStep();
Console.WriteLine(GetPitfalls());
if (keyChar == '0')
break;
}
}
private static void GoToNextStep()
private static char GoToNextStep()
{
Console.ReadKey();
var ch = Console.ReadKey().KeyChar;
Console.Clear();
return ch;
}
public static string GetPatternDescription()
@@ -55,9 +79,9 @@ Decouples sender and receiver (as a sender you don't know who will handle the re
Hierarchical in nature
When using the Chain of Responsibility is more effective:
More than one object can handle a command
The handler is not known in advances
The handler is not known in advance
The handler should be determined automatically
It's wished that the request is addressed to a group of objects without explicitly specifying its receiver
Its wished that the request is addressed to a group of objects without explicitly specifying its receiver
The group of objects that may handle the command must be specified in a dynamic way.
Examples in real life:
-java.util.logging.Logger.#log()
@@ -80,12 +104,12 @@ Chain length/performance issues - in theory you could see a chain that is too bi
public static string ExecuteAllUntilConditionIsFalseFlavorDescription()
{
return @"Flavor 2 of chain of responssibility:Execute all elements of chain until the condition does not match";
return @"Flavor 2: Execute all elements of chain until the condition does not match";
}
public static string ExecuteAllFlavorDescritpion()
{
return @"Flavor 3 of chain of responssibility: Execute all elements of chain";
return @"Flavor 3: Execute all elements of chain";
}
}
}

View File

@@ -9,15 +9,15 @@ namespace ChainOfResponssibility.Validators.UserEntities
{
UserRepository userRepository;
PrincipalHelper principalHelper;
Operation inputOperation;
Operation operation;
ChainValidation<User> userCreationValidation;
ChainValidation<User> authenticateUserValidation;
public UserProcessor()
{
userRepository = new UserRepository();
principalHelper = new PrincipalHelper();
inputOperation = new AuthenticateOperation(AuthenticateUser);
inputOperation.SetSuccessor(new CreateNewUserOperation(CreateNewUser));
operation = new AuthenticateOperation(AuthenticateUser);
operation.SetSuccessor(new CreateNewUserOperation(CreateNewUser));
userCreationValidation = new IsAuthorisedToDoOperationsOnUser(principalHelper, Rights.Create);
userCreationValidation.SetSuccessor(new ValidateNoDuplicateEmail(userRepository));
@@ -55,12 +55,12 @@ namespace ChainOfResponssibility.Validators.UserEntities
string userInput;
do
{
inputOperation.PrintMenu();
operation.PrintMenu();
Console.Write(">");
userInput = Console.ReadLine();
if (!IsExitCode(userInput))
inputOperation.Execute(userInput);
operation.Execute(userInput);
} while (!IsExitCode(userInput));
}

View File

@@ -1,8 +1,8 @@
{
{
"version": "1.0.0-*",
"dependencies": {
"NETStandard.Library": "1.6.0"
"NETStandard.Library": "1.5.0-rc2-24027"
},
"frameworks": {