Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

onqun

macrumors regular
Original poster
Oct 13, 2006
100
1
Hello, I am trying to make 52 card deck with couple of players, in objective c. I am a freshman. after the interface section I confused, and could not move on.

#import <Foundation/Foundation.h>

@interface cards : NSObject

@property int* spade;

@end


@interface spade : cards

@property int spadeCards;

@end


@interface heart : cards

@property int heartCards;

@end


@interface diamond : cards

@property int diamondCards;

@end


@interface club : cards

@property int clubCards;

@end



@interface players : NSObject

-(void) player;
-(void) print;

@end

@interface dealer : NSObject

-(void) deal: (int) d;

@end

** i wanted to create 4 objects under the object of cards . My aim for the implementation part is to create 13 cards for each by giving them numbers and for the face cards i want to point them to string for print function.

My problem is, as i said earlier, i am a freshman; i do not know how to put numbers in an array and use them as objects. :/

@implementation cards

@synthesize spade;

@end


thank you for help !
 
This creates a spade card and sets it's int value to 1, which I assume should be the Ace:
Code:
spade * aceOfSpaces = [[spade alloc] init];
[aceOfSpades setSpadeCards:1];

Note that, since you define spadeCards as a property, the setter method setSpadeCards is automatically created by the compiler.

You should learn some basic C and Objective C.
 
A sample answer

So basically , you already create 4 card types.

At your main file or view controller class, what you need is just import all card types. just something like this. This is just a simple way of doing it. I am sure there can be much better way of doing once you learn more bout programming :)
Code:
-(void)add52cards{
      mutableArray = [[NSMutableArray alloc]initWithCapacity:52];
        for (int i = 0; i< 13< i++) {
      Spade *spade= [Spade alloc]init];
      spade.spadeCards = i;
     [mutableArray addObject: spade];
      }

//repeat for other types, hearts,diamonds..

}
And i suggest you have a look on objective c for dummies, it is very good book for learning the basics.
 
Last edited by a moderator:
Thank you for replies. I used have knowledge about basic c, unfortunately I have forgotten. I have two books one of them is programming in Objective-C by developers library, 4e and objective c programming: the big nerd ranch guide by Aaron Hillegass
 
Have you though about a deck object that contains 52 cards, and each card has a value and suit?
 
Do you mean something like this for interface part?

Code:
@interface deck : NSObject

@property int spade, club, heart, diamond;

@end
 
You could have one property 'suit'

And it would just be an enum like
Code:
enum suit
{
  spade = 1,
  club,
  diamond,
  heart
};
 
does enum creates a property ? what is the difference between @property and enum?
 
You should Google a bit to find this stuff out for yourself. The short answer though is that an enum is treated like an integer, so it works the same way as an integer property. I agree this is the perfect situation for an enum (when there is a discrete list of items to choose from).
 
Hey, thank you for your reply. Of course I do google everything rather than definitions i need more like similar examples. Even though, I do have a experience with coding. However, i can not apply what i think :/
Since, I do not know objective c deeply, I look at sources and too many sources mixes everything.

----------

Code:
#import <Foundation/Foundation.h>

@interface cards : NSObject

    
@property int spade, diamond, club, heart;


+ (int)cardsOfSpadeWithCapacity:(NSUInteger)numItems;
+ (int)cardsOfDiamondWithCapacity:(NSUInteger)numItems;
+ (int)cardsOfClubWithCapacity:(NSUInteger)numItems;
+ (int)cardsOfHeartWithCapacity:(NSUInteger)numItems;
    

@end

Code:
@implementation cards

@synthesize spade, diamond, club, heart;

@end

I changed the code with these lines. I am hoping to create arrays for every suit and assign them their to appropriate suit as an object. I hope this is right way to do it. I do not know how to use enum therefor, i do not want to use it.
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.