Object Oriented Card Game

ryansobol
Unregistered
 
Post: #16
kelvin Wrote:better...

but, there's still some little kinks.

The instance method -init... should only be ever called once per alloc'd instance (this is up to you and your code). (This is also why most Cocoa programming tutorials suggest you nest the alloc/inits.) So therefore, you shouldn't ever have to check the instance members against nil, just set them. When you alloc an object it's instance members are all nil/NULL.

I'm assuming you're referencing the instance variables being set in -(Rank *)initWithName: Symbol: method. I think I got the getter/setter technique confused with the alloc/init technique. Here's the method now.

Code:
-(Rank *)initWithName:(NSString *)aName Symbol:(NSString *)aSymbol
{
    self = [super init];
    
    if (self != nil)
    {
        name = [aName retain];
        symbol = [aSymbol retain];
    }
    
    return self;
}


kelvin Wrote:also, in your dealloc method...
[super dealloc]; should always be the last line of your subclass' dealloc method. Calling it first like you do can release important stuff and cause serious problems. Do your subclass member clean up, then pass to the superclass to do the final clean up.

My bad. Here's the new -dealloc method.

Code:
-(void)dealloc
{    
    [name release];
    [symbol release];
    
    [super dealloc];
}
Quote this message in a reply
Member
Posts: 196
Joined: 2003.10
Post: #17
I dunno, but doesn't this seem a little overkill? I'm interested, because you've managed to make it act pretty "english" (i.e. no cryptic arrays of cards, etc). But I would have never thought to do it this way... Neat, though. Keep it up.
Quote this message in a reply
ryansobol
Unregistered
 
Post: #18
Honestly, I wouldn't have thought about creating complete classes to represent primative aspects of a playing card. Originally, I thought about using a C enumeration or possibly a C struct. After I stumbled across the programming challenge, I really enjoyed the author's explanation of the problem and the typical pit-falls made by novice programmers. If you get a chance, read the article that I included in my first post. It really helped me understand some of the decisions to 'overkill' the problem.
Quote this message in a reply
ryansobol
Unregistered
 
Post: #19
After running a few tests, I'm finding that all my nice static class objects aren't being initialized properly. I ran the gdb and realized that the +initialize method is NOT sending messages to the -initWithName: Symbol: method at all. This leads me to believe that +initialize methods cannot send messages to instance methods of the same class. I've googled for more information on this, but so far, nothing definitive has been said about using custom instance methods inside the +initialize method. Anyone have a clue as to what might be going on?
Quote this message in a reply
Member
Posts: 469
Joined: 2002.10
Post: #20
try changing...
Quote:ACE = [[Rank alloc] initWithName:@"Ace" Symbol:@"A"];
to...
Code:
ACE =    [[self alloc] initWithName:@"Ace"    Symbol:@"A"];
in your +initialize classmethod.

---Kelvin--
15.4" MacBook Pro revA
1.83GHz/2GB/250GB
Quote this message in a reply
Member
Posts: 469
Joined: 2002.10
Post: #21
and get rid of those allocWithZone, etc. overrides. If you're returning nil from allocWithZone, you're not gonna be able to allocate anything in the first place.

---Kelvin--
15.4" MacBook Pro revA
1.83GHz/2GB/250GB
Quote this message in a reply
Member
Posts: 370
Joined: 2002.04
Post: #22
Don't override the basic release, retain, autorelease mechanism unless you have a really good reason. Instead, just ensure that you retain it once when its created and nobody releases it.

Did you ever wonder why we had to run for shelter when the promise of a brave new world unfurled beneath the clear blue sky?
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Game engines with object programming JonnyThunder 3 8,142 Aug 16, 2010 10:42 AM
Last Post: SethWillits
  Coding A Card Game Jerm #1 3 5,112 Apr 11, 2010 03:05 PM
Last Post: TimMcD