我一直在C#中製作一個卡匹配遊戲。它使用圖片陣列設置了12張卡片(每張照片2張,總共6張照片),當您按下卡片時,通過使用另一個數組(一個數組爲1的int數組) -6重複兩次,相同的照片)我使用此代碼隨機數字匹配遊戲,使照片在C中相匹配#
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];
}
它適用於比賽的4,那麼它說的匹配是錯誤的休息...誰能幫助?
這裏是我的代碼的其餘部分:
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];
}
}
}
}
如果使用__points__的圖片和標籤數組索引的數據結構,可以使用隨機播放功能:http://stackoverflow.com/questions/273313/randomize-a-listt-in-c -sharp –
即時通訊對不起,我不知道如何使用此... @matthew –
因爲您正在使用它們來確定要使用哪個標籤,因此分配給您的PictureBox的名稱是什麼? – Shar1er80