0

I'm trying to make the game 2048 in C#, but I have a problem when I move the tiles.

Here is where I keep the tiles:

picture = new PictureBox[4, 4] {
               {pic1,pic2,pic3,pic4},
               {pic5,pic6,pic7,pic8}, 
               {pic9,pic10,pic11,pic12}, 
               {pic13,pic14,pic15,pic16} };

And when I press 'W' I move it up :

public void Up()
    {
        for (int i = 1; i < picture.GetLength(0); i++)
            for (int j = 0; j < picture.GetLength(1); j++)
                if(picture[i,j].Image!=null && picture[i-1,j].Image==null)
                {
                    picture[i - 1, j].Image = picture[i, j].Image;
                    picture[i, j].Image = null;           
                    Up();       
                }

    }

but that code only interchanges images from picturebox not the picturebox itself. How can I interchange the picturebox with image and all its property ?

TaW
  • 53,122
  • 8
  • 69
  • 111
Ursaciuc Adrian
  • 37
  • 1
  • 2
  • 10
  • Why is interchanging the `Image`s inside the picture box not enough? If you want to move the picture boxes instead, you need to modify the `Location` property of the `PictureBox`. – Maximilian Gerhardt Dec 31 '15 at 15:19
  • When i try to make the sum of 2 tiles i need the exact index , if i change just the images the index remain to the initial position – Ursaciuc Adrian Dec 31 '15 at 15:26
  • You need a better dataformat for the "Map" of the game than just a 2-dimensional array of `PictureBox`es, it'll be hard to tell what value the pictureBox presents (even with reflection and relying on the variable's name, if you thought about that). You could e.g. use a 2-dimensional array of `int`s. and operate on that. `var map = new int[4,4] = { { 1, 1, 1, 0 }, ....};` Regarding the "Interchanging": Have a look at http://stackoverflow.com/questions/3473597/it-is-possible-to-copy-all-the-properties-of-a-certain-control-c-window-forms for how to clone a control with reflection. – Maximilian Gerhardt Dec 31 '15 at 15:33
  • You simply need one helper PictureBox variable and then code just as you want using three assignments: `ph=p1;p1=p2;p2=ph;` – TaW Dec 31 '15 at 15:52
  • I try it but is crashing, can you give me a more explicit code? – Ursaciuc Adrian Dec 31 '15 at 15:57

1 Answers1

0

There is no switching functionality in C#, so you will need a temporary helper variable.

It is up to you to decide what you want to switch:

  • the pictureBoxes referenced in your array
  • just their locations
  • their images
  • or any combination..

Here is an example of a simple helper function that exchanges the references and their Locations:

void SwitchControls(PictureBox pb1, PictureBox pb2)
{
    PictureBox temp = pb1;
    Point tempLoc = pb1.Location;
    pb1 = pb2;
    pb2 = temp;
    pb1.Location = pb2.Location;
    pb2.Location = tempLoc ;
}

You would call it for example like this:

SwitchControls(picture[i - 1, j], picture[i, j]);

To exchange the Images you would write the function as

void SwitchImages(PictureBox pb1, PictureBox pb2)
{
    Image temp = pb1.Image;
    pb1.Image = pb2.Image;
    pb2.Image = temp.Image;
}

etc..

TaW
  • 53,122
  • 8
  • 69
  • 111
  • Note that this doesn't switch any images in your GUI or anything, it just *exchanges* the `PictureBox`es these variables were pointing to (i.e. switching references). Not sure what you expect this code to do for you. – Maximilian Gerhardt Dec 31 '15 at 19:20
  • As noted it is up to you to adapt the code to what you want to happen. I have added a variant for exchanging the Images. Note that we do not see all of your code, so there may well be other properties of interest in the PictureBoxes. You could also combine the first version with additionally echanging the Locations for the same visual effect.. I have expanded to first version to do so ! – TaW Dec 31 '15 at 19:40