Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Need help with a card game in C.
First of all I have to create two data types. The first is "Card" using a struct to model a single card and using two seperate enum's for the suit and rank. The second data type is Card Deck which supports any number of packs of cards entered by the user(Must use dynamic memory allocation). Need CardDeck Operations such as shuffle, sorting, adding or removing cards, etc..

If i got this amount done, I think I would be able to do the actual game development myself. Any help is appreciated

c

What I have tried:

This is what I have so far..

C
#include <stdio.h>



// Data Type "Card"

struct Card
{
	char suit;
	char rank;
};


enum suit  { Club, Spade, Heart, Diamond};

enum rank { Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King, Ace};
Posted
Updated 19-Nov-20 5:58am
v3
Comments
F-ES Sitecore 14-Nov-16 9:14am    
We're not here to do your homework for you.
Member 12849609 14-Nov-16 9:16am    
This isn't my homework, I just want to know how to do it
F-ES Sitecore 14-Nov-16 9:35am    
"Must use dynamic memory allocation" - not homework, sure thing :)
[no name] 14-Nov-16 9:14am    
Help with what?
Member 12849609 14-Nov-16 9:22am    
I need to know if my first bit of code is right. And i need to know how to make sure my DataType CardDeck can support any number of card decks to be used in the game, which is entered by the user.
Also I need to know how make operations which are useful for Deck of Cards (e.g. shuffle, sorting, adding or removing a card) and adding a function that implements the operation.

Well, you defined the enumerations and a data structure. Now you need to add some real code (that is functionality). Try, for instance, to implement card sorting in the deck. We would be glad to help on that, once you started and possible get stuck.
 
Share this answer
 
In your struct you define suit and rank as char types. Then below that you define two enum types suit and rank which just serves to confuse. Define your enums first, and then use those types (but with different names) as the variables in your structure. You can then set both types inside each card that you create.

So from that you can create CardDeck and Hand, both of which would contain arrays of Cards (52 for the CardDeck, and whatever number for the Hand). Shuffling the deck just needs a randomiser to select one of the 52 and create a new array of shuffled cards ready to deal.

The other functions should not be too difficult to figure out.
 
Share this answer
 
Comments
Member 12849609 14-Nov-16 9:54am    
Do you mean something like this?:

enum suit
{
Club, Spade, Heart, Diamond
};

typedef enum suit Suit;

enum rank
{
Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King, Ace
};
typedef enum rank Rank;

struct Card
{
Suit c_suit;
Rank c_rank;
};
Richard MacCutchan 14-Nov-16 10:30am    
Yes. Give it a try and see what happens when you compile it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900