Note that there are some explanatory texts on larger screens.

plurals
  1. POCan't quite get the randomization down
    primarykey
    data
    text
    <p>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.</p> <p>Here is the contents of the .h file:</p> <pre><code>@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 </code></pre> <p>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:</p> <pre><code>- (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; } </code></pre> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload