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

  • SEARCH
  • Home
  • 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 7718935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:17:23+00:00 2026-06-01T03:17:23+00:00

I have a simple jigsaw puzzle i’m making. I have a method that is

  • 0

I have a simple jigsaw puzzle i’m making. I have a method that is called when the view is loaded and when the device is shaken. This method places 4 images in 4 specific places on screen. Below is the code:

-(void) makePieceHolders {
//create 4 points where the jigsaw pieces (images) will be placed
CGPoint holder1 = CGPointMake(80, 80);
CGPoint holder2 = CGPointMake(200, 80);
CGPoint holder3 = CGPointMake(80, 200);
CGPoint holder4 = CGPointMake(200, 200);

image1.center = holder1;    //set the position of the image center to one of the newly created points
image1.alpha = 0.3;         //set the image opacity back to 0.3
image2.center = holder2;
image2.alpha = 0.3;
image3.center = holder3;
image3.alpha = 0.3;
image4.center = holder4;
image4.alpha = 0.3;
}

What i’d like is to place the images randomly in the four placeholders. I have some more code written below where i get a random number between 1 and 4 and set the tag of each image to each of these random numbers.

int randomNumber;
int placeHolders[4];
int i=0;
bool numberFound;

do{ // until you get 4 unique numbers
    randomNumber=arc4random()%4+1;
    // Does this number exist already?
    numberFound=FALSE;
    for (int j=0; j<i; j++) {
        if (placeHolders[j]==randomNumber)
            numberFound=TRUE;
    }
    if (numberFound==FALSE){
        placeHolders[i]=randomNumber;
        i++;
    }
} while (i<4);

image1.tag = placeHolders[0];
image2.tag = placeHolders[1];
image3.tag = placeHolders[2];
image4.tag = placeHolders[3];


NSLog(@"img1 tag: %i img2 tag: %i img3 tag: %i img4 tag: %i", image1.tag, image2.tag, image3.tag, image4.tag);

How do now refer to this tag information in order to move it to a placeholder?

In pseudocode i was thinking:

where image tag = 1, move that image to holder1
where image tag = 2, move that image to holder2
............

I don’t know how to write this though.

If there is a better way i’d appreciate the help. Thanks

  • 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-01T03:17:25+00:00Added an answer on June 1, 2026 at 3:17 am

    You don’t need your complicated do..while / tag logic.
    Just use an array:

    NSMutableArray* images = [NSMutableArray arrayWithObjects: image1,image2,image3,image4,nil];
    
    // shuffle the array
    NSUInteger count = [images count];
    for (NSUInteger i = 0; i < count; i++) {
        // Select a random element between i and end of array to swap with.
        int nElements = count - i;
        int n = (arc4random() % nElements) + i;
        [images exchangeObjectAtIndex:i withObjectAtIndex:n];
    }
    

    After that, you have randomly placed your images in a new order. After that assign the positions:

    UIImageView* imageView1 = (UIImageView*)[images objectAtIndex: 0];
    imageView.center = holder1;
    UIImageView* imageView2 = (UIImageView*)[images objectAtIndex: 1];
    imageView.center = holder2;
    UIImageView* imageView3 = (UIImageView*)[images objectAtIndex: 2];
    imageView.center = holder3;
    UIImageView* imageView4 = (UIImageView*)[images objectAtIndex: 3];
    imageView.center = holder4;
    

    (You could also do this in a loop.. so it would be more general and reusable.)

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

Sidebar

Related Questions

I am creating a simple Jigsaw puzzle. In order to do this, I need
I have simple win service, that executes few tasks periodically. How should I pass
I have simple form. <form target=_blank action=somescript.php method=Post id=simpleForm> <input type=hidden name=url value=http://...> <input
I have simple WinForms application where modifying Windows Registry. The problem is that in
i have simple side menu with this html code : <div id=menu> <div> Menu
i have simple QStandardItemModel that holds the date to show in QTreeview when i
I have simple aspx page that has a form and a input field in
I have simple user model: var user = Backbone.Model.extend({ initialize: function(){ this.bind(change:auth, function (){
i have simple url of image, i want to display that image in Bitmap
I have simple front controller plugin which contains this code: http://pastebin.com/m155c59b0 When session expire

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.