1

I call this function in order to get a List<List<Card>>:

List<List<Card>> comboCards = GetComboCards(new List<Card>(), board, new List<List<Card>>());

private List<List<Card>> GetComboCards(List<Card> previous, List<Card> board, List<List<Card>> combos)
{
    for (int i = 0; i < board.Count; i++)
    {
        Card card = board[i];
        List<Card> newPrevious = previous.Skip(i).ToList(); //  FIXME: Not right
        newPrevious.Add(card);
        combos.Add(newPrevious);
        List<Card> newBoard = board.Skip(i + 1).ToList();
        GetComboCards(newPrevious, newBoard, combos);
    }

    return combos;
}

There's a problem with previous.Skip(i).ToList() which I'm unsure of how to correct.


Example:

Board contains 4 cards. When run through GetComboCards, the List will return the appropriate number of List<Card>'s (15). However the List will have duplicates of the same combination in [5] and [12] for the 3rd card and again in [7], [11], and [14] for the 4th card.


Can anyone tell me what I'm doing wrong here? I know it has to do with the line where I've added FIXME, but I can't seem to figure it out. Any help would be greatly appreciated, thank you!

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
henz90
  • 31
  • 1
  • 1
    `Skip(n)` will skip n values. If you want all the values except the one at index n you'd do `list.Take(n).Concat(list.Skip(n+1))` – juharr Nov 07 '21 at 15:52
  • When you need to eliminate duplicates, you should take a look at: [LINQ's Distinct() on a particular property](https://stackoverflow.com/questions/489258/linqs-distinct-on-a-particular-property) – Luuk Nov 07 '21 at 16:27

0 Answers0