0

I have built a specialized card application. What it does is allow a user to 'draw' a card, then view the card, and place it back in a random place in the deck. The only problem that I am having is that, more often than not, the card is being placed at the top of the deck.

Here is the contents of the .h file:

@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

Now, here is my draw method, which will draw a card (more than one if the cards so specify it) and then place that card back into the deck, if it is allowed:

- (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;
}

Is there anything that I may be doing wrong? If you need any more information, please let me know. Thanks in advance for any help that you can provide.

4

2 に答える 2

0

「頻繁に」とはどういう意味ですか?

ここに表示されるものは完璧に見えます....

これはランダムであり、(まれではありますが) 特定の数字が 10 回連続して得られる可能性が非常に高いことに注意してください。 ただし、長期的には均等な分布が得られるはずです。

このルーチンを 10,000,000 回ほど実行し、それぞれの数字が得られる回数を確認してください (デッキに同じ枚数のカードがあることを確認してください)。

また、indexArray に正しい値が含まれており、そこに 0 が重複していないことを確認していますか?

于 2012-07-18T18:52:08.790 に答える
0

これを正しく理解している場合、問題は indexArray のインデックス 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];
}

あなたのコードは問題ないようです。以前にオブジェクトを削除するのが好きではないと思います...ログを追加したのは、毎回実際にオブジェクトが挿入されているかどうかを確認できるようにするためです。最新情報を教えてください。これで問題が解決しない場合は、プロジェクト ファイルを確認できます。

于 2012-07-18T17:49:01.097 に答える