this snippet below is my code.

It's supposed to be a memory game, but i have some problems making it work, I've made a button_1 that generates 6 random numbers in random order, those 6 numbers are supposed to tell the "p1 - p6" (pictures (2 of each to make pairs)) what picture boxes, they're getting in to, I'd really like it to be random what pictures, goes into the picture boxes.
problems i have:

1. random numbers cant get into the array "R[6]" and then i cant use it down in the "pictureboxes" to get assigned to a picture.
2. when 2 pictures are flipped with the same picture, i need them to disappear, (after a timer)

its supposed to give point for every pair of match,
in this code, there's only 6 picture boxes, i need like 25-30
i only got 3 different pictures in this code, again i need like 6 so that there's 5 pairs of each picture if you got any solutions, or questions regarding what i need in my game, please e-mail me on <<snip>>

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Memory_try_1
{
    public partial class Form1 : Form
    {
        System.Random randi = new System.Random();

        Image b1 = Properties.Resources.bagside;

        Image[] pic = new Image[6];
        Bitmap[] imgArray = new Bitmap[6];
        PictureBox[] boxArray = new PictureBox[6];

        Image p1 = Properties.Resources.Creek;
        Image p2 = Properties.Resources.Creek;
        Image p3 = Properties.Resources.Toco_Toucan;
        Image p4 = Properties.Resources.Toco_Toucan;
        Image p5 = Properties.Resources.Green_Sea_Turtle;
        Image p6 = Properties.Resources.Green_Sea_Turtle;

        Image førstepic = Properties.Resources.bagside;
        PictureBox førstepicturebox = new PictureBox();

        bool forside1 = false;
        bool forside2 = false;                  // bare over with me, there is alot of un-used varibels in this program
        bool forside3 = false;                  // thats's cause i've tryed manny solutions, but when i tryed something else
        bool forside4 = false;                  // all of them werent delleted
        bool forside5 = false;
        bool forside6 = false;

        bool klik = true; // if klik is true, its's the first click of 2

        int kortnummer = 0;
        int i = 0;

        int [] R = new int[6];
        int R1 = 0;
        int R2 = 0;
        int R3 = 0;
        int R4 = 0;
        int R5 = 0;
        int R6 = 0;

        int førindex = 0;

        int clicks = 0;

        public Form1()
        {

            InitializeComponent();

            pictureBox1.Image = b1;
            pictureBox2.Image = b1;
            pictureBox3.Image = b1;
            pictureBox4.Image = b1;
            pictureBox5.Image = b1;
            pictureBox6.Image = b1;       

        }            
            
        

            private void button1_Click(object sender, EventArgs e) // the button that starts the game
            {
                    R[1] = randi.Next(1, 7);
                do
                    R[2] = randi.Next(1, 7);                            // here i make 6 random numbers in a random order
                while (R[2] == R[1]);                                   // and define them in an array "R" R[1 to 6]
                do
                    R[3] = randi.Next(1, 7);                            
                while (R[3] == R[1] || R[3] == R[2]);
                do
                    R[4] = randi.Next(1, 7);
                while (R[4] == R[1] || R[4] == R[2] || R[4] == R[3]);
                do
                    R[5] = randi.Next(1, 7);
                while (R[5] == R[1] || R[5] == R[2] || R[5] == R[3] || R[5] == R[4]);
                do
                    R[6] = randi.Next(1, 7);
                while (R[6] == R[1] || R[6] == R[2] || R[6] == R[3] || R[6] == R[4] || R[6] == R[5]);

                pic[R1 - 1] = p1;
                pic[R2 - 1] = p2;
                pic[R3 - 1] = p3;
                pic[R4 - 1] = p4;
                pic[R5 - 1] = p5;
                pic[R6 - 1] = p6;

                pictureBox1.Image = b1;
                pictureBox2.Image = b1;
                pictureBox3.Image = b1;
                pictureBox4.Image = b1;
                pictureBox5.Image = b1;
                pictureBox6.Image = b1;
                pictureboxtest.Image = b1;
            }

            private void pictureBox1_Click(object sender, EventArgs e)
            {

                if (forside1)
                    pictureBox1.Image = b1;

                else
                {
                    pictureBox1.Image = pic[0];
                }
                forside1 = !forside1;

                if (klik)
                {
                    i = 0;
                    while (R[i++] != 1) ;

                    førindex = i - 1;

                }

                else
                    i = 0;
                while (R[i++] != 1) ;


                switch (i - 1)
                {
                    case 0: if (førindex == 1)//bite


                            ;
                        break;
                    case 1: if (førindex == 0)//bite

                            ;
                        break;
                    case 2: if (førindex == 3)//bite

                            ;
                        break;
                    case 3: if (førindex == 2)//bite

                            ;
                        break;
                    case 4: if (førindex == 5)//bite

                            ;
                        break;
                    case 5: if (førindex == 4)//bite

                            ;
                        break;

                    // bite = they together make a match, and needs to be removed
                }
                klik = !klik;
            }

            private void pictureBox2_Click(object sender, EventArgs e)
            {

                if (forside2)
                    pictureBox2.Image = b1;

                else
                {
                    pictureBox2.Image = pic[1];
                }
                forside2 = !forside2;

                if (klik)
                {
                    i = 0;
                    while (R[i++] != 2) ;

                    førindex = i - 1;

                }

                else
                    i = 0;
                while (R[i++] != 2) ;


                switch (i - 1)
                {
                    case 0: if (førindex == 1)//bite


                            ;
                        break;
                    case 1: if (førindex == 0)//bite
                        
                            ;
                        break;
                    case 2: if (førindex == 3)//bite

                            ;
                        break;
                    case 3: if (førindex == 2)//bite

                            ;
                        break;
                    case 4: if (førindex == 5)//bite

                            ;
                        break;
                    case 5: if (førindex == 4)//bite

                            ;
                        break;

                    // bite = they together make a match, and needs to be removed
                }
                klik = !klik;
            }

            private void pictureBox3_Click(object sender, EventArgs e)
            {

                if (forside3)
                    pictureBox3.Image = b1;

                else
                {
                    pictureBox3.Image = pic[2];
                }
                forside3 = !forside3;

                if (klik)
                {
                    i = 0;
                    while (R[i++] != 3) ;

                    førindex = i - 1;

                }

                else
                    i = 0;
                while (R[i++] != 3) ;


                switch (i - 1)
                {
                    case 0: if (førindex == 1)//bite


                            ;
                        break;
                    case 1: if (førindex == 0)//bite

                            ;
                        break;
                    case 2: if (førindex == 3)//bite

                            ;
                        break;
                    case 3: if (førindex == 2)//bite

                            ;
                        break;
                    case 4: if (førindex == 5)//bite

                            ;
                        break;
                    case 5: if (førindex == 4)//bite

                            ;
                        break;

                    // bite = they together make a match, and needs to be removed
                }
                klik = !klik;
            }

            private void pictureBox4_Click(object sender, EventArgs e)
            {

                if (forside4)
                    pictureBox4.Image = b1;

                else
                {
                    pictureBox4.Image = pic[3];
                }
                forside4 = !forside4;

                if (klik)
                {
                    i = 0;
                    while (R[i++] != 4) ;

                    førindex = i - 1;

                }

                else
                    i = 0;
                while (R[i++] != 4) ;


                switch (i - 1)
                {
                    case 0: if (førindex == 1)//bite


                            ;
                        break;
                    case 1: if (førindex == 0)//bite

                            ;
                        break;
                    case 2: if (førindex == 3)//bite

                            ;
                        break;
                    case 3: if (førindex == 2)//bite

                            ;
                        break;
                    case 4: if (førindex == 5)//bite

                            ;
                        break;
                    case 5: if (førindex == 4)//bite

                            ;
                        break;

                    // bite = they together make a match, and needs to be removed
                }
                klik = !klik;
            }

            private void pictureBox5_Click(object sender, EventArgs e)
            {

                if (forside5)
                    pictureBox5.Image = b1;

                else
                {
                    pictureBox5.Image = pic[4];
                }
                forside5 = !forside5;

                if (klik)
                {
                    i = 0;
                    while (R[i++] != 5) ;

                    førindex = i - 1;

                }

                else
                    i = 0;
                while (R[i++] != 5) ;


                switch (i - 1)
                {
                    case 0: if (førindex == 1)//bite


                            ;
                        break;
                    case 1: if (førindex == 0)//bite

                            ;
                        break;
                    case 2: if (førindex == 3)//bite

                            ;
                        break;
                    case 3: if (førindex == 2)//bite

                            ;
                        break;
                    case 4: if (førindex == 5)//bite

                            ;
                        break;
                    case 5: if (førindex == 4)//bite

                            ;
                        break;

                    // bite = they together make a match, and needs to be removed
                }
                klik = !klik;
            } 

            private void pictureBox6_Click(object sender, EventArgs e)
            {

                if (forside6)
                    pictureBox6.Image = b1;

                else
                {
                    pictureBox6.Image = pic[5];
                }
                forside6 = !forside6;

                if (klik)
                {
                    i = 0;
                    while (R[i++] != 6) ;

                    førindex = i - 1;

                }

                else
                    i = 0;
                    while (R[i++] != 6) ;


                    switch (i-1)
                    {
                        case 0: if (førindex == 1)//bite

                                
                                ;
                            break;
                        case 1: if (førindex == 0)//bite

                                ;
                                break;
                        case 2: if (førindex == 3)//bite

                                ;
                                break;
                        case 3: if (førindex == 2)//bite

                                ;
                                break;
                        case 4: if (førindex == 5)//bite

                                ;
                                break;
                        case 5: if (førindex == 4)//bite

                                ;
                                break;
                        
                                // bite = they together make a match, and needs to be removed
                    }
                 klik = !klik;  
            }      
    }
}
// firstly, let's make it easier to use these:
    /*
    Image p1 = Properties.Resources.Creek;
    Image p2 = Properties.Resources.Creek;
    Image p3 = Properties.Resources.Toco_Toucan;
    Image p4 = Properties.Resources.Toco_Toucan;
    Image p5 = Properties.Resources.Green_Sea_Turtle;
    Image p6 = Properties.Resources.Green_Sea_Turtle;
    */
    Image[] images = new Image[6] // I have made an array and initialized it, it will be easier to use later
    { 
        Properties.Resources.Creek, 
        Properties.Resources.Creek,
        Properties.Resources.Toco_Toucan,
        Properties.Resources.Toco_Toucan,
        Properties.Resources.Green_Sea_Turtle,
        Properties.Resources.Green_Sea_Turtle
    };
private void button1_Click(object sender, EventArgs e) // the button that starts the <strong class="highlight">game</strong>
    {
        /* Ok, this is a very poor way to do this
         * it is non-scalable, so if you decide to have more than  6 images, you will need 
         * more of these
         * so, let's get rid of them
        R[1] = randi.Next(1, 7);
        do R[2] = randi.Next(1, 7);                            // here i make 6 random numbers in a random order     
        while (R[2] == R[1]);                                   // and define them in an array "R" R[1 to 6]                
        do
            R[3] = randi.Next(1, 7);
        while (R[3] == R[1] || R[3] == R[2]);
        do
            R[4] = randi.Next(1, 7);
        while (R[4] == R[1] || R[4] == R[2] || R[4] == R[3]);
        do
            R[5] = randi.Next(1, 7);
        while (R[5] == R[1] || R[5] == R[2] || R[5] == R[3] || R[5] == R[4]);
        do
            R[6] = randi.Next(1, 7);
        while (R[6] == R[1] || R[6] == R[2] || R[6] == R[3] || R[6] == R[4] || R[6] == R[5]);
         * */

        // now there are 2 ways to do this:
        #region ARRAY (yucky)
        // initialize the array
        for (int i = 0; i < R.Length; i++)
        {
            R[i] = -1; // this inits them
        }

        // using an array you need to do somethin like this:
        for (int r = 0; r < R.Length; r++)
        {
            bool isOk = false;
            while (!isOk)
            {
                bool foundIt = false;
                int rTest = randi.Next(6);
                foreach (int i in R)
                {
                    if (i == rTest)
                    {
                        foundIt = true;
                        break;
                    }
                }
                if (!foundIt)
                {
                    R[r] = rTest;
                    isOk = true;
                }
            }
        }
        #endregion
        #region Generic List (YUMMY)
        List<int> myR = new List<int>(6) { -1, -1, -1, -1, -1, -1 };
        // I'd much prefer to use generics, as it has an indexOf
        for (int r = 0; r < myR.Count; r++)
        {
            while (true)
            { // start an infinite loop
                int rTest = randi.Next(6);
                if (myR.IndexOf(rTest) == -1) // not found
                {
                    myR[r] = rTest;
                    break; // break out of loop
                }
            }
        }
        #endregion

        // load them into the pictures
        for (int picIndex = 0; picIndex < pic.Length; picIndex++)
        {
            pic[picIndex] = images[R[picIndex]];
        }

        // NOTE: if you convert all of your arrays to List<T> they will be even 
        // allow you to use .Add(); and .RemoveAt();

        // I suggest you have these an list too, I will do that next to show you how
        pictureBox1.Image = b1;
        pictureBox2.Image = b1;
        pictureBox3.Image = b1;
        pictureBox4.Image = b1;
        pictureBox5.Image = b1;
        pictureBox6.Image = b1;
        pictureboxtest.Image = b1;
    }

A much cleaner way would be to initialise your images and associate the Tag property with their name:

Image imgOne = Resource.YourImage;
imgOne.Tag = ImageNameString;

Then initialise your Image List:

List<Image> imageList = new List<Image>{// Your Images};

Then initialise your PictureBox list:

List<PictureBox> pictureBoxList = new List<PictureBox>();

Ok now we have the basics set up. Begin assigning randomly. Once one picture has been assigned to the picture box, remove it from the list:

while(imageList.Count > 0)
{
     int imageIndex = //random number between 0 and (imageList.Count - 1);
     PictureBox tempBox = new PictureBox { Image = imageList[imageIndex], OnClick = PictureBox_Click };
     picturBoxList.Add(tempBox);
     imageList.RemoveAt(imageIndex);
}

Ok, now remember how the Tag was set before? This stores any information you want. It takes the Object type so you can box and un-box. In this case we've saved a string (your image name).

In order to make a comparison, include a Private PictureBox variable to your form. Here you will save your first selection.

When you make your second selection, compare the tags (you can convert them to strings first, I have assumed you'll be doing this in a Form Wide PictureBox_Click event):

private void PictureBox_Click(object sender, EventArgs args)
{
     // Psuedo code, I don't want to give it allll away ;)
     // Check if first selection variable is null
     // If true, cast sender to PictureBox, store in first selection variable and return
     // else, perform the code below
     String firstString = (String)firstSelection.Image.Tag;
     String secondString = (String)((PictureBox)sender).Image.Tag;
     if(firstString.Equals(secondString))
     {
          // Do your fade out stuff
     }
     firstSelection = null;
}

And that's it pretty much done.

As you can see it's a lot clearer and concise than the old way :)

At a code level, implementing it this way is VERY scalable, but there is nothing to put the picture boxes onto the form. I'll leave that for yourself :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.