我想在c#中编写一个扑克手评估方法。我已经设法为每个使用LINQ的扑克手执行此操作,除了直线。 对于那些不玩直板的玩家来说,每张卡片由5张卡片组成,每张卡片增量为1。 Ace可以高或低。c#直接检查扑克
我已经创建了一个名为卡的对象,它具有套装,等级和值(J = 11,Q = 12等)。我的方法将被传递含7卡的这个对象的列表(孔卡和主板。)
另一件事要记住的是,如果玩家有5或10
只能做成直查看下面我的其他扑克手的方法,请让我知道,如果你有一个直接的方法的想法。伪代码也可以。
public bool CheckPair(List<Card> cards)
{
//see if exactly 2 cards card the same rank.
return cards.GroupBy(card => card.Rank).Count(group => group.Count() == 2) == 1;
}
public bool CheckTwoPair(List<Card> cards)
{
//see if there are 2 lots of exactly 2 cards card the same rank.
return cards.GroupBy(card => card.Rank).Count(group => group.Count() >= 2) == 2;
}
public bool CheckTrips(List<Card> cards)
{
//see if exactly 3 cards card the same rank.
return cards.GroupBy(card => card.Rank).Any(group => group.Count() == 3);
}
public bool CheckStraight(List<Card> cards)
{
// order by decending to see order
var cardsInOrder = cards.OrderByDescending(a => a.Value).ToList();
// check for ace as can be high and low
if (cardsInOrder.First().Rank == "A")
{
// check if straight with ace has has 2 values
bool highStraight = cards.Where(a => a.Rank == "K" || a.Rank == "Q" || a.Rank == "J" || a.Rank == "10").Count() == 4;
bool lowStraight = cards.Where(a => a.Rank == "2" || a.Rank == "3" || a.Rank == "4" || a.Rank == "5").Count() == 4;
// return true if straight with ace
if (lowStraight == true || highStraight == true)
{
return true;
}
}
else
{
// check for straight here
return true;
}
// no straight if reached here.
return false;
}
public bool CheckFlush(List<Card> cards)
{
//see if 5 or more cards card the same rank.
return cards.GroupBy(card => card.Suit).Count(group => group.Count() >= 5) == 1;
}
public bool CheckFullHouse(List<Card> cards)
{
// check if trips and pair is true
return CheckPair(cards) && CheckTrips(cards);
}
public bool CheckQuads(List<Card> cards)
{
//see if exactly 4 cards card the same rank.
return cards.GroupBy(card => card.Rank).Any(group => group.Count() == 4);
}
// need to check same 5 cards
public bool CheckStraightFlush(List<Card> cards)
{
// check if flush and straight are true.
return CheckFlush(cards) && CheckStraight(cards);
}
你要去这只是为了好玩,或为真正的使用情况?因为实际使用情况,现有的库经过精心设计,速度非常快。 – Evk
[检查数组数组是否连续的函数方法]的可能重复(http://stackoverflow.com/questions/18225010/functional-way-to-check-if-array-of-numbers-is-sequential) – fubo
嗨,这只是为了应对挑战 –