1

I'm writing a poker game, but the rules are custom. I have 2 player cards and 5 community cards (arrays of structs). I need to initialize a player's possible hands (21 hands - 7 combination 5) with any combination of 5 cards (structs).

Passing in two pointers to the player and community card struct arrays, as well as a pointer to the player struct, I go through them and put all 21 in a "hands" struct array, each element containing a 5 "card" struct array in the "player" struct.

enum Value {
    ONE = 1, TWO, THREE, FOUR...ACE
};

enum Suit {
    CLUB = 0, DIAMOND...
};

struct card {
    enum Value val;
    enum Suit suit;
};

struct hand {
     struct card cards[5];
}

struct player {
    struct hand hands[21];
}

void makePlayer(struct player *person, struct card *personCards, struct card *commCards);

we can make this easier and just conceptualize with an array of 7 cards

void makePlayer(struct player *person, struct card *sevenCards);

My question is how to implement the combination to achieve all 21 results without hard coding them? I have a function to copy cards, just passes in two card arrays and gives them the same values. I'm just lost conceptually on the combination.

A step in the right direction would help. I'm looking more for concept proof than an actual working program. Say you have cards "p1, p2, c1, c2, c3, c4, c5". You make 21 combinations of 5 cards, order doesn't matter, you can use any cards. So: p1 p2 c1 c2 c3. Or: c1, c3, c4, p1, c5. Just no repeating combinations. So basically, given an array of 7 card structures, create 21 possible combinations of 5 of the structures without repeating any.

Avallauch
  • 65
  • 2
  • 11
  • 1
    Please explain more about your rules and what you want to achieve. Most importantly, give sample input and desired output. Make the environemt code around the missing part available here. Show the declaration of all involved datatypes. Show the values of constant arrays (e.g. card names). Create as much of a [mcve] as possible. The missing part can be an empty function. – Yunnosch Sep 29 '17 at 04:37
  • This is a big program. I don't want to post my entire header file. I don't have any expected output from this function, I just input a line of letters into another function and create cards from that. Then, I take those cards and make hands in this function. I'm mainly looking for a way to create combinations with structures, the program itself shouldn't be that important. I added more detail at the bottom. – Avallauch Sep 29 '17 at 05:04
  • Please study the concept of a [mcve], I did not ask for all your code. And if you do not want to provide the needed declarations from your header, then you will understand that probably nobody here will want to invent them first, before being able to help you. – Yunnosch Sep 29 '17 at 05:06
  • Ok, I understand. I added some visualization for you. – Avallauch Sep 29 '17 at 05:12
  • Seriously "..."? Please spend the effort of doing more than a "visualisation". – Yunnosch Sep 29 '17 at 05:15
  • you could semicolon the enum at any point... I just want to know how to make combinations of structs...Any more code would be pointless... this will compile with two added semicolons :( – Avallauch Sep 29 '17 at 05:20
  • Good luck with your question. – Yunnosch Sep 29 '17 at 05:21
  • This question could be answered with any struct. I was trying to eliminate things irrelevant to the question. I described the issue, gave clear example, and my example gives everything needed for the problem I have. – Avallauch Sep 29 '17 at 05:29
  • Here are answers in several languages, including C, for what I believe is your core problem. https://rosettacode.org/wiki/Combinations – Yunnosch Sep 29 '17 at 05:31
  • Yes, that answer helps a lot. Rosettacode I was at earlier and it didn't help much. Sorry if I don't ask questions right I'm new to this site. – Avallauch Sep 29 '17 at 05:41
  • For the special case of 5 in 7, you could probably get a simpler solution with only two fine tuned loops, which place the two **non**used cards in all combinations. I.e. outer loop i=7...2, inner loop j=i-1...1. By the implicit sorting you will always get only one of two possibilities, the one where the first gap has the higher index. (7,6...1),(6,5...1),(5,4...1),(4,3...1),(3,2...1), (2,1): 6+5+4+3+2+1==21. (By the way, because of the restriction to 5 in 7, instead of generic case, I do not believe this anymore to be a duplicate. If it gets reopened, I will make this an answer.) – Yunnosch Sep 29 '17 at 18:08

0 Answers0