0

Tell me if there is a way to sort the list by the rules.

I have several playing decks and each has its own rules:

  1. Count - how many cards to take from the deck
  2. From / Before - in which interval the cards should be (If 0, then in any)
  3. Row - how many cards can fall out in a row

My current code:

private void ShuffleDecks()
    {
        List<TestCard> testCards = new();
        
        TestDesk desk1 = new TestDesk
        {
            From = 0,
            Before = 0,
            Row = 2,
            Count = 3
        };
        desk1.Cards = new List<TestCard>
        {
            new("a1", desk1),
            new("a2", desk1),
            new("a3", desk1),
        };

        TestDesk desk2 = new TestDesk
        {
            From = 1,
            Before = 4,
            Row = 1,
            Count = 2
        };
        desk2.Cards = new List<TestCard>
        {
            new("b1", desk2),
            new("b2", desk2),
            new("b3", desk2),
        };

        TestDesk desk3 = new TestDesk
        {
            From = 3,
            Before = 5,
            Row = 0,
            Count = 2
        };
        desk3.Cards = new List<TestCard>
        {
            new("c1", desk3),
            new("c2", desk3),
            new("c3", desk3),
        };

        List<TestDesk> desks = new List<TestDesk> { desk1, desk2, desk3 };

        foreach (TestDesk desk in desks)
        {
            desk.Cards.Shuffle();
            testCards.AddRange(desk.Cards.Take(desk.Count).ToList());
        }
        
        testCards.Sort();
        
        Debug.LogWarning($"{string.Join(", ", testCards.Select(x => x.Name))}");
    }

TestDesk and TestCard:

public class TestDesk
{
    public List<TestCard> Cards;
    public int From;
    public int Before;
    public int Row;
    public int Count;
}

public class TestCard
{
    public string Name;
    public TestDesk Desk;

    public TestCard(string name, TestDesk desk)
    {
        Name = name;
        Desk = desk;
    }
}
xew
  • 37
  • 6
  • just make your `TestCard`-class implement `IComparable`. – MakePeaceGreatAgain Sep 14 '22 at 08:03
  • Normally you sort by comparing items, so as long as you can figure out how any two items should be ordered you can implement a `IComparer` and do the sorting . Note that comparison sorts must follow some [rules](https://devblogs.microsoft.com/oldnewthing/20031023-00/?p=42063), otherwise the sorting will not work. – JonasH Sep 14 '22 at 08:04
  • However, Your rules does not seem to have much to do with sorting as far as I can see. So you should probably show some examples of how you would expect some example decks to be sorted. – JonasH Sep 14 '22 at 08:08
  • It's not really clear what the phrases you're using mean but it looks like there could easily be contradictions between cards such that it's not possible to create a sort that satisfies all of them. Could you elaborate further on what the rules *mean* (You may think "how many cards can fall out in a row" is clear but I've no idea) and what should happen if there are contradictions? – Damien_The_Unbeliever Sep 14 '22 at 08:08
  • @JonasH, For example from the current code: We can take only 3 cards from deck 1, but not more than 2 times in a row at any time. We can take two cards from the 2nd deck if they are in the interval between the 1st and 4th, but not more than 1 time in a row. We can take two cards from the 3rd deck if it is in the interval between the 3rd and 5th, even if everything is in a row. – xew Sep 14 '22 at 08:23
  • @Damien_The_Unbeliever So that there are no contradictions, there will always be one deck from which you can take any card at any time (Without restrictions by the rules) – xew Sep 14 '22 at 08:25
  • You seem to be describing *game* rules, not *sorting* rules. Sorting rules are only concerned about if `a` and `b` are 1) equal, 2) `a` before `b` 3) `a` after `b`. So, is the question about sorting, or about some game design issue? – JonasH Sep 14 '22 at 08:32
  • @JonasH Yes, I want to achieve incomplete randomness in my game. At the moment, my cards are collected from different decks of several pieces in random order. But this is not quite right, because I have buff cards. I would not want them to fall out at the beginning or at the end of the game, as well as several times in a row. So I thought sorting would be able to help me with this. – xew Sep 14 '22 at 08:52
  • 1
    It sounds like you are really asking about some constrained form of [*shuffeling*](https://stackoverflow.com/questions/273313/randomize-a-listt). A typical approach is to shuffle all normal cards, split the deck into multiple parts, insert your special cards in each part at a random location, and merge the lists. – JonasH Sep 14 '22 at 08:55
  • @JonasH Thanks, I'll think about my implementation ) – xew Sep 14 '22 at 09:00

0 Answers0