2012-07-18 26 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