自定义卡片组应用程序的数据和类设计



我正在为一个自定义塔罗牌组制作一个应用程序,该应用程序应该能够洗牌、选择一张牌并给出对牌的描述。

我的主要问题是:

  1. 在Card类中用作数据持有者的内容。总共有36张牌。每个都有不同的png/文本作为正面图像/描述,但每个都有相同的背面图像(就像扑克牌一样)。我假设这将是某种数组,但我不知道如何声明两个图像和文本(前/后/描述)并将其链接到一个索引位置,或者如果我需要3个单独的数组,那么我如何将它们彼此链接,以便它们都能获得正确的数据?

  2. 牌组类:我假设这将是一个空数组,它是在洗牌后从牌组中得到的对象吗?我有一个很好的shuffle方法,我一直在NSLog控制台中尝试,但基本上需要在任何卡类上实现它?甲板将显示在"FlowCover"中(http://chaosinmotion.com/flowcover.html)。这是有效的,我已经整理了"didselect"方法来更改视图,但是-

  3. 选择:我不确定什么对象将保存并将所选数据从甲板传递到所选视图。我想它必须是与卡片类相同的对象?

  1. 将三个"事物"链接到单个索引位置的典型方法是将这些事物放入单个类中,然后将该类的对象放在索引位置
  2. 甲板可以是一个简单的NSMutableArray,它是objective-c的可编辑对象容器
  3. 选择可以是第二个NSMutableArray,也可以将选定的属性添加到每个卡中。两者都是可行的选择,但您的算法会有所不同

因此,有一个CardClass,它包含一个静态的背面图像(即背面图像存在于您从中实例化的每个对象中)。为正面图像和说明的类添加属性。然后将列表创建为这些对象的集合。

//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

扩展您的NSMutableClass以拥有一个shuffle例程。请参阅What';打乱NSMutableArray的最佳方法是什么?

两个常量数组,test和front-image。你的牌组是一个简单的整数数组(1-36)。所以洗牌,最上面的牌是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.

如果你的视图只是一张卡片的两个管理和文本,那么它不需要知道卡片类或卡片组类的任何信息,只需要这三个参数。

最新更新