RoshDamunki
RoshDamunki

Reputation: 109

Data and class design for custom card deck app

I'm making an app for a custom Tarot deck that should be able to shuffle, choose a card, and give a description of the cards.

My main problems are:

  1. What to use as the data holder in the Card class. There are 36 cards in all. Each has a different png/text for the frontImage/description but each has the same back image (just like a playing deck would). I assumed this would be an array of some sort but I don't know how to declare two images and text (front/back/description) and link it to a single index location, or if I need 3 separate arrays, how then would I link them to each other so that they all get the right data?

  2. The deck class: I assume will be an empty array which is given the objects from the card class after they have been shuffled? I have a good shuffle method that I have been trying in the console with NSLog but basically need to implement it on whatever the card class will be? The deck will then be displayed in "FlowCover" (http://chaosinmotion.com/flowcover.html). This is working and I have sorted out the "didselect" method to change views but-

  3. The selection: I am unsure about what object will hold and pass the selected data from the deck to the selected view. I assume it will have to be the same object as the card class?

Upvotes: 2

Views: 657

Answers (2)

Rian Rizvi
Rian Rizvi

Reputation: 10845

  1. The typical way to link three 'things' to a single index location, is to put the things into a single class, and then put objects of that class at the index location.
  2. The deck can be a simple NSMutableArray, which is objective-c's editable container of objects
  3. The selection can be a second NSMutableArray or you can add a selected property to each card. Both are viable choices but your algorithms will be different depending.

So have a CardClass which contains a static back image (ie the back image is present in every object you instantiate from it). Add properties to the class for the front image and description. Then create your list as a collection of these objects.

//Card.h
@interface Card : NSObject

{
    UIImage * back;
    UIImage * front;
    NSString * description;
}
@property (readonly) UIImage * back;
@property (readonly) UIImage * front;
@property (readonly) NSString * description;
- (id) initWithFront:(UIImage *)setFront Description:(NSString*)setDescription; 
@end

//Card.m
#import "Card.h"

static UIImage * backimage = nil;

@implementation Card
@synthesize back;
@synthesize front;
@synthesize description;

+(void) initialize
{
    if (!backimage){
        backimage = [[UIImage alloc]initWithContentsOfFile:@"imagefile.png"]; //though imagefile.png will be replaced with a reference to a plist.info string 
    }
}

- (id) initWithFront:(UIImage *)setFront Description:(NSString*)setDescription{
    if (self = [super init]){
        front=setFront;
        description= setDescription;
        back = backimage;
    }
    return self;
}
@end

//... elsewhere, perhaps your main viewDidLoad method
NSMutableArray *deck = [NSMutableArray initWithCapacity:36];
Card * card1 = [[CardClass alloc] initWithFront:@"card1.png" Description:@"card 1"];
[deck addObject:card1];
... //etc to create the remaining cards in the whole deck

Extend your NSMutableClass to have a shuffle routine. See What's the Best Way to Shuffle an NSMutableArray?

Upvotes: 1

Tony Hopkinson
Tony Hopkinson

Reputation: 20330

Two constant arrays, test and front image. Your deck is then simple array of integers (1 - 36). So shuffle them, top card is 18

Text is Descriptions[18] (or 17 if zero based arrays)
FrontImage is Images[18] ditto
BackImage is always the same so no point in having 36 of them.

If your view is just the two imnages and the text of one card, then it doesn't need to know anything about the card class or the deck class, just needs those three arguments.

Upvotes: 0

Related Questions