-1

I am creating a game which composed of 6 cards, I want to make a combination of those cards but the problem is that I am stuck in creating that function. Below are the codes I made

public class Cards
{
   
    public int number{ get; set; }
    public string symbol{ get; set; }

    public Cards(int number, string symbol)
    {
       
        number= Number;
        symbol = Symbol;
    }
}

//populate list
List<Cards> CardList = new List<Cards>();
void InitCards()
{
            CardList.Add(new Cards(1, "dogman"));
            CardList.Add(new Cards(2, "dogman"));
            CardList.Add(new Cards(1, "catman"));
            CardList.Add(new Cards(2, "catman"));
            CardList.Add(new Cards(1, "birdman"));
            CardList.Add(new Cards(2, "birdman"));
}

void MakeCombination()
{
    foreach (var card in CardList)
        {
           //make combination
        }
}

My expected output is to get all the possible combination with a set of two cards sample expected output below

1 dogman, 2 dogman
1 dogman, 1 catman
1 catman, 2 birdman
Liam
  • 27,717
  • 28
  • 128
  • 190
  • 2
    What is the expected result? What do you mean by combination? Maybe related : https://stackoverflow.com/questions/30081908/ – Drag and Drop Sep 03 '20 at 11:20
  • 1
    Why are there only 3 combinations in your output? Why no 1dogman,2catman or no dogman/birdman at all for example? – Caius Jard Sep 03 '20 at 11:23
  • 2
    Not really related to you question, but it's better to call your card class `Card`. And the properties should be Pascal cased (starting with an uppercase character) – Hanno Sep 03 '20 at 11:24
  • Agree with Hanno; in C# make class names singular. If a class represents a collection, it makes more sense to append the word "Collection" than to pluralize the name. Reason being an array/list of Card should really be called Cards (as a property) -> `List Cards`. If you called your class Cards, you ought to write `List Cardss` or `List Cardses`. `List` gets a plural name because it's a property of some other class. When a class's sole purpose is to be a collection of something else, call it `-Collection`. A subtle difference to be sure – Caius Jard Sep 03 '20 at 11:27
  • It basically the same problem [as here](https://stackoverflow.com/questions/7802822/all-possible-combinations-of-a-list-of-values). your just using objects not integers. I'd just use the class [hashcode as a single value for your object](https://learn.microsoft.com/en-us/dotnet/api/system.object.gethashcode?view=netcore-3.1) – Liam Sep 03 '20 at 11:29
  • Is "1 dogman, 2 dogman" the same as "2 dogman, 1 dogman"? If so, see [Words combinations without repetition](https://stackoverflow.com/questions/5132758/words-combinations-without-repetition). – Andrew Morton Sep 03 '20 at 11:32
  • So you don't want combinaison you simple what to [shuffle](https://stackoverflow.com/questions/273313/randomize-a-listt) and deals pair? For dealings paire you can either split in sub list like [Split a List into smaller lists of N size](https://stackoverflow.com/questions/11463734/split-a-list-into-smaller-lists-of-n-size) or [Split List into Sublists with LINQ](https://stackoverflow.com/questions/419019/split-list-into-sublists-with-linq?noredirect=1&lq=1) or write a simple loop that increment by 2. – Drag and Drop Sep 03 '20 at 11:34
  • Thank you for the information, I will apply those so that I can improve my code – stud_Jaguar Sep 03 '20 at 11:41
  • If it's about the math of All combinaison of K element from an array of size N I will recommend https://stackoverflow.com/questions/127704/algorithm-to-return-all-combinations-of-k-elements-from-n – Drag and Drop Sep 03 '20 at 11:42
  • Does this answer your question? [Words combinations without repetition](https://stackoverflow.com/questions/5132758/words-combinations-without-repetition) – Andrew Morton Sep 06 '20 at 18:15

1 Answers1

0

Based on your requirements which is to get all possible combinations of pairs of cards, the following double nested loop should do the magic for you:

void MakeCombination()
{
    for (int i = 0; i < CardList.Count - 1; i++)
        for (int g = i + 1; g < CardList.Count; g++)
            Console.WriteLine($"{CardList[i].number} {CardList[i].symbol}, {CardList[g].number} {CardList[g].symbol}");
}
  • 1
    Thank you, It got me an hour on how to solve it but I can't believe the solution only contains 3 lines of code. – stud_Jaguar Sep 03 '20 at 11:36
  • Your welcome! Just keep in mind, next time, please write down your expected output so that we can help you much easier without all these questions. – Efthymios Kalyviotis Sep 03 '20 at 11:42
  • Thanks again sir, I will apply all that. Sir is it okay if I have another question posted here related to my question? Or do I need to post another new question? – stud_Jaguar Sep 03 '20 at 11:45
  • Drop it here in the comments and if it can be answered easy I will answer you, else I will redirect you to ask a new one. :) – Efthymios Kalyviotis Sep 03 '20 at 11:46
  • The restriction on the problem make the code short. here the size of the sub set is fixed. For a 3 elements you will need an other loop like [this](https://stackoverflow.com/a/127898/6560478). But the major simplification cames from {A,B} == {B,A}. – Drag and Drop Sep 03 '20 at 11:48
  • How about adding 3rd card if the total of 2 cards is less than 4? For example the card combination is 1 dogman, 1 catman, then the code should add another card – stud_Jaguar Sep 03 '20 at 11:49
  • You will have to either write a new question or alter this one provided you give all your expected output (and the reasoning). Although, I would suggest that you try it yourself without asking. You will not learn programming by getting answers to solutions by others. Programming is a way of thinking and I am confident that you can do it. – Efthymios Kalyviotis Sep 03 '20 at 11:55
  • Thank you sir! I will do it :) – stud_Jaguar Sep 03 '20 at 11:57