我已经建立了一个专门的卡应用程序。它的功能是允许用户“画”一张卡片,然后查看该卡片,并将其放回卡座中的随机位置。我遇到的唯一问题是,卡通常放在甲板的顶部。不能完全得到随机化
这里是.h文件的内容:
@class _Card;
@interface _Deck : NSObject
@property (readonly) NSString *deckData;
@property (readonly) NSUInteger count;
@property (readonly) NSUInteger deckCount;
@property (readonly) BOOL needsReset;
@property (readonly) NSArray *cards;
- (id)initWithArray:(NSArray *)array;
- (id)initWithContentsOfFile:(NSString *)filePath;
- (void)shuffle;
- (NSArray *)draw;
- (void)reset;
- (void)changeEdition;
@end
现在,这里是我的画法,将抓一张牌(不止一个,如果卡,让指定它),然后把该卡回到甲板,如果允许的话:
- (NSArray *)draw {
// first, we create an array that can be returned, populated with the
// cards that we drew
NSMutableArray *drawArray = [[[NSMutableArray alloc] init] autorelease];
// next, we get the top card, which is actually located in the
// indexArray (I use this for shuffling, pulling cards, etc.)
NSNumber *index = [[[indexArray objectAtIndex:0] retain] autorelease];
// now we get the card that the index corresponds to
// from the cards array
_Card *card = [cards objectAtIndex:[index integerValue]];
// now I remove the index that we
// got from the indexArray...don't worry,
// it might be put back in
[indexArray removeObject:index];
// if the card is supposed to discard after
// draw, we leave it out
if(!card.discard) {
int insertIndex = arc4random_uniform(indexArray.count);
// then I insert the card into the deck using the random, random
// number
[indexArray insertObject:index atIndex:insertIndex];
}
_Card *cardCopy = [card copy];
// we add the card to the array
// that we will return
[drawArray addObject:cardCopy];
// next, if the card is not the final card...
if(!card.final) {
// ...and the card has an
// additional draw specified...
if(card.force) {
// we keep drawing until we need to stop
[drawArray addObjectsFromArray:[self draw]];
}
}
return drawArray;
}
有什么我可能做错了吗?如果您需要更多信息,请告诉我。预先感谢您提供的任何帮助。
那么,我正在做的是我有两个'NSMutableArray'对象。第一个(卡片)按照它们在plist文件中指定的顺序保存所有_Card对象。第二个(indexArray)包含一组代表卡片'索引'编号的'NSNumber'对象。我使用Fisher-Yates算法随机化'indexArray'数组。然后我拉出最顶端的'索引'(模拟顶部卡的拉动)。然后,我尝试将该索引随机放回'indexArray'中。希望这有助于理解这个问题 – 2012-07-18 18:05:51
我这样做,并建议什么测试,现在似乎给人的牌很好的随机化。仍然一遍又一遍地拉同一张牌,但以更可接受的方式。谢谢 – 2012-07-18 19:36:23