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];
}
}
}
}