INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Throw of NSException

Throw of NSException

(OP)
I have been trying to write a card game for the iphone. for some reason whenever i try to print the contents of an NSMutableArray it crashes. I don't understand what is going on. I have always programmed in java, but recently decided to teach myself objective-c. Any advice would be very helpful.

here is the source code for the files.

CODE

============================================================
#import <Cocoa/Cocoa.h>
#import "Card.h"

@interface Deck : NSObject {
    NSMutableArray *deck;
}

-(id) initDeck;

//fill the array with card objects. it returns void cause it points to the same object
-(void) fillDeck: (NSMutableArray *) anArray;

//create an array and copied the old array card objects in a different order
//then return the new shuffled array
-(NSMutableArray *) shuffle: (NSMutableArray *) aDeck;

-(void) dealloc;

@end

============================================================
#import "Deck.h"

@implementation Deck

-(id) initDeck {
    [super init];
    deck = [[NSMutableArray alloc] initWithCapacity:52];
    
    return self;
}

-(void) fillDeck: (NSMutableArray *) anArray {
    for(int i = 1; i <= 4; i++) { //lopp that will go through each suit 1 = hearts, 2 = diamonds, 3 = spades, 4 = clubs
        for(int j = 1; j <= 13; j++) { //loop that will go through each card within the suit
            Card *card = [[Card alloc] initCard:i :j];
            [anArray addObject:card];
        }
    }
}

-(void) dealloc {
    [deck release];
    [super dealloc];
}

@end
============================================================
#import <Cocoa/Cocoa.h>


@interface Card : NSObject {
    
@private
    NSString *suit;
    int value; //cards 2 - 10 are there face value and jack through ace are 11 - 14
}

//the values here are readonly because you never want to be able to change the value of a card or suit once
//it has been initiaiized
@property (assign) NSString *suit;
@property (assign) int value;

//initailze a card object with suit being and itnerger value of the card. 2-10 equals the card number and 11-14 jack - ace
//suit is also an integer representing the card suit such as 1 = hearts, 2 = diamonds, 3 = spades, 4 = clubs
-(id) initCard: (int) aSuit: (int) aValue;

//description of the card object
-(NSString *) description;

-(void) dealloc;

@end

============================================================
//
//  Card.m
//  CardGame
//
//  Created by Macintosh on 11/23/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "Card.h"


@implementation Card

@synthesize suit, value;

-(id) initCard: (int) aSuit: (int) aValue {
    [super init];
    
    NSString * assignValue;
    
    switch (aSuit) {
        case 1:
            if (aSuit == 1) {
                assignValue = @"hearts";
            }
        case 2:
            if (aSuit == 2) {
                assignValue == @"diamonds";
            }
            break;
        case 3:
            if (aSuit == 3 ) {
                assignValue = @"spades";
            }
            break;
        default:
            assignValue = @"clubs";
            break;
    }
    suit = assignValue;
    value = aValue;
    return self;
}

-(NSString *) description {
    NSString *retVal;
        retVal    = (@"%i of %@",value, suit);
    return retVal;
}

-(void) dealloc {
    [suit release];
    [super dealloc];
}

@end
============================================================

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close