PDA

View Full Version : Objective - C Beginner question




onqun
Apr 19, 2012, 05:24 PM
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 !



thundersteele
Apr 19, 2012, 08:17 PM
This creates a spade card and sets it's int value to 1, which I assume should be the Ace:

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.

Vsching
Apr 19, 2012, 08:20 PM
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 :)

-(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.

onqun
Apr 20, 2012, 03:04 AM
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

robvas
Apr 20, 2012, 09:25 AM
Have you though about a deck object that contains 52 cards, and each card has a value and suit?

onqun
Apr 20, 2012, 09:32 AM
Do you mean something like this for interface part?



@interface deck : NSObject

@property int spade, club, heart, diamond;

@end

robvas
Apr 20, 2012, 07:53 PM
You could have one property 'suit'

And it would just be an enum like

enum suit
{
spade = 1,
club,
diamond,
heart
};

onqun
Apr 21, 2012, 01:23 PM
does enum creates a property ? what is the difference between @property and enum?

GorillaPaws
Apr 21, 2012, 01:36 PM
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).

onqun
Apr 21, 2012, 03:29 PM
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.

----------

#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


@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.