2

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?

jscs
  • 63,694
  • 13
  • 151
  • 195
RoshDamunki
  • 109
  • 1
  • 8
  • 1
    Welcome to SO! This isn't a forum; the personal details that are not related to your programming problem just distract from the task at hand, which is answering your question. Further, this seems rather broad and might be best broken up into two or three smaller questions, each addressing a single one of your issues. – jscs Apr 04 '12 at 17:22

2 Answers2

1
  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?

Community
  • 1
  • 1
Rian Rizvi
  • 9,989
  • 2
  • 37
  • 33
  • Hi Riaz. Just to be clear about what you've done in the example. You are declaring each data part as a method -() in the card class? Would the deck array still be in a different class? And would you run your random method over the "Deck" array? If you add a selected property then how do you link the data to that second view? – RoshDamunki Apr 05 '12 at 09:33
  • Hi Rosh, I took some shortcuts here by only specifying the interface of the class. I'll write it up in full later today. – Rian Rizvi Apr 05 '12 at 16:44
  • each data part is a member variable that is accessed via a method called a property accessor. the deck array is a different class, it is an extended NSMutableArray, so you don't have to write the class just the extend it with a shuffle/random method (see link), ie you would call the random method of your deck object. If you add a selected property to the card class, then you could simply iterate through the deck when you want to show what is selected, this way you wouldn't need a second selecteddeck object. Or you could have a second selecteddeck object that just contains references, up to you – Rian Rizvi Apr 06 '12 at 08:06
  • Does the Card class really need a back property - since it is always the same i guess you can easily remove the property. – Pfitz Apr 06 '12 at 08:26
  • I think it will make the use of the class more coherent, all the attributes of a card are accessed from the same place. – Rian Rizvi Apr 06 '12 at 21:22
  • Thanks riaz I'm pretty sure it's working - on to the next problem..! – RoshDamunki Apr 16 '12 at 16:00
0

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.

Tony Hopkinson
  • 20,172
  • 3
  • 31
  • 39