0

I have been making a card matching game in C#. It has a 12 card set up (2 of each photo so 6 photos in total) using a picture array, and when you press the cards it finds out if the photos are a match by using another array (a int array with the numbers 1-6 repeated twice, same as the photos) I use this code to randomize the numbers

            Image away;
            int tagger;
            for (int i = 1; i < 13; i++)
            {
                cards[i].Visible = true;
                away = pics[i];
                tagger = tags[i];

                int h = random.Next(1, 6);
                pics[i] = pics[h];
                tags[i] = tags[h];
                pics[h] = away;
                tags[h] = tagger;
            }
            for (int i = 1; i < 13; i++)
            {
                cards[i].Image = pics[i];
            }

It works for about 4 of the matches, then it says the matches are wrong for the rest... can anyone help?

here is the rest of my code:

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

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
         int a = 0;
         PictureBox card = null;
         Image[] pics = new Image[13];
         int[] tags = new int[13];
    PictureBox[] cards = new PictureBox[13];
    Random random = new Random();
    public Form1()
    {
        InitializeComponent();
        pics[1] = Image.FromFile(@"h:\profile\desktop\game\photos\g1.jpg");
        pics[2] = Image.FromFile(@"h:\profile\desktop\game\photos\g1.jpg");
        pics[3] = Image.FromFile(@"h:\profile\desktop\game\photos\g2.jpg");
        pics[4] = Image.FromFile(@"h:\profile\desktop\game\photos\g2.jpg");
        pics[5] = Image.FromFile(@"h:\profile\desktop\game\photos\g3.jpg");
        pics[6] = Image.FromFile(@"h:\profile\desktop\game\photos\g3.jpg");
        pics[7] = Image.FromFile(@"h:\profile\desktop\game\photos\g4.jpg");
        pics[8] = Image.FromFile(@"h:\profile\desktop\game\photos\g4.jpg");
        pics[9] = Image.FromFile(@"h:\profile\desktop\game\photos\g5.jpg");
        pics[10] = Image.FromFile(@"h:\profile\desktop\game\photos\g5.jpg");
        pics[11] = Image.FromFile(@"h:\profile\desktop\game\photos\g6.jpg");
        pics[12] = Image.FromFile(@"h:\profile\desktop\game\photos\g6.jpg");
        tags[1] = 1;
        tags[2] = 1;
        tags[3] = 2;
        tags[4] = 2;
        tags[5] = 3;
        tags[6] = 3;
        tags[7] = 4;
        tags[8] = 4;
        tags[9] = 5;
        tags[10] = 5;
        tags[11] = 6;
        tags[12] = 6;

        cards[1] = pictureBox1;
        cards[2] = pictureBox2;
        cards[3] = pictureBox3;
        cards[4] = pictureBox4;
        cards[5] = pictureBox8;
        cards[6] = pictureBox7;
        cards[7] = pictureBox6;
        cards[8] = pictureBox5;
        cards[9] = pictureBox9;
        cards[10] = pictureBox10;
        cards[11] = pictureBox11;
        cards[12] = pictureBox12;
    }

    private void click_card(object sender, EventArgs e)
    {
        PictureBox pic = sender as PictureBox;
        string s = pic.Name;
        string s1 = s.Substring(10);
        int num = int.Parse(s1);
        if (card == null)
        {
            card = pic;
            a = num;
        }
        else if (a > 0)
        {
            int one = tags[a];
            int two = tags[num];
            if (one == two)
            {
                System.Windows.Forms.MessageBox.Show("Correct!");
                a = 0;
                pic.Visible = false;
                card.Visible = false;
                card = null;
            }
            else if (one != two)
            {
                System.Windows.Forms.MessageBox.Show("Wrong!");
                card = null;
                a = 0;
            }
        }

    }

    private void button1_Click(object sender, EventArgs e)
    {
            Image away;
            int tagger;
            for (int i = 1; i < 13; i++)
            {
                cards[i].Visible = true;
                away = pics[i];
                tagger = tags[i];

                int h = random.Next(1, 6);
                pics[i] = pics[h];
                tags[i] = tags[h];
                pics[h] = away;
                tags[h] = tagger;
            }
            for (int i = 1; i < 13; i++)
            {
                cards[i].Image = pics[i];
            }
        }
    }
}

1 Answers1

1

To comment on your "shuffler".

If cards is your array, then your for loop should start at 0. Arrays are zero based. You have 12 cards, so you'll 0 - 11 indexes. Then you should use the Length property of the array to control your for loop.

When you use random.next(1, 6), you're only generating a random number from 1 - 5. Indexes 0 & 6 - 11, are never being generated, so are never being shuffled.

Lastly, I don't see why you need the extra for loop, just add the assigning of Image to the bottom of your for loop after the swapping.

Image away;
int tagger;
for (int i = 1; i < cards.Length; i++)
{
    cards[i].Visible = true;
    away = pics[i];
    tagger = tags[i];

    int h = random.Next(1, cards.Length + 1);
    pics[i] = pics[h];
    tags[i] = tags[h];
    pics[h] = away;
    tags[h] = tagger;

    cards[i].Image = pics[i];
}
Shar1er80
  • 9,001
  • 2
  • 20
  • 29
  • i only have 6 cards, the array has 12 cards, 2 of each cards are duplicated, so i only want a number between 1 and 6, then i want the tag array to copy the picture number over – Brandon Zwicker Apr 22 '15 at 17:46
  • 1
    Your code indicates that you have 12 pics, tags, and cards objects. – Shar1er80 Apr 22 '15 at 17:50
  • You should be posting this into your question, and not as a comment. Still see my answer in regards to shuffling. If your variable pics has a 12 index, then pics declaration probably looks like Image[] pics = new Image[13]; – Shar1er80 Apr 22 '15 at 17:55
  • yes, i have also added my whole code to my question. I have tried your answer and it doesn't seem to work – Brandon Zwicker Apr 22 '15 at 17:58
  • @BrandonZwicker Change your array declarations from [13] to [12] and try my shuffler answer. You'll also have to change all the referencing indexes in your constructor from 1 - 12 to 0 - 11. – Shar1er80 Apr 22 '15 at 18:12
  • 1
    @BrandonZwicker Alright... Change it back and see edits to my answer. You should understand that arrays are zero based. Since you're using indexes 1 - 12, you have an unused index at index 0. – Shar1er80 Apr 22 '15 at 18:19
  • @BrandonZwicker I'm glad to help, please post any updates to your code in your question – Shar1er80 Apr 23 '15 at 17:10