Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8250803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:01:18+00:00 2026-06-08T00:01:18+00:00

I have built a specialized card application. What it does is allow a user

  • 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T00:01:20+00:00Added an answer on June 8, 2026 at 12:01 am

    If I am understanding this correctly, the problem is that it is inserting the card at index 0 in indexArray?

    Okay, have you tried something like this:

    (don’t use this line yet [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];
    }
    

    Your code seems to be okay, I’m guessing it just doesn’t like removing the object before… I added the log just so you could see if it really was inserting it at the top each time. Give me an update- if this doesn’t work, I could take a look at your project file.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have built so far an application that allows the user to drag and
I have built a site which requires user to login. Now I have decided
I have built ICS source using make -j4, then I have modified the Music
I have built up an array of objects, created from a class, I wrote
I have built a local DVD Database using Codeigniter, With film names etc in.
I have built a Custom MaskedTextBox , changing the values of BeepOnError and AsciiOnly
I have built a mock object using EasyMock, and I'm trying to have the
I have built a game in HTML5 and a web form posts data to
I have built the UI for my Google Chrome Extension, but I am trying
I have built a site using php and want to try keep it one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.