如何查找同一对象在列表中出现了多少次,然后找到出现次数最多的对象的 属性
How to find how many times the same object appears in a list and then find a property of the most appeared object
我正在为我已经提交给 codereview stack exchange 的一些编程练习制作一个扑克手牌评估器。我需要能够正确地比较手牌,为此我需要看到对子的价值。我当前的过牌手牌 class
private static PokerHandsRank CheckHandForPairs(Hand hand)
{
var faceCount = (from card in hand.Cards
group card by card.Face
into g
let count = g.Count()
orderby count descending
select count).Take(2).ToList(); // take two to check if multiple pairs of pairs, if second in list is 1 there will be two pairs
switch (faceCount[0])
{
case 1: return PokerHandsRank.HighCard;
case 2: return faceCount[1] == 1 ? PokerHandsRank.Pair : PokerHandsRank.TwoPair;
case 3: return faceCount[1] == 1 ? PokerHandsRank.ThreeOfKind : PokerHandsRank.FullHouse;
case 4: return PokerHandsRank.FourOfKind;
default: throw new Exception("something went wrong here");
}
}
如您所见,我已经使用 linq 获取出现次数最多的配对列表,但是我不确定如何完成它以在将它们分开后获得卡片的面值。
这是我目前的比较方法
public int CompareTo(Hand other)
{
if (HandRank == other.HandRank) //if the hand rank is equal, sort the cards by face value and compare the two biggest
{
sortHandbyFace(this); // sorts cards into order by face
sortHandbyFace(other);
for (int i = 4; 0 <= i; i--)
{
if (Cards[i].Face == other.Cards[i].Face)
{
if (i == 0) return 0;
continue;
}
return Cards[i].Face > other.Cards[i].Face ? 1 : -1;
}
}
return HandRank > other.HandRank ? 1 : -1;
比较非常适合比较高牌问题,但是我需要添加检查两手牌的等级是否相等,然后检查它们的值是一对、两对、满屋还是三(然后到找出该对的最高面值)
如果您需要有关我的程序的更多信息,请随时查看我的代码审查 post https://codereview.stackexchange.com/questions/152857/beginnings-of-a-poker-hand-classifier-part-2?noredirect=1&lq=1
这可能不是您要查找的内容,因为您比较的是 object
而不仅仅是 int
,但这可以帮助您入门。基于这里的这个问题:How to get frequency of elements stored in a list in C#.
using System.Linq;
List<int> ids = //
int maxFrequency = 0;
int IDOfMax = 0;
foreach(var grp in ids.GroupBy(i => i))
{
if (grp.Count() > maxFrequency)
{
maxFrequency = grp.Count();
IDOfMax = grp.Key;
}
}
// The object (int in this case) that appears most frequently
// can be identified with grp.key
更新:重读问题后,听起来你需要尝试 return 一个新对象,其中包含查询中的计数和面值。
您可以这样做:
public class FaceCountResult
{
public int Count { get; set; }
public Face FaceValue { get; set; }
public FaceCountResult(int count, Face faceValue)
{
Count = count;
FaceValue = faceValue;
}
}
那么,faceCount
应该看起来像这样:
var faceCount = (from card in hand.Cards
group card by card.Face
into g
let count = g.Count()
orderby count descending
select new FaceCountResult(count, card.Face);
我不确定 Take(2)
部分会如何影响这一点,因为我不太理解那部分代码。
然后你可以在faceCount[0].Count
上做一个switch
然后用faceCount[0].FaceValue
得到面值。
我正在为我已经提交给 codereview stack exchange 的一些编程练习制作一个扑克手牌评估器。我需要能够正确地比较手牌,为此我需要看到对子的价值。我当前的过牌手牌 class
private static PokerHandsRank CheckHandForPairs(Hand hand)
{
var faceCount = (from card in hand.Cards
group card by card.Face
into g
let count = g.Count()
orderby count descending
select count).Take(2).ToList(); // take two to check if multiple pairs of pairs, if second in list is 1 there will be two pairs
switch (faceCount[0])
{
case 1: return PokerHandsRank.HighCard;
case 2: return faceCount[1] == 1 ? PokerHandsRank.Pair : PokerHandsRank.TwoPair;
case 3: return faceCount[1] == 1 ? PokerHandsRank.ThreeOfKind : PokerHandsRank.FullHouse;
case 4: return PokerHandsRank.FourOfKind;
default: throw new Exception("something went wrong here");
}
}
如您所见,我已经使用 linq 获取出现次数最多的配对列表,但是我不确定如何完成它以在将它们分开后获得卡片的面值。
这是我目前的比较方法
public int CompareTo(Hand other)
{
if (HandRank == other.HandRank) //if the hand rank is equal, sort the cards by face value and compare the two biggest
{
sortHandbyFace(this); // sorts cards into order by face
sortHandbyFace(other);
for (int i = 4; 0 <= i; i--)
{
if (Cards[i].Face == other.Cards[i].Face)
{
if (i == 0) return 0;
continue;
}
return Cards[i].Face > other.Cards[i].Face ? 1 : -1;
}
}
return HandRank > other.HandRank ? 1 : -1;
比较非常适合比较高牌问题,但是我需要添加检查两手牌的等级是否相等,然后检查它们的值是一对、两对、满屋还是三(然后到找出该对的最高面值)
如果您需要有关我的程序的更多信息,请随时查看我的代码审查 post https://codereview.stackexchange.com/questions/152857/beginnings-of-a-poker-hand-classifier-part-2?noredirect=1&lq=1
这可能不是您要查找的内容,因为您比较的是 object
而不仅仅是 int
,但这可以帮助您入门。基于这里的这个问题:How to get frequency of elements stored in a list in C#.
using System.Linq;
List<int> ids = //
int maxFrequency = 0;
int IDOfMax = 0;
foreach(var grp in ids.GroupBy(i => i))
{
if (grp.Count() > maxFrequency)
{
maxFrequency = grp.Count();
IDOfMax = grp.Key;
}
}
// The object (int in this case) that appears most frequently
// can be identified with grp.key
更新:重读问题后,听起来你需要尝试 return 一个新对象,其中包含查询中的计数和面值。
您可以这样做:
public class FaceCountResult
{
public int Count { get; set; }
public Face FaceValue { get; set; }
public FaceCountResult(int count, Face faceValue)
{
Count = count;
FaceValue = faceValue;
}
}
那么,faceCount
应该看起来像这样:
var faceCount = (from card in hand.Cards
group card by card.Face
into g
let count = g.Count()
orderby count descending
select new FaceCountResult(count, card.Face);
我不确定 Take(2)
部分会如何影响这一点,因为我不太理解那部分代码。
然后你可以在faceCount[0].Count
上做一个switch
然后用faceCount[0].FaceValue
得到面值。