Objective - C Beginner question

Discussion in 'Mac Programming' started by onqun, Apr 19, 2012.

  1. macrumors member

    Joined:
    Oct 13, 2006
    #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 !
     
  2. macrumors 68030

    Joined:
    Oct 19, 2011
    Location:
    Switzerland
    #2
    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.
     
  3. Vsching, Apr 19, 2012
    Last edited by a moderator: Apr 20, 2012

    macrumors newbie

    Joined:
    Apr 19, 2012
    #3
    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.
     
  4. thread starter macrumors member

    Joined:
    Oct 13, 2006
    #4
    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
     
  5. macrumors 68000

    Joined:
    Mar 29, 2009
    Location:
    USA
    #5
    Have you though about a deck object that contains 52 cards, and each card has a value and suit?
     
  6. thread starter macrumors member

    Joined:
    Oct 13, 2006
    #6
    Do you mean something like this for interface part?

    Code:
    
    @interface deck : NSObject
    
    @property int spade, club, heart, diamond;
    
    @end
    
    
     
  7. macrumors 68000

    Joined:
    Mar 29, 2009
    Location:
    USA
    #7
    You could have one property 'suit'

    And it would just be an enum like
    Code:
    enum suit
    {
      spade = 1,
      club,
      diamond,
      heart
    };
     
  8. thread starter macrumors member

    Joined:
    Oct 13, 2006
    #8
    does enum creates a property ? what is the difference between @property and enum?
     
  9. macrumors 6502a

    GorillaPaws

    Joined:
    Oct 26, 2003
    Location:
    Richmond, VA
    #9
    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).
     
  10. onqun, Apr 21, 2012
    Last edited: Apr 21, 2012

    thread starter macrumors member

    Joined:
    Oct 13, 2006
    #10
    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.
     

Share This Page