This is what I did so far:
class CardDisplayer
{
public int CardSuit;
public int CardValue;
}
List<CardDisplayer> _playerHand;
// Group all cards by the same suit
var _handDuplicates = _playerHand.GroupBy(x => x.CardSuit)
.Select(g => g.ToList())
.ToList();
CardDisplayer _duplicateFound = null;
// And then find all cards with the same value number
for (int i = 0; i < _handDuplicates.Count; i++)
{
var _handReference = _handDuplicates[i];
var _temp = _handReference.GroupBy(x => x.CardValue)
.Where(g => g.Count() > 1)
.Select(g => g.ToList())
.ToList();
// If you find more than one card with the same number
if(_temp.Count > 0)
{
// Take it
_duplicateFound = _temp.First().First();
break;
}
}
What I'm trying to achieve is after get the player's hand I want to find if the player has duplicates in his hand by looking if there is cards with the same suit and the same value.
I tried a lot of things on the internet but I cannot figure out how to get the list of duplicates using LINQ instead write all these lines of code.
Can someone know how to do it please? Thank you.