Reputation:
I'm developing a Poker Texas Hold'Em app I have all the combinations working but i cant seem to find a compact way to check which player has the highest combination and the type of it here's some of my code
void Rules(int current,int Power)
{
//checking if the player has any pair
{
Power = 1;
current = 1;
}
}
and so on.. I'm updating current and Power with each new combination and since they are parameters of the void each player has his own "current" and "Power" so they don't get messed up.This is what i have so far and my question is how do i check which player has the highest hand without using 20-30 repetitive if statements i was thinking about :
List <int> Arr = new List <int>();
void Rules(int current,int Power)
{
//checking if the player has any pair
{
Power = 1;
current = 1;
Arr.Add(Power);
Arr.Add(current);
}
}
but like this i have no idea which type belongs to which player so it's not use i also tried with strings but than i wont be able to compare them that easily.What should i do ? What's the right approach ?
Upvotes: 0
Views: 1032
Reputation: 5907
You might want to make a class for reach rule, to encapsulate the logic. Something like this:
public class Card
{
public string Name { get; private set; }
/* cards have a value of 2-14 */
public int Value { get; private set; }
}
public abstract class Rule()
{
public abstract string Name { get; }
/* hands are calculated in powers of 100, when the card value is added you will get something like 335 */
public abstract int Value { get; }
public abstract bool HasHand(IReadonlyList<Card> cards);
}
public class PairRule() : Rule
{
public override string Name
{
get { return "Pair"; }
}
public override int Value
{
get { return 100; }
}
public override bool HasHand(IReadonlyList<Card> cards)
{
/* implement rule here */
return Enumerable.Any(
from x in cards
group x by x.Value into g
where g.Count() == 2
select g
);
}
}
...
public class Player
{
public IReadonlyList<Card> Hand { get; private set; }
public int GetHandValue(IReadonlyList<Rule> rules)
{
/* get value of hand 100, 200, 300 etc. */
var handValue = Enumerable.Max(
from x in rules
where x.HasHand(Hand)
select x.Value
);
/* get value of cards */
var cardValue = Hand
.OrderByDescending(x => x.Value)
.Take(5)
.Sum();
return handValue + cardValue;
}
}
public class Pot
{
public int Value { get; private set; }
public IReadonlyList<Player> Players { get; private set; }
public IReadonlyList<Player> GetWinners(IReadonlyList<Rule> rules)
{
var playerHands = Enumerable.ToList(
from x in players
select new {
Player = x,
HandValue = x.GetHandValue(rules)
}
);
var maxHand = playerHands.Max(x => x.HandValue);
return Enumerable.ToList(
from x in playerHands
where x.HandValue == maxHand
select x.Player
);
}
}
Upvotes: 2