2012-07-18 94 views
0

我已经建立了一个专门的卡应用程序。它的功能是允许用户“画”一张卡片,然后查看该卡片,并将其放回卡座中的随机位置。我遇到的唯一问题是,卡通常放在甲板的顶部。不能完全得到随机化

这里是.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; 
} 

有什么我可能做错了吗?如果您需要更多信息,请告诉我。预先感谢您提供的任何帮助。

回答

0

如果我正确理解这个问题,问题是它插入索引0索引阵列卡?

好了,你有没有尝试过这样的事情:

(不使用该行尚未[indexArray removeObject:index];

if(!card.discard) 
{ 
    int insertIndex = arc4random_uniform(indexArray.count); 
    id obj = [indexArray objectAtIndex:index]; 
    [indexArray removeObjectAtIndex:index]; 
    [indexArray insertObject:obj atIndex:insertIndex]; 
NSLog(@"insertIndex is %i and obj is %@", insertIndex, obj); 
} 
else 
{ 
    [indexArray removeObjectAtIndex:index]; 
} 

您的代码似乎是好了,我猜它只是不就像在删除对象之前......我添加了日志,以便您可以看到它是否真的每次都将其插入顶部。给我一个更新 - 如果这不起作用,我可以看看你的项目文件。

+0

那么,我正在做的是我有两个'NSMutableArray'对象。第一个(卡片)按照它们在plist文件中指定的顺序保存所有_Card对象。第二个(indexArray)包含一组代表卡片'索引'编号的'NSNumber'对象。我使用Fisher-Yates算法随机化'indexArray'数组。然后我拉出最顶端的'索引'(模拟顶部卡的拉动)。然后,我尝试将该索引随机放回'indexArray'中。希望这有助于理解这个问题 – 2012-07-18 18:05:51

+0

我这样做,并建议什么测试,现在似乎给人的牌很好的随机化。仍然一遍又一遍地拉同一张牌,但以更可接受的方式。谢谢 – 2012-07-18 19:36:23

0

“往往不是”是什么意思?

什么你看这里看上去很完美....

请记住,这是随机,这是完全可能的(虽然罕见)在一行中获取特定数量的10倍。 长期来看你应该得到一个平均分配。

运行这个程序10,000,000次左右,并检查你决定什么是错的前得到每个号码(请确保您有在每次甲板相同数量的卡)的次数。

此外,你确定你的indexArray包含正确的值,你没有在那里复制0?

+0

我会尝试像这样长时间运行例程。索引的重复确实在我脑海中浮现,我确信我没有。由于 – 2012-07-18 18:59:35

+1

另一个很好的问题将是“为'indexArray'肯定与内容的真实目的,即是'indexArray.count'不返回0,所有的时间?” – 2012-07-18 19:00:45

+0

@JoshCaswell好点! – lnafziger 2012-07-18 19:02:59