Click here to Skip to main content
12,401,984 members (58,572 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: Windows Forms
is there a way to make a control to change location everytime it runs. not just randomly but perhaps at points x,y and z.
Posted 26-Mar-13 10:17am
Comments
Zoltán Zörgő 26-Mar-13 16:18pm
   
Of course. What have you tried? Z??? Do you have a 3D GUI?
ryanb31 26-Mar-13 16:18pm
   
Sure. Where are you stuck?
Code-genious 26-Mar-13 16:26pm
   
u know those picture games whereby you try to select two same pictures? i wrote it. but now my friends now where the same pictures are and the game is not fun anymore. is any way to make the pictures to swap positions with each other at every run.

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

"u know those picture games whereby you try to select two same pictures? i wrote it. but now my friends now where the same pictures are and the game is not fun anymore. is any way to make the pictures to swap positions with each other at every run."

So, you do want it randomly then. Honest, you do. You just don't want the actual location to be random.

Assume you have three pairs of images (yes, I know it's a bit simple - this is just an example). You set up an array of 6 Images:
private Image[] pics = new Image[6];
You then fill the slots randomly:
private Random rand = new Random();
...
   SetImage(pics, Image1, rand.Next(0, 6));
   SetImage(pics, Image1, rand.Next(0, 6));
   SetImage(pics, Image2, rand.Next(0, 6));
   SetImage(pics, Image2, rand.Next(0, 6));
   SetImage(pics, Image3, rand.Next(0, 6));
   SetImage(pics, Image3, rand.Next(0, 6));
...
private void SetImage(Image[] pics, Image image, int index)
   {
   while (pics[index] != null)
      {
      index++;
      index %= 6;
      }
   pics[index] = image;
   }
You then use the pics array in that order to show the pictures on the screen. Because the order of the pics in the array is random, but the locations aren't, they appear in random positions each time.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160721.1 | Last Updated 26 Mar 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100